Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 886
Adrian Jandl Avatar asked Mar 23 '13 07:03

Adrian Jandl


People also ask

How do I center text in a JTextField?

Just use the setBounds attribute. Jtextfield. setBounds(x,x,x,x);

Can the program put text in JTextField?

The class JTextField is a component that allows editing of a single line of text.


1 Answers

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

like image 85
1ac0 Avatar answered Sep 21 '22 05:09

1ac0