1

I am making JComboBox in JTable in Java Swing which have two items.

When I click on the combo box an ActionEvent fires and my ActionListener is notified.

When I select an item in the combo box the listener is called again.

comboBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String school_id = null;
            JComboBox comboBox = (JComboBox) e.getSource();
            String access_value = comboBox.getSelectedItem().toString();
            final int selectedRowIndex = table.getSelectedRow();
            System.out.println("selected row: " + selectedRowIndex);
            if(selectedRowIndex == -1) {
                System.out.println("returned value");
                return;
            } else {
                school_id = (String) table.getModel().getValueAt(selectedRowIndex, 2);
            }
            if(adminDaoImpObj.updateSchoolAccount(school_id, access_value) > 0) {
                //System.out.println("updated success");
            } else {
                System.out.println("fail");
            }
            System.out.println(school_id + "--------");
        }
    });

When I click on the combo box, it shows the previously selected school_id value.

After selecting an item in the combo box, it shows currently selected row school_id value,

but I want it to show only currently selected school_id value.

Any help is apreciated.

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Vishal Singh
  • 621
  • 3
  • 7
  • 17
  • I think you have to use another Listener type. – Diversity Dec 14 '13 at 06:30
  • You have to use ItemChangeListener: Have a look here: http://stackoverflow.com/questions/58939/jcombobox-selection-change-listener – Diversity Dec 14 '13 at 07:41
  • 1
    Post an [SSCCE](http://sscce.org) that shows your problem. – Paul Samsotha Dec 14 '13 at 08:11
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Please don't forget to add a '?' to questions! Some people do a search in the page for '?' and if none exists in the 'question' go directly to the next (actual) question in line. – Andrew Thompson Dec 14 '13 at 23:12

1 Answers1

2

Try to use ItemListener instead. Maybe this results in your preferred behaviour.

You have to create a new actionListemer for each combobox in a newly created row and assign it to the associated combobox.

Mayby u assigne always the same ActionListener to the combobixes for each row which makes no sense. This could be a problem.

Diversity
  • 1,890
  • 13
  • 20
  • thanks for your reply. I have used this but its give same result – Vishal Singh Dec 14 '13 at 07:11
  • pls tell me if i am wrong but you just want to see the school id of the selected table row within you combobox or do you want that the row entry changes when th combobox selelection changes, – Diversity Dec 14 '13 at 07:24
  • I want to change my combobox selected value in database according to respective school_id but when i click on combobox then action listener fires which have previous selected school_id but i want it has currently row school_id. – Vishal Singh Dec 14 '13 at 08:33
  • Sry this answer was not usefull but at the moment i don't know what kind of problem you have, cause it seems to me that there are different kind of issues which have to be resolved seperately – Diversity Dec 14 '13 at 08:45
  • Try to seperate database behaviour from ui behaviour and check if you updates work in the way you want – Diversity Dec 14 '13 at 08:46
  • After that simulate UI behaviour with mockobject to ensure that UI works – Diversity Dec 14 '13 at 08:47
  • After that combine or tell your question again Sry that i didn't solve your problem – Diversity Dec 14 '13 at 08:48