0

Hi

I have a JTextpane where i will be adding instructions. I know this will not be large enough, but there is no further space on my interface, so I need to add either a scrollbar or scrollpane.

I have tried a few attempts at this but cannot get either to appear. Can anyone help me with this?

Should I be using a scrollbar or scrollpane for a textpanel?

I just need it so the user can scroll down the text which isn't visible.

instructionTextPanel = new javax.swing.JTextPane();
slider = new JScrollPane(instructionTextPanel);
// instructionTextPanel.add(slider);
//slider.addAdjustmentListener(this);
instructionTextPanel.setFont(new java.awt.Font("MS Sans Serif", 1, 14));
instructionTextPanel.setText("instructions will go in here!");
instructionPanel.add(instructionTextPanel);
instructionTextPanel.disable();
instructionTextPanel.setBounds(6,7,175,179);
instructionTextPanel.setBorder(new javax.swing.border.TitledBorder(null, "Instructions", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 16), new java.awt.Color(0, 102, 102)));

instructionPanel.setBounds(0,0,190,260);
getContentPane().add(instructionPanel);

I did read somewhere that the scrollbars will appear when the preferred size of the panel is greater than the size of the pane, but couldn't set this up.

Is it that I should use a JTextArea instead of a JTextPane?

Any pointers would be appreciated :)

2
Contributors
4
Replies
6
Views
11 Years
Discussion Span
Last Post by JavaFish
0

Replace the line

instructionPanel.add(instructionTextPanel);

with

instructionPanel.add(slider);

The scrollpane slider was created to wrap the textpane. You must add that object to the panel not the original text pane.

Are you sure you want this JTextPane to be disabled?

For more help, www.NeedProgrammingHelp.com

0

Hi

Thanks for the reply. I have replaced that line, but then the instruction textpane doesn't show at all?

Yeah, need the textpane disabled as I don't want the user to be able to overwrite my instructions!!

Can you help me a bit further?

Thanks very much :lol:

0

This is the code that works when I changed the line. Run the code below.

import javax.swing.*;
import java.awt.*;

class TextPane
{
	public static void main(String[] args)
	{
		JFrame jf = new JFrame();
		JPanel instructionPanel = new JPanel();
		jf.setLayout(null);
		
			JTextPane instructionTextPanel = new javax.swing.JTextPane();
			JScrollPane slider = new JScrollPane(instructionTextPanel);
			// instructionTextPanel.add(slider);
			//slider.addAdjustmentListener(this);
			instructionTextPanel.setFont(new java.awt.Font("MS Sans Serif", 1, 14));
			instructionTextPanel.setText("instructions will go in here!");
			instructionPanel.add(slider);
		//	instructionTextPanel.disable();
			instructionTextPanel.setBounds(6,7,175,179);
			instructionTextPanel.setBorder(new javax.swing.border.TitledBorder(null, "Instructions", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 16), new java.awt.Color(0, 102, 102)));
			
			instructionPanel.setBounds(0,0,190,260);
			jf.getContentPane().add(instructionPanel);
		
		jf.setBounds(100,100,400,300);
		jf.setVisible(true);
		
	}
}

Maybe it is showing but the size of the scroll pane ends up being much too small. Try to set the bounds of it.

slider.setBounds(6,7,175,179);

For more help, www.NeedProgrammingHelp.com

0

Hi

Yes, it was the setBounds, so set this and once I add enough text to warrant a scrollbar it appears - fantastic! :mrgreen:

Thanks loads for your help with this, I knew it was simple and that I was being simple!!! :o

Maybe it is showing but the size of the scroll pane ends up being much too small. Try to set the bounds of it.

slider.setBounds(6,7,175,179);

Edited by diafol: fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.