Is it Possible to show a previously hidden JFrame using a keylistener

半腔热情 提交于 2019-12-02 03:26:37

Visible components can have the keyboard focus, and recieve keyboard and other input events, but invisible components cannot have the focus, nor recieve input events. When you make the frame invisible, it stops recieving input events, as does all it's children, including the test component you are adding a KeyListener to.

To make this work, you will have to have a visible component, and forward keyboard events from that to your invisible frame. Or better, call invisibelFrame.setVisible(true) from your already visible component.

Alternatively, you may be able to find some other way to trigger showing the frame. For example, a system tray component, with a context menu is a common pattern for hiding and showing application frames (and for some users, causing much chagrin in the process!)

See

As a general rule you should NOT be using a KeyListener. That was the old way of doing things when only AWT was around.

When using Swing you should be using Key Bindings. The binding won't solve your problem with invisible components and frames but this is approach all Swing component use.

Yes it is possible but you will need to use a external java library with a native library. Search for "JIntellitype". But this will only work on Windows. If you want to use this functionality on linux, use "JXGrabKey"

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