开发者

How to hide the JWindow when I press Enter key?

开发者 https://www.devze.com 2023-04-02 11:02 出处:网络
I have a frame and a JWindow. In my frame I have a textfield, whenever I type something to the fiel开发者_StackOverflow中文版d, the window will appear with list of suggestions below the textfield. I u

I have a frame and a JWindow. In my frame I have a textfield, whenever I type something to the fiel开发者_StackOverflow中文版d, the window will appear with list of suggestions below the textfield. I used a keylistener to the field. When I press the enter key on the list of suggestion in the window, the word that I select goes to the field.

Now the problem is that the window still appear, I want the window to disappear whenever I select a word.

Could someone got an idea about this?

Thanks..


Try this:

jWindowInstance.setVisible(false);


I'm assuming you have an OK button on there, in which case you should be able to set the default button on the root pane of the window, e.g.

window.getRootPane().setDefaultButton(okBtn);


Try this:

jWindowInstance.addKeyListener(new KeyAdapter() {
   public void keyReleased(KeyEvent e) {
      if(e.getKeyCode() == KeyEvent.VK_ENTER) {
         jWindowInstance.dispose(); // Release resources

         // OR
         jWindowInstance.setVisble(false); // Just hide the window so you can reuse it afterwards
      }
   }
});


You can send the selected item to the textbox, right? I assume that you have used some kind of event listener to do that. At the end of the action method, make the window's visibility to false. Swaranga's way should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消