Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDialog and JButton Focus acting strange.

 
Jeff Sadowski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I'm not using things correctly but I'm having issues with setting focus.
Here is test code that is giving me the problem.



Running in Windows 7
when I run the program from one command line then invoke it (ie: http://127.0.0.1:8080 in a web browser)
It brings up the dialog box but not focused.
How can I get it to be focused?

Second problem is the focused button when I focus the window I expect enter key to do the same as pressing the focused button but this is not happening. Do I need another action listener on the JDialog to do this?

(I edited the code to run more cleanly on the connected web browser)
 
Jason Bullers
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the javadoc (emphasis mine):


Requests that this Component get the input focus, and that this Component's top-level ancestor become the focused Window. This component must be displayable, visible, and focusable for the request to be granted.
This method is intended for use by focus implementations. Client code should not use this method; instead, it should use requestFocusInWindow().


Call requestFocusInWindow() before you call setVisible() and it should work fine, I think. I'm not sure about the issue with pressing enter. I think that should generate an action event. You could try registering your focused_button as the default button for your container: frame.getRootPane().setDefaultButton(focused_button); On Windows, I believe that should treat the button as the target for hitting enter, even if it doesn't have focus.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the GUI forum.
 
Jeff Sadowski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Bullers wrote:From the javadoc (emphasis mine):

Call requestFocusInWindow() before you call setVisible() and it should work fine, I think.

Seems to work the same.
Jason Bullers wrote:
I'm not sure about the issue with pressing enter. I think that should generate an action event. You could try registering your focused_button as the default button for your container: frame.getRootPane().setDefaultButton(focused_button); On Windows, I believe that should treat the button as the target for hitting enter, even if it doesn't have focus.

It did select the button when I pressed enter; however instead of setting it to one button is there a way to have the focused button (not my button I named focused_button but which ever button is focused) to be activated when the enter key is pressed.

Thank you your some suggestions.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a way to have the focused button (not my button I named focused_button but which ever button is focused) to be activated when the enter key is pressed.

Check out Rob Camick's article on Enter Key and Button.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic