This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
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
Pie
Posts: 64629
86
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: 2539
9
  • 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: 5125
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic