How do I align text within a JTextField?

Adrian Jandl picture Adrian Jandl · Mar 23, 2013 · Viewed 25.2k times · Source

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.

Answer

1ac0 picture 1ac0 · Mar 23, 2013

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