Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to place cursor in a JTextField

Status
Not open for further replies.

prgmrgirl

Programmer
Feb 12, 2004
119
US

I can't believe this is so difficult to do in Java. I grow to dislike this language more and more as time goes on.

I have tried .requetFocus(), .requestFocusInWindow(), setCaretPosition().

I can't believe how many hours I have spent on something that should be so simple...

Can someone please explain how this is done?

Thanks
 
Let me clarify something. You are using the Swing part in Java and Swing can be not easy. It seems you have to do lot of coding to get little GUI result.

What do you mean cursor? Do you mean the mouse pointer?
Java program in Swing do not mouse the mouse pointer into textfield. If you want to do so, you can
Code:
Robot robot = new Robot();
robot.mouseMove(x, y);

If you mean cursor is the place for insertion of text, yourJTextField.requestFocus() will work.

If I misunderstanding your question, please post again with some simple program that we can run.
 
Thanks so much for the responses!

Ah swing, how I hate thee! LOL

I am using a JTable which uses a JComboBox as a cell editor. When the user is done, I would like for them to be able to hit 'enter' to go to the next line. Going to the next line is easy. But moving the focus to the next line does not activate the cell editor (ie put the carat in the editor). The user must click or hit some key in order to begin typing.

I was able to use robot to simulate the user pressing a key but it ended up screwing up some other things so I had to remove that. Tried editor.requestFocus() but it does not work. Tried table.editCellAt(row, col) but that didn't work either. Not sure where to go with this. It's making me crazy!

I hope someone out there knows how to do this!
 
prosper,

Thanks for the link. Unfortunately, it's not so much the movement from one line to the other that is the problem. I am able to do that just fine. The issue is that once I get to the next line, I cannot activate the editor. The cell is selected but the editor doesn't necessarily have the focus, which I confirmed using a focusListener. If I could get the focus to the editor (in my case, a combobox) then it would work perfectly.

Thanks for the info!

Anyone else have any ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top