简体   繁体   中英

How to stop JTable TableModelListener from firing or listening when specific column on table changed

I have created a TableModelListener which listens to specific column. Specifically, columns 2,3,4,5 . Such that when any column from 2-5 has value or not null , it will take and add the values store to a BigDecimal value to get the sum . After that, I need to set the value of the 6th column to the sum of values of columns 2-5 . So I thought I can simply collect the values then if it encounters column 6 , it will set the sum.

Here's the code.

@Override
    public void tableChanged(TableModelEvent e) {
        DefaultTableModel tableModel = (DefaultTableModel) e.getSource();
        if (tableModel.getRowCount() > 0) {
            for (int row = 0; row < tableModel.getRowCount(); row++) {
                BigDecimal sum = new BigDecimal(BigInteger.ZERO).setScale(2,RoundingMode.HALF_UP);
                for (int col = 0; col < tableModel.getColumnCount(); col++) {
                    if (col == 2 || col == 3 || col == 4 || col == 5) {
                        if (tableModel.getValueAt(row, col) != null) {
                            sum = sum.add(new BigDecimal(Double.parseDouble(tableModel.getValueAt(row, col).toString())));
                        }
                    } 
                }
                JOptionPane.showMessageDialog(null,"Sum: "+sum);
                tableModel.setValueAt(sum, row, 6);
            }
        }
    }

So what happens is I get an infinite JOptionPane Dialog with the value of sum. I researched online and thought that maybe after editing I could terminate the listener by adding view.getMyJTable().putClientProperty("terminateEditOnFocusLost", true);

Didn't help. I basically just want to be able to get the sum of columns 2-5 whether or not the columns has value that is why I initialized the BigDecimal to ZERO

It appears to be listening table model changes even on 6th column. I don't know how to select specific columns where the tablechanged will apply.

Thanks.

I've already solved my problem. Sorry for asking this. The solution was simple. I did some research and found a method called e.getColumn()

Thanks anyway for viewing my question.

@Override
    public void tableChanged(TableModelEvent e) {
        int colChanged = e.getColumn();
        if (colChanged == 2|| colChanged == 3 || colChanged == 4 || colChanged == 5) {
            DefaultTableModel tableModel = (DefaultTableModel) e.getSource();
            if (tableModel.getRowCount() > 0) {
                for (int row = 0; row < tableModel.getRowCount(); row++) {
                    BigDecimal sum = new BigDecimal(BigInteger.ZERO).setScale(2, RoundingMode.HALF_UP);
                    for (int col = 0; col < tableModel.getColumnCount(); col++) {
                        if (col == 2 || col == 3 || col == 4 || col == 5) {
                            if (tableModel.getValueAt(row, col) != null) {
                                sum = sum.add(new BigDecimal(Double.parseDouble(tableModel.getValueAt(row, col).toString())));
                            }
                        }
                    }
                    JOptionPane.showMessageDialog(null, "Sum: " + sum);
                    tableModel.setValueAt(sum, row, 6);
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM