TransWikia.com

Como capturar el evento click de un Jcombobox

Stack Overflow en español Asked by Fabian Peñaloza on January 6, 2021

tengo el siguiente código para un jcombobox, donde necesito poder capturar también el evento cuando haga clic en uno de los items del mismo.

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();
                    }
                }
            }
    );
}

2 Answers

Estimados encontré esta manera para identificar unicamente cuando el evento provenga de la acción del 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 Fabian Peñaloza on January 6, 2021

Para eso se necesita el ItemListener lo puedes agregar de la siguiente forma.

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

Este escuchará cuando el item seleccionado haya cambiado.

Answered by Sr1871 on January 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP