简体   繁体   中英

Java Jtable error with setting a value to a column after listener runs

I am working on the TableModelListener for a JTable. Now the jtable can get the value fine. but when it comes down to setting the value of the last column of the selected row. It gets wonky, not sure what the error is it just hangs up. Not seeing any errors on netbeans so not sure what to think. Because of this I am not even sure if the if statements work at all in terms of setting values. Should I be using or doing something else for this to happen ? Update: It does appear to be an infinite loop. edited the code to a suggestion to check for Update table events but still having the same problem. Here is the code below:

public  class MyListener implements TableModelListener {

    @Override
    public void tableChanged(TableModelEvent tme) { 

     if (tme.getType() == TableModelEvent.UPDATE)
        {
           int rowcount = jDetailSubmitTable.getSelectedRow();

           // Initial return when table is starting to be filled.
        if(rowcount == -1 )
        {
            return;
        }

        int com = tme.getColumn();

        // Number being Validated.
        if(jDetailSubmitTable.getModel().getValueAt(rowcount, com).toString().trim().isEmpty())
        {
            JOptionPane.showMessageDialog(null, "Invaid Number selected.");
            jDetailSubmitTable.getModel().setValueAt("0", rowcount, com);
            return;
        }
        try
        {
          Double.parseDouble(jDetailSubmitTable.getModel().getValueAt(rowcount, com).toString().trim());
        }
        catch(NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Invaid Number selected.");
            jDetailSubmitTable.getModel().setValueAt("0", rowcount, com);
            return;
        }


        // Adjusted amount is calculated below.

        double nur = Double.parseDouble(jDetailSubmitTable.getModel().getValueAt(rowcount, 9).toString().trim());
        double our = Double.parseDouble(jDetailSubmitTable.getModel().getValueAt(rowcount, 8).toString().trim());
        double diff = nur - our;
        double nunits = Double.parseDouble(jDetailSubmitTable.getModel().getValueAt(rowcount, 11).toString().trim());
        double ans = diff * nunits;

        jDetailSubmitTable.getModel().setValueAt(ans, rowcount, 12);  
    }
 }

}

The following line will get the selected row index, that index being a view index since you ask this from the JTable instance:

int rowcount = jDetailSubmitTable.getSelectedRow();

Later on you use this view index to index the model:

jDetailSubmitTable.getModel().getValueAt(rowcount, com)

You should first convert this view index to a model index using JTable.convertRowIndexToModel :

int selrowid = jDetailSubmitTable.getSelectedRow();
selrowid = jDetailSubmitTable.convertRowIndexToModel(selrowid);
[...]jDetailSubmitTable.getModel().getValueAt(selrowid, com)[...]

I gave a longer explanation about view vs model and the need to convert indexes in this answer on SO.


Update: @camickr made the correct observation that you should be using the data that comes with the TableModelEvent , ie TableModelEvent.getFirstRow() , TableModelEvent.getLastRow() and TableModel.getColumn() . These methods return model indexes.

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