Forums Register Login

JDialog and JButton Focus acting strange.

+Pie Number of slices to send: Send
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)
1
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
Moving to the GUI forum.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
Bring out your dead! Or a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2180 times.
Similar Threads
coding a texteditor: want to open a separate option window
Focus problem with setModal in JDialog
JButton + ActionListeners Question
How can i get specific button in JOptionaPne
Problem Updating a JButton
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:34:25.