paudel.sulav
paudel.sulav

Reputation: 162

On clicking the JTable row, the valueChanged(ListSelectionEvent e) method is calling multiple times

I have the JTable with multiple columns. When I clicked on the table rows, the value changed method call multiple times and at last it throw an error of

java.lang.ArrayIndexOutOfBoundsException: -1

My code is

subjectTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            System.out.println("Value IS Adjusting --> " + e.getValueIsAdjusting());

             try {

                if(subjectTable.getSelectedRow() == -1)
                    return;

                if(!e.getValueIsAdjusting()) {

                    System.out.println("Selected Row --> " + subjectTable.getSelectedRow());
                    System.out.println("Selected Value of Column 0 --> " + subjectTable.getValueAt(subjectTable.getSelectedRow(), 0).toString());

                    cmbClass.setSelectedItem(subjectTable.getValueAt(subjectTable.getSelectedRow(), 0).toString());
                    txtSubjectName.setText(subjectTable.getValueAt(subjectTable.getSelectedRow(), 1).toString());
                    txtFullMarks.setText(subjectTable.getValueAt(subjectTable.getSelectedRow(), 2).toString());
                    txtPassMarks.setText(subjectTable.getValueAt(subjectTable.getSelectedRow(), 3).toString());
                    cmbGrade.setSelectedItem(subjectTable.getValueAt(subjectTable.getSelectedRow(), 4).toString());
                }
            } catch (IndexOutOfBoundsException ex) {
                ex.printStackTrace();
                new KILogger("Error in the value changed, Edit Subject table", ex);
            }
        }
    });

I got the error like this:

enter image description here

Upvotes: 1

Views: 124

Answers (1)

paudel.sulav
paudel.sulav

Reputation: 162

I didn't know what is this issue. But now it is solved by doing following code:

subjectTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {

             try {

                if(subjectTable.getSelectedRow() == -1)
                    return;

                if(!e.getValueIsAdjusting()) {

                    txtSubjectName.setText(subjectTable.getValueAt(subjectTable.getSelectedRow(), 1).toString());
                    txtFullMarks.setText(subjectTable.getValueAt(subjectTable.getSelectedRow(), 2).toString());
                    txtPassMarks.setText(subjectTable.getValueAt(subjectTable.getSelectedRow(), 3).toString());
                    cmbGrade.setSelectedItem(subjectTable.getValueAt(subjectTable.getSelectedRow(), 4).toString());
                }
            } catch (IndexOutOfBoundsException ex) {
                ex.printStackTrace();
                new KILogger("Error in the value changed, Edit Subject table", ex);
            }
        }
    }); `

Upvotes: 1

Related Questions