How to set fixed width but dynamic height on JTextPane?

后端 未结 2 1077
孤城傲影
孤城傲影 2020-12-21 03:44

I want to use a JTextpane with fixed width but dynamic height which should also allow wrapping. The height should change as the user adds or removes text. I wou

2条回答
  •  长情又很酷
    2020-12-21 04:06

    Use a JScrollPane with no horizontal scroll.

    JFrame frame = new JFrame();    
    JPanel pnel = new JPanel(); 
    frame.setContentPane(pnel); 
    JTextPane txtpane = new JTextPane();    
    txtpane.setPreferredSize(new Dimension(200, 200));
    JScrollPane jsp = new JScrollPane(txtpane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    pnel.add(jsp);  
    frame.pack();
    frame.setVisible(true);
    

    This will not wrap text. To wrap text take a look at these: http://java-sl.com/wrap.html , http://java-sl.com/tip_html_letter_wrap.html

    I will suggest to use JTextArea not JTextPane

提交回复
热议问题