JDialog box not gaining focus

*爱你&永不变心* 提交于 2019-12-12 12:55:05

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6626169/jdialog-box-not-gaining-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!