本文介绍了在Swing中,Enter键无法从组合框中选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将ItemListener添加到JComboBox。 ItemListener能够使用鼠标单击或空格键检测我所做的任何选择。但它没有识别Enter键。换句话说,如果我使用箭头键滚动ComboBox中的条目然后按Enter键,则不会调用itemStateChanged方法。你有什么想法吗?

I have added an ItemListener to a JComboBox. The ItemListener is able to detect any selection I make using Mouse click or space-bar. But it is not recognizing the Enter Key. In other words, if I use arrow key to scroll through entries in ComboBox and then hit Enter, then the itemStateChanged method is not invoked. Do you have any ideas?

推荐答案


  • 从<$ c $收听更改c> MouseEvent 是 ItemListener ,触发 SELECTED / DESELECTED

    用于收听 ENTER键按下 ActionListener

    其他关键事件可以从JComboBox的JList中确定

    rest of key events is possible to determine from JComboBox's JList

    例如

    import java.awt.Component;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.BasicComboBoxRenderer;
    
    public class ComboBoxHoverOver {
    
        private JComboBox combo = new JComboBox();
    
        public ComboBoxHoverOver() {
            combo.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXXXXXX");
            combo.setRenderer(new ComboToolTipRenderer(combo));
            combo.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    System.out.println(combo.getSelectedItem().toString());
                }
            });
            combo.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println(combo.getSelectedItem().toString());
                }
            });
            combo.addItem("");
            combo.addItem("Long text 4");
            combo.addItem("Long text 3");
            combo.addItem("Long text 2");
            combo.addItem("Long text 1");
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(combo);
            f.pack();
            f.setVisible(true);
        }
    
        private class ComboToolTipRenderer extends BasicComboBoxRenderer {
    
            private static final long serialVersionUID = 1L;
            private JComboBox combo;
            private JList comboList;
    
            ComboToolTipRenderer(JComboBox combo) {
                this.combo = combo;
            }
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (comboList == null) {
                    comboList = list;
                    KeyAdapter listener = new KeyAdapter() {
    
                        @Override
                        public void keyReleased(KeyEvent e) {
                            if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_UP) {
                                int x = 5;
                                int y = comboList.indexToLocation(comboList.getSelectedIndex()).y;
                                System.out.println(comboList.getSelectedIndex());
                            }
                        }
                    };
                    combo.addKeyListener(listener);
                    combo.getEditor().getEditorComponent().addKeyListener(listener);
                }
                if (isSelected) {
                    System.out.println(value.toString());
                }
                return this;
            }
        }
    
        public static void main(String[] args) {
    
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ComboBoxHoverOver comboBoxHoverOver = new ComboBoxHoverOver();
                }
            });
        }
    }
    

    这篇关于在Swing中,Enter键无法从组合框中选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:14