Swing: Enter detection in JComboBox?

I tried using getInputMap () + getActionMap () on a JComboBox and it seems to have no effect.

I tried addActionListener () / addItemListener () on a JComboBox, and I cannot distinguish the selection change from someone pressing the Return / Enter key.

Any suggestions? In my application, I want the Return / Enter key to be stronger than just a choice, it's a choice + action.


Here is my code for setting key binding. It works fine (for example, note("hit ENTER")is called) when the component is JList, but does not work when the component is a JComboBox.

private void setupApplyProfile(final JComponent component, final MyComboBoxModel mcbm)
{   
    String enterAction = "applyItem";
    KeyStroke enterKey = KeyStroke.getKeyStroke("ENTER");
    component.getInputMap().put(enterKey, enterAction);
    component.getActionMap().put(enterAction, new AbstractAction()
    {
        @Override public void actionPerformed(ActionEvent e) {
            note("hit ENTER");
            applySelectedProfile(mcbm);
        }
    });
}
+3
source share
2 answers

, , : note("cb editor action") , Enter .

        comboBox.getEditor().addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent arg0) {
                note("cb editor action");
            }               
        });
+5

, Return/Enter , , + .

, :

comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);

ActionEvent ItemEvents , . EEvents , /.

+3

Source: https://habr.com/ru/post/1791603/


All Articles