Search code examples
javaactionlistenerjcombobox

add item to JComboBox from another JDialog


Hi I need help with filling ComboBox (ClassA) from ClassAddItem. After selecting "Add New Item" from the ComboBox, call the new window. When you click "Add new item" (ClassAddItem) I want to join a new item into the ComboBox - class ClassA.

In a ClassA is also button "Add New Item" it works, but how do I add a new item to the CB from another class???

Item "Add new item" in a ComboBox is deliberately not in the array String[] cbItem, because in my application possibilities Loading from a file and I do not want this item is visible in the file. Also, it would be advantageous if the item "Add new item" still at the end of the ComboBox.

Thank you for your response sorry for my English :)

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

Solution

  • you can make a constructor using the combobox...

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

    and use this comboBox within your actionListener

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