I would like to have a JTextPane that have scroll bar, how can I do so ? Thanks.
Asked
Active
Viewed 3.5k times
5 Answers
20
To insert a scroll bar on your new JTextPane, just use a JScrollPane:
JTextPane txt = new JTextPane();
JScrollPane jsp = new JScrollPane(txt);
JTextPane API: http://download.oracle.com/javase/6/docs/api/javax/swing/JTextPane.html
JScrollPane API: http://download.oracle.com/javase/6/docs/api/javax/swing/JScrollPane.html
If you have some issues, take a look at this SO question : Java JTextPane JScrollPane Display Issue
Or take a look at: http://www.daniweb.com/software-development/java/threads/30283
![](../../users/profiles/-1.webp)
Community
- 1
- 1
![](../../users/profiles/702048.webp)
Alberto Solano
- 7,454
- 3
- 33
- 57
5
Wrap it into a JScrollPane. Read the swing tutorial about scroll panes.
![](../../users/profiles/571407.webp)
JB Nizet
- 633,450
- 80
- 1,108
- 1,174
3
Just put the JTextPane in a JScrollPane.
public class SomeFrame
{
public static void main( String[] args )
{
JFrame frame = new JFrame( );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JTextPane tp = new JTextPane();
JScrollPane sp = new JScrollPane(tp);
frame.getContentPane().add( sp );
frame.pack( );
frame.setVisible( true );
}
}
![](../../users/profiles/812149.webp)
S.L. Barth
- 7,954
- 71
- 47
- 62
1
Here is the code to add a scrollbar to the TextBox
JEditorPane edtDTWinfo = new JEditorPane();
edtDTWinfo.setEditable(false);
edtDTWinfo.setBorder(new LineBorder(Color.ORANGE, 2));
edtDTWinfo.setForeground(Color.BLUE);
JScrollPane spEditor = new JScrollPane(edtDTWinfo,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
spEditor.setBounds(0, 0, 200, 300);
Add component "spEditor" to the JPanel
0
Before this just add a ScrollPane to ContentPane in Design and add EditopPane to ScrollPane as child
JScrollPane sp = (JScrollPane)contentPane.getComponent(23);//this is in my hierarchy 23
JViewport vp = sp.getViewport();
JEditorPane ep = (JEditorPane)vp.getView();
![](../../users/profiles/2074608.webp)
Qantas 94 Heavy
- 14,790
- 31
- 61
- 78
![](../../users/profiles/3455624.webp)
Vladdi
- 1
- 2