0

我想在单击 CheckBoxListComboBox 中的 Ok 和 Cancel 按钮时收听事件 有人知道如何注册 Ok 和 Cancel 按钮上的事件吗?如果事件注册是不可能的,我们可以覆盖我们自己的确定和取消按钮吗?

4

1 回答 1

1

似乎没有注册监听器的选项。但是,您可以覆盖getDialogOKAction()getDialogCancelAction()。您还可以覆盖createListChooserPanel()并在那里提供您自己的操作。

例如:

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);
            }
        });
    }
}
于 2014-08-22T23:12:54.737 回答