How to have a Scrollable JTextPane?

淺唱寂寞╮ 提交于 2019-11-30 22:59:26

问题


I would like to have a JTextPane that have scroll bar, how can I do so ? Thanks.


回答1:


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




回答2:


Wrap it into a JScrollPane. Read the swing tutorial about scroll panes.




回答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 );
  }
}



回答4:


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




回答5:


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();


来源:https://stackoverflow.com/questions/8182473/how-to-have-a-scrollable-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!