0

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

  1. 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.

  2. on the small frame I have text fields which changes depending on the button pressed on the main frame.

  3. only on start I initialize the small frame but after the first time I use it just by changing the text field values

  4. the small frame has an exit button once pressed the frame is hidden using Jframe.toback() and once a button is clicked I use Jframe.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.

c0der
  • 18,467
  • 6
  • 33
  • 65
flashDisk
  • 55
  • 5
  • 2
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 12 '17 at 11:19
  • @AndrewThompson it is not a code problem, I need to trigger an event for the frame to be activated which will enable its sub panel child to request the focus and gain it. – flashDisk Mar 12 '17 at 12:04
  • *"it is not a code problem"* Did I say it was? But since you state there's none - if there's no problem, what are you doing here? 'Problem solving' is easily 99% of what's allowable on this Q&A Site. BTW - since there's no MCVE / SSCCE, voting to close. – Andrew Thompson Mar 12 '17 at 12:08
  • @AndrewThompson I posted the question and I need a help in how to make a frame active when calling tofront java method on it, the code is very complex and I am pretty sure it is not related to the code rather than to how I said earlier. – flashDisk Mar 12 '17 at 12:16
  • *"I am pretty sure it is not related to the code"* Likewise, I'm pretty sure I could not be bothered looking into it unless you provide runnable code. But hey, it's your conundrum, and I have only an academic interest in solving the ..'not a problem'. Right now there are lots of other people requesting help, so I'll leave it to you to think about it while I help them. – Andrew Thompson Mar 12 '17 at 12:22
  • @AndrewThompson ok thanks for your help but I am not allowed to paste any thing from the code :( – flashDisk Mar 12 '17 at 12:25
  • @flashDisk `code is very complex` - It doesn't need to be. You have one JFrame with a button. You have a second JDialog with a text field. When you click on the button the dialog is made visible and gets focus. The other Swing components or business logic is irrelevant to the question. `I am not allowed to paste any thing from the code` - which is why you create the [mcve] that demonstrates the problem. Chances are you will find the problem as you create the `MCVE`. – camickr Mar 12 '17 at 17:01
  • @camickr I found why this is happening!, the small frame has an exit button when pressed it is moved to back of the main frame but when pressing on the exit button which I added, didn't use the frame default one, the frame gains the focus although it is not the visible frame, I tried to send an AWT event grammatically to the small frame but that didn't help. – flashDisk Mar 13 '17 at 08:30
  • @flashDisk a verbal description doesn't help. Post your [mcve] demonstrating the problem, then maybe someone will look at the code. – camickr Mar 13 '17 at 14:45

0 Answers0