Set Cursor On A JTextField

橙三吉。 提交于 2019-12-30 06:57:31

问题


I am making a small application in Java that uses a JTextField. Now, I want, as soon as I run the app, the cursor to be put automatically in that so that the user doesn't have to click on it and then write the text. I have tried pretty much everything that I found on the net: setCaretPosition(0), grabFocus(), requestFocusInWindow() or requestFocus() but none of them worked! I am desperate, can you please help me solve this? Thanks a lot


回答1:


By default focus will go to the first component on the Window.

If the text field is not the first component then you can use:

textField.requestFocusInWindow();

however you must invoke this method AFTER the window is visible.

If you want specify the component with focus before the window is visible then you can use the RequestFocusListener approach from Dialog Focus.




回答2:


this works properly for cursor position textField.requestFocus();



来源:https://stackoverflow.com/questions/18908902/set-cursor-on-a-jtextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!