JCombobox focusLost is not firing-why is that?

后端 未结 2 1040
栀梦
栀梦 2020-12-11 03:53

I have a JCombobox in my code. I have added the FocusLost event. But it didn\'t fired anyway. I have tried lots of time but didn\'t find solution.<

2条回答
  •  借酒劲吻你
    2020-12-11 04:38

    I have found a very simple way to solve this.

    The JComboBox default editor has an internal class BasicComboBoxEditor$BorderlessTextField that is the component that gets and loses focus.

    It can be accessed simply by

    Component component = comboBox.getEditor().getEditorComponent();  
    if (component instanceof JTextField) 
        JTextField borderlesstextfield = (JTextField) borderless;
    

    Then add a focus listener like you would to any JTextField

    borderlesstextfield.addFocusListener(new FocusListener() 
    {
       public void focusGained(FocusEvent e) 
         {
         }
       public void focusLost(FocusEvent e) 
         {
         }
    }});
    

    Now you have a FocusListener that will respond as expected to gain and loss of focus for the ComboBox

提交回复
热议问题