从另一个JDialog添加项目到JComboBox

时间:2014-09-22 10:49:32

标签: java actionlistener jcombobox

您好我需要帮助从 ClassAddItem 填充ComboBox(ClassA)。选择" 添加新项"从ComboBox中,调用新窗口。当您点击" 添加新项目" (ClassAddItem)我想将一个新项加入ComboBox - 类ClassA。

在ClassA中也是按钮"添加新项目"它有效,但如何从另一个类向CB添加新项目???

项目" 添加新项目"在一个ComboBox中故意不在数组String [] cbItem中,因为在我的应用程序中可能从文件加载,我不希望这个项目在文件中可见。此外,如果项目"添加新项目"仍然在ComboBox的末尾。

感谢您的回复 对不起我的英文:)

public class ClassA extends JFrame {
    private JPanel contentPane;
    public JComboBox comboBox;
    private String[] cbItem = {"A", "B", "C"};

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ClassA frame = new ClassA();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public ClassA() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 291, 152);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);        
        comboBox = new JComboBox(cbItem);
        comboBox.addItem("add new Item");
        comboBox.setBounds(10, 11, 116, 23);
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JComboBox comboBox = (JComboBox) event.getSource();
                Object selected = comboBox.getSelectedItem();
                if(selected.toString().equals("add new Item")) {
                    ClassAddItem cAI = new ClassAddItem();
                    cAI.setVisible(true);
                }
            }
        });
        contentPane.add(comboBox);        
        JButton btnAddNewItem = new JButton("add new item");
        btnAddNewItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                comboBox.addItem("this works");
            }
        });
        btnAddNewItem.setBounds(136, 11, 116, 23);
        contentPane.add(btnAddNewItem);
    }
}

ClassAddItem:

public class ClassAddItem extends JDialog {

    public ClassAddItem() {
        setBounds(100, 100, 266, 155);
        getContentPane().setLayout(null);
        JButton btnNewButton = new JButton("Add new item");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ClassA cA = new ClassA();
                cA.comboBox.addItem("this does not work");
            }
        });
        btnNewButton.setBounds(62, 11, 120, 23);
        getContentPane().add(btnNewButton);
    }
}

1 个答案:

答案 0 :(得分:0)

你可以使用组合框来构建一个构造函数......

private JComboBox comboBox;
//constructor
public ClassAddItem(JComboBox box) {
    this.comboBox = box;
    ....
}

并在actionListener中使用此comboBox

public void actionPerformed(ActionEvent e) {
    this.comboBox.addItem("this does work");
}