I have a problem which I am stuck with about 3 days, I have read a lot of answers regarding my problem but none solved my issue.
Discription
I have java application which have two frames ,the main frame has a buttons. Once one is selected a new small frame is opened on the top of the main frame.
on the small frame I have text fields which changes depending on the button pressed on the main frame.
only on start I initialize the small frame but after the first time I use it just by changing the text field values
the small frame has an exit button once pressed the frame is hidden using
Jframe.toback()
and once a button is clicked I useJframe.tofront()
to make it visible.
The problem
when I press a button on the main frame I want the focus to be granted for the first text field,I need to see the mouse cursor on it.
When I start the first time the focus is granted to that text field and when I press the exit button and select another button it works but sometimes when I press very quickly the focus is not granted and the frame is not activated.
I need a way to force the small frame to be activated once opened.
when one of the buttons is clicked on the main frame I change the data in the small frame and run this code:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
logger.debug("isDisplayable() + isFocusable() + isVisible());
idField.grabFocus();
}
});
the first time I start the application everything works just fine because I use the method requestFocusInWindow()
before the frame becomes visible.
After that the above code does not always works, it seems to me like a race condition.
How can I force the small frame gain focus once a button is clicked to be active?
thanks for any help.