newbee
newbee

Reputation: 409

set alignment of JTextField

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|
|----------------------|

Upvotes: 2

Views: 457

Answers (2)

hendalst
hendalst

Reputation: 3085

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

Upvotes: 0

patrick.elmquist
patrick.elmquist

Reputation: 2140

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

Upvotes: 3

Related Questions