Search code examples
javaswingalignmentjtextfield

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

Solution

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