Detecting when user presses enter in Java

后端 未结 2 741
天涯浪人
天涯浪人 2021-01-18 07:14

I have a subclass of JComboBox. I attempt to add a key listener with the following code.


        addKeyListener(new KeyAdapter() 
        {
            pub         


        
2条回答
  •  一个人的身影
    2021-01-18 07:35

    This is NOT the proper approach. The editor for a JComboBox is a JTextField. If you want to handle the Enter key then you add an ActionListener to the text field.

    Always avoid using KeyListeners.

    Edit:

    comboBox.getEditorComponent().addActionListener( ... );
    

提交回复
热议问题