简体   繁体   中英

How can I prevent JTextPane to grow horizontally…?

I am building an application with several tabs containing JPanels with JTextPanes. While testing, I wrote text longer than the screen, and the JTextPane grows horizontally. Does somebody know a way to stop it from growing like that? Also, how can I force that the text, once the line has been completed, jumps to the next line on the JTextPane?

Thanks!

UPDATE: I opted for the first option that Julien Gauthier mentioned, which is using JTextArea and enabling the Wrapping. It worked neatly. Given that the size of my text boxes (or areas) is more than enough for the limit I established as text capture (800 characters), I did not need to add Scrolls to the text areas. And also, I did not had any trouble with my save-text-to-file functions with JTextAreas instead of JTextPanes. Thanks a lot for the help!

I wrote text longer than the screen, and the JTextPane grows horizontally. Does somebody know a way to stop it from growing like that?

Add the text pane to a JScrollPane and then add the scroll pane to the frame. Then the text will wrap and scrollbars will appear when required.

Read the section from the Swing tutorial on Text Component Features for a working example of a text pane.

You have several possibilities :

  • You can first replace the JTextpane by a JTextArea, and use jTextArea.setLineWrap(boolea wrap).

  • You can use an editorKit with your JTextPane, wich will wrap the text. Please look this example : http://java-sl.com/tip_html_letter_wrap.html

  • You can use a JScrollPane, but the result will be ugly with long sentences.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM