Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting the cursor in a text field after clicking a button?  RSS feed

 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have a JButton programmed in a SWing GUI that when I click it, it will take the score and put it into a JLabel somewhere else. The thing is when I click the JButton the cursor doesn't reset back to the JTextField. How would I go about doing that?

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing? HTML? Other?
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Swing? HTML? Other?


I'm sorry about that, I am using a Swing GUI with JButton and JTextField
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
textfield.requestFocusInWindow()

but it might be simpler to have
button.setFocusable(false)
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:but it might be simpler to have
button.setFocusable(false)


Then you can't use the keyboard and tab to the button.

A GUI should support keyboard and mouse UI.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
Michael Dunn wrote:but it might be simpler to have
button.setFocusable(false)


Then you can't use the keyboard and tab to the button.

A GUI should support keyboard and mouse UI.


Yeah... I am able to type in a number and press enter on the keyboard, and I am able to click the button... I like to personally press enter, but my buddy likes to click calculate... He said he would like the cursor to be automatically set to the JtextField... I thought that using some sort of focusable, but I'm not really sure how they work...
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:textfield.requestFocusInWindow()

but it might be simpler to have
button.setFocusable(false)



.requestFocusInWindow() worked thanks! So this basically keeps the cursor in the window at all times?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Orsaw wrote:.requestFocusInWindow() worked thanks! So this basically keeps the cursor in the window at all times?

The method does what the API says. You have read the API, haven't you?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!