2

My question is with reference to this post here JTextField : How to set text on the left of JTextField when text too long

If the string is too long, is there any way to display the text in the next subsequent lines instead of a single line?

Right now, it displays the string as

|----------------------|
| JTextField example ..|
|----------------------|

Is there any way i can make it like this?

|----------------------|
| JTextField example ..|
|..continue string.....|
|.........end of string|
|----------------------|
Community
  • 1
  • 1
newbee
  • 409
  • 2
  • 12
  • 34

2 Answers2

3

If you want a field with several lines you should use a

JTextArea textArea = new JTextArea()

instead since the JTextField does not support it (afaik).

EDIT
To get the JTextArea to scroll you need to put it in a JScrollPane like this:

JFrame frame = new JFrame();
JTextArea textArea = new JTextArea("Test");
textArea.setLineWrap(true);
//textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane);
patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35
  • thanks.. but when i use `JTextArea`, do i need to add something extra with it so that it is scroll-able? Right now, it displays only part of the text and is not scroll-able – newbee Apr 24 '13 at 15:09
  • Updated my answer with scroll explanation – patrick.elmquist Apr 24 '13 at 15:16
  • Thanks for the explanation :) Isn't there any way in which it automatically breaks the long sentence and displays into multiple lines based on the textArea size? – newbee Apr 24 '13 at 15:23
  • Updated again, you need to set `textArea.setLineWrap(true)`, the line after is if you want it to wrap whole words instead – patrick.elmquist Apr 24 '13 at 15:35
0

Your linked post indicates you simply wish to display a long string. If you don't require editing, I'd suggest using a JLabel and using HTML tags.

JLabel myLabel = new JLabel("<html>I will wrap.</html>");
hendalst
  • 2,957
  • 1
  • 24
  • 25