File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JDialog and JButton Focus acting strange. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JDialog and JButton Focus acting strange." Watch "JDialog and JButton Focus acting strange." New topic
Author

JDialog and JButton Focus acting strange.

Jeff Sadowski
Greenhorn

Joined: Jan 23, 2012
Posts: 8
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
Greenhorn

Joined: Dec 27, 2011
Posts: 28

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

Joined: May 03, 2008
Posts: 4658
    
    5

Moving to the GUI forum.


luck, db
There are no new questions, but there may be new answers.
Jeff Sadowski
Greenhorn

Joined: Jan 23, 2012
Posts: 8
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

Joined: May 03, 2008
Posts: 4658
    
    5

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDialog and JButton Focus acting strange.