Java Editable JCombobox Keylistener event for Enter key

前端 未结 6 1819
太阳男子
太阳男子 2020-12-11 05:56

I have editable JCombobox and I added keylistener for combobox editor component. When user press \'Enter key\' and if there is no text on the editable combobox I need to dis

相关标签:
6条回答
  • 2020-12-11 06:37

    Please check if this code helps you!!!

    JFrame frame = new JFrame("Welcome!!");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JComboBox cmb = new JComboBox();
    cmb.setEditable(true);
    cmb.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
    
        @Override
        public void keyReleased(KeyEvent event) {
            if (event.getKeyChar() == KeyEvent.VK_ENTER) {
                if (((JTextComponent) ((JComboBox) ((Component) event
                        .getSource()).getParent()).getEditor()
                        .getEditorComponent()).getText().isEmpty())
                    System.out.println("please dont make me blank");
            }
        }
    });
    frame.add(cmb);
    
    frame.setLocationRelativeTo(null);
    frame.setSize(300, 50);
    frame.setVisible(true);
    

    Most people find it difficult because of this casting.

    0 讨论(0)
  • 2020-12-11 06:38

    We need to add a key listener on the component that the combo box is using to service the editing.

    JTextComponent editor = (JTextComponent) urCombo.getEditor().getEditorComponent();
    editor.addKeyListener(new KeyAdapter() {
       public void keyReleased(KeyEvent evt) {
          // your code
       }
    });
    

    Hope this code helps.

    0 讨论(0)
  • 2020-12-11 06:40
    Note that I can't use Action listener for this.
    

    this doesn't make me any sence, then to use ItemListener

    Any idea how to solve this?
    
    • never to use KeyListener for Swing JComponents, use (Note that I can't use Action listener for this.) KeyBindings instead,

    • notice ENTER key is implemented for JComboBox in API by default, have to override this action from ENTER key pressed

    0 讨论(0)
  • 2020-12-11 06:43

    If you are using Netbeans then right click on your combobox and select customize code. add following lines of code

    JTextComponent editor = (JTextComponent) Code.getEditor().getEditorComponent();
    editor.addKeyListener(new KeyAdapter() {
       public void keyReleased(KeyEvent evt) {
            if(evt.getKeyCode()==10)
                //do your coding here.
       }
    });
    
    0 讨论(0)
  • 2020-12-11 06:51

    Please check the event ascii code by ev.getkeycode() and check if it is a number or character. If it is neither a number nor a character do nothing. If it is what you want then do the process.

    0 讨论(0)
  • 2020-12-11 06:54

    One option would be to replace the KeySelectionManager interface with your own. You want to replace the JComboBox.KeySelectionManager as it is responsible for taking the inputted char and returns the row number (as an int) which should be selected.

    0 讨论(0)
提交回复
热议问题