swing - Detecting Keyboard Input while Having a Focus on JpopupMenu (Java) -


i know if there way detect keyboard input while having focus on jpopupmenu. remove focus on jpopupmenu whenever there input detection keyboard. possible?

thank you.

below simplified code have written.

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.caretevent; import javax.swing.event.caretlistener; import javax.swing.event.changelistener; import javax.swing.event.changeevent; import java.net.*; import java.io.*; public class testclass { static jpopupmenu textpopupmenu = new jpopupmenu("menu"); final static jtextarea textinput = new jtextarea(50,80); final static jpanel overallpanel = new jpanel(); final static jframe overallframe = new jframe("test"); public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { final actionlistener actionlistener1 = new actionlistener() { public void actionperformed(actionevent actionevent) { textpopupmenu.setfocusable(false); } }; keylistener textinputlistener = new keyadapter() { @override public void keypressed(keyevent e) { //get suggested words function , populate them jmenuitem textpopupmenu = new jpopupmenu("menu"); for(int i=0;i<5;i++) { switch(i) { case 0: jmenuitem item1 = new jmenuitem("a"); textpopupmenu.add(item1); break; case 1: jmenuitem item2 = new jmenuitem("b"); textpopupmenu.add(item2); break; case 2: jmenuitem item3 = new jmenuitem("c"); textpopupmenu.add(item3); break; case 3: jmenuitem item4 = new jmenuitem("d"); textpopupmenu.add(item4); break; case 4: jmenuitem item5 = new jmenuitem("e"); textpopupmenu.add(item5); break; }; } textpopupmenu.setfocusable(true); if (textpopupmenu.isvisible()) { textpopupmenu.setlocation(0, 0 + 20); } else { textpopupmenu.show(textinput,0, 0 + 20); } } }; textinput.addkeylistener(textinputlistener); overallpanel.add(textinput); overallframe.getcontentpane().add(overallpanel); overallframe.setdefaultcloseoperation(jframe.exit_on_close);`enter code here` overallframe.setsize(1000, 900); overallframe.setlocationrelativeto(null); overallframe.setvisible(true); } }); } }

i'm not quite sure why did this, have use jpopupmenu.addmenukeylistener().

when tested this, events delivered twice, had store time of last event obtained event.getwhen() , process events newer last stored time.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -