Adrian Jandl Adrian Jandl - 7 months ago 17
Java Question

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.

Answer

At first, JTextArea should be more accurately. Or use JLabel:

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

and the call:

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

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