Search code examples
javaswingalignmentjtextfield

How do I align text within a JTextField?


I have a JTextField with rather big bounds, the text however does not behave like I want it to.

 _________________
|                 |
|                 |
|text             |
|                 |
|_________________|

how would i do it so my text aligns like this

 _________________
|text             |
|                 |
|                 |
|                 |
|_________________|

Edit: Using JTextArea fixed my problem. Thank you.


Solution

  • JTextArea aligns to the top.

    Or use a JLabel:

    JLabel myLabel = new JLabel("my text");
    

    and the call:

    myLabel.setHorizontalAlignment(SwingConstants.LEFT);
    myLabel.setVerticalAlignment(SwingConstants.TOP);
    

    Layout managers is the another way of doing this: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html