Stanley Mungai
Stanley Mungai

Reputation: 4150

Set the Horizontal Position of a cursor on JTextField

I have a Swing Application where a user has to Input Some information. I need the cursor By default to be at Position 10 of the JtextField: I have tried these Two Methods but none of them has worked for me:

JTextField text = new JTextField("          ", 50);
text.setHorizontalAlignment(10)

The other one I have tried is

JTextField text = new JTextField("          ", 50);
text.setCaretPosition(10)

Is there really a way to do what am trying?

Upvotes: 3

Views: 1747

Answers (2)

Eng.Fouad
Eng.Fouad

Reputation: 117597

Try this:

text.getCaret().setDot(10);

Upvotes: 4

Autar
Autar

Reputation: 1589

Doesn't the problem come from your JTextField containing an empty String ?

If you want the cursor to be at a set position, this position should be reachable, i.e having a String containing 10 blank spaces.

PS : I think setCaretPosition is the right method here.

Upvotes: 3

Related Questions