3

I have a modeless dialog box being generated which prompts users to open a new window. The box can be opened in two ways, either directly from the file menu for the frame I'm writing or indirectly via the framework my panel is plugging into.

When I make the call directly via the file menu the dialog box comes up with focus exactly as I want. But when I have the framework indirectly open the dialog box it does not have focus as it should.

There doesn't seem to be a difference between the two methods of opening the dialog, in both cases a load function is called and it's not until 5 method calls later the dialog box is opened. In both cases the frame which generates the dialog box is realized at the time the box is generated. I've tried calling requestFocus after making the dialog box visible but it doesn't seem to do anything.

Any suggestion why the dialog box wouldn't have focus, or how I can give it focus as a separate window from the window that usually has focus?

drew
  • 577
  • 2
  • 8
  • 20
  • He already tried it, it would appear. I think there's also the `grabFocus()` function you could try? – aardvarkk Jul 08 '11 at 14:52
  • You might want to have a look at question http://stackoverflow.com/questions/309023/howto-bring-a-java-window-to-the-front – 01es Jul 08 '11 at 14:58
  • What 'framework'? Does it have a method to set a parent component for dialogs? If not, perhaps you should approach the authors of the framework to provide such a method, rather than try to code around that shortcoming. – Andrew Thompson Jul 08 '11 at 17:17

2 Answers2

3

in some cases is hard to set Focus to the expected top-level container as are demonstrated here , but for excelent workaround would be better look at camickr's Dialog Focus

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

When you create the dialog, try setting the main GUI as parent of the dialog.

In the first case, when you click from menu, it automatically sets the main GUI as the parent of the dialog, but it doesnt in the second case.

So make sure when you create the dialog, you are setting the main GUI/ window as parent always.

It should help most times.

Nick
  • 1,692
  • 3
  • 21
  • 35
  • This was the issue. The call I was making to set the top level window was returning null when I opened the dialog indirectly. I really should have known that, My GUI skills aren't as good as my other Java skills it would seem :) – drew Jul 08 '11 at 17:29