javajide

Ok and cancel button handle in Jide CheckBoxListComboBox


I would like to listen to events when clicked on Ok and Cancel buttons in CheckBoxListComboBox Does any one know how to register for events on Ok and Cancel buttons? If the events registration is not possible, can we override the Ok and cancel buttons of our own?


Solution

  • It seems there is no option to register a listener. However, you can override getDialogOKAction() and getDialogCancelAction(). You can also override createListChooserPanel() and provide you own actions there.

    For example:

    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import com.jidesoft.combobox.CheckBoxListComboBox;
    
    public class TestCheckboxList extends JPanel{
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {   
                public void run() {   
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLocationByPlatform(true);
    
                    String[] items = {"Item1", "Item2", "Item3"};
    
                    frame.add(new CheckBoxListComboBox(items){
                        @Override
                        protected Action getDialogOKAction() {
                            return new AbstractAction() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    System.out.println("OK");
                                }
                            };
                        }
    
                        @Override
                        protected Action getDialogCancelAction() {
                            return new AbstractAction() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    System.out.println("Cancel");
                                }
                            };
                        }
                    });
    
                    frame.pack();
    
                    frame.setVisible(true);
                }
            });
        }
    }