osx - Editable JComboBox in Java 8 does not forward Enter key to default button -



osx - Editable JComboBox in Java 8 does not forward Enter key to default button -

i have editable jcombobox, focus on combobox, when press enter key, behavior differs between java 8 compared older versions.

this code works expected in java 7 , below, not in java 8 (tested in oracle jvm in mac):

package org.wiztools.comboboxdefaultbutton; import java.awt.borderlayout; import java.awt.container; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.swingutilities; /** * * @author subhash */ public class myframe extends jframe { private static myframe me; public myframe() { container c = getcontentpane(); c.setlayout(new borderlayout()); // press come in key focus on component: jcombobox jcb = new jcombobox(new string[]{"option: one", "option: two"}); jcb.seteditable(true); c.add(jcb, borderlayout.center); jbutton jb = new jbutton("ok"); jb.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { joptionpane.showmessagedialog(me, "button pressed!"); } }); swingutilities.getrootpane(c).setdefaultbutton(jb); c.add(jb, borderlayout.east); pack(); setvisible(true); } public static void main(string[] arg) { swingutilities.invokelater(new runnable() { @override public void run() { me = new myframe(); } }); } }

how create code work in java 8 , below uniformly?

i found next code in handler class of basiccomboboxui in jdk7. handler added editor actionlistener:

// // actionlistener // // prepare 4515752: forwards come in pressed on // editable combo box default button // note: depend on event ordering. first actionevent // editor may handled jcombobox in case, // enterpressed action invoked. public void actionperformed(actionevent evt) { object item = combobox.geteditor().getitem(); if (item != null) { if(!combobox.ispopupvisible() && !item.equals(combobox.getselecteditem())) { combobox.setselecteditem(combobox.geteditor().getitem()); } actionmap = combobox.getactionmap(); if (am != null) { action action = am.get("enterpressed"); if (action != null) { action.actionperformed(new actionevent(combobox, evt.getid(), evt.getactioncommand(), evt.getmodifiers())); } } }

i guess can check jdk8 source see if alter has been made.

if changes have been made might need create own actionlistener invokes "enterpressed" action of combo box , add together action editor of combo box manually.

java osx swing java-8 jcombobox

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -