How to capture the click event of a Jcombobox

0

I have the following code for a jcombobox, where I need to be able to also capture the event when I click on one of the items in it.

private void initComboKeyListener() {
    filterEditor.getFilterLabel().addKeyListener(
            new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    char keyChar = e.getKeyChar();
                    if (!Character.isDefined(keyChar)) {
                        return;
                    }
                    int keyCode = e.getKeyCode();
                    switch (keyCode) {
                        case KeyEvent.VK_DELETE:
                            return;
                        case KeyEvent.VK_ENTER:
                            selectedItem = comboBox.getSelectedItem();
                            resetFilterComponent();
                            return;
                        case KeyEvent.VK_ESCAPE:
                            resetFilterComponent();
                            return;
                        case KeyEvent.VK_BACK_SPACE:
                            filterEditor.removeCharAtEnd();
                            break;
                        default:
                            filterEditor.addChar(keyChar);
                    }
                    if (!comboBox.isPopupVisible()) {
                        comboBox.showPopup();
                    }
                    if (filterEditor.isEditing() && filterEditor.getText().length() > 0) {
                        applyFilter();
                    } else {
                        comboBox.hidePopup();
                        resetFilterComponent();
                    }
                }
            }
    );
}
    
asked by Fabian Peñaloza 29.08.2018 в 23:18
source

2 answers

0

For that you need the ItemListener you can add it in the following way.

TuJcomboBox.addItemListener(new ItemListener() {
  public void itemStateChanged(ItemEvent itemEvent) {
    //tu código
  }
});

This will listen when the selected item has changed.

    
answered by 30.08.2018 в 00:12
0

Dear, I found this way to identify only when the event comes from the action of the mouse.

        private void initComboMouseListener() {
       comboBox.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
            System.out.println(" ################ mouseClicked");
            selectedItem = comboBox.getSelectedItem();
            resetFilterComponent();
        }

        @Override
        public void mousePressed(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println(" ################ mouseEntered");
            selectedItem = comboBox.getSelectedItem();
            resetFilterComponent();
        }

        @Override
        public void mouseExited(MouseEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    });
    
answered by 30.08.2018 в 00:57