Dynamically resize textarea to fit content

后端 未结 3 1241
梦毁少年i
梦毁少年i 2021-01-27 03:00

Here is a textarea I have put in my program using netbeans. The first image shows what the textarea looks like when I run the program. The second image is show the textarea afte

相关标签:
3条回答
  • 2021-01-27 03:24

    Try this http://java-sl.com/tip_text_height_measuring.html

    I haven't tried it with JTextArea but suppose it should work

    0 讨论(0)
  • 2021-01-27 03:25

    Include the JTextArea inside a JScrollPane

    JTextArea textArea = new JTextArea();
    JScrollPane scrollArea = new JScrollPane(textArea);
    

    This will dynamically change the text area based on if scrolling is needed or not

    0 讨论(0)
  • 2021-01-27 03:33

    I wouldn't use JTextArea for anything except prototyping. It has the bitter functionality of notepad. But it's up to you.

    I would use a JEditorPane or JTextPane. I know you can't size it based on character size but that's for the best. For word wrap, you can do setContentType("text/html"); and wrap the text in <'p><'/p> tags.

    (Note you still use JScrollPane for scrolling. In fact, the scroll pane works for any component)

    See the differences: http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html

    0 讨论(0)
提交回复
热议问题