Grigoris Loukidis
Grigoris Loukidis

Reputation: 403

Two comboboxes sync in java

i'm creating an application in java (eclipse) and i have a problem..i want two sync two comboboxes..the user selects a company from the first one and after that the second one will full with the employees of the selected company..if i select another company again the second combobox will full with the new selected company's employees. My problem is that when I try to select another company the second combobox with the employees doesnt sync..Any solution/suggestion?!

fists code:

JComboBox comboBox = new JComboBox();
        comboBox.setBounds(53, 53, 280, 20);
        epiloghEtairiasGiaPanel.add(comboBox);
        for(int i=0;i<c.getEtairies().size();i++){
            comboBox.addItem(c.getEtairies().get(i).getName());
        }
         String name = comboBox.getSelectedItem().toString();

seconds code:

for(int i=0;i<c.getEtairies().size();i++){
            if(c.getEtairies().get(i).getName().equals(name)){
            for(int j=0;j<c.getEtairies().get(i).getErgazomenoi().size();j++){

                    comboBox_1.addItem(c.getEtairies().get(i).getErgazomenoi().get(j).getSurname());    

            }

            }
        }

Upvotes: 1

Views: 572

Answers (1)

Xipo
Xipo

Reputation: 1823

You need to add a listener which "listens" to selection changes

    comboBox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            String name = comboBox.getSelectedItem().toString();
            for (int i = 0; i < c.getEtairies().size(); i++) {
                if (c.getEtairies().get(i).getName().equals(name)) {
                    for (int j = 0; j < c.getEtairies().get(i).getErgazomenoi().size(); j++) {

                        comboBox_1.addItem(c.getEtairies().get(i).getErgazomenoi().get(j).getSurname());

                    }
                }
            }
        }
    });

Upvotes: 2

Related Questions