简体   繁体   English

Java Jtable错误,在侦听器运行后将值设置为列

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

I am working on the TableModelListener for a JTable. 我正在为JTable开发TableModelListener。 Now the jtable can get the value fine. 现在,jtable可以得到很好的值。 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. 在netbeans上没有看到任何错误,因此不确定要怎么做。 Because of this I am not even sure if the if statements work at all in terms of setting values. 因此,我什至不确定if语句是否在设置值方面完全起作用。 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: 下面的行将获取选定的行索引,该索引是视图索引,因为您从JTable实例询问了这一点:

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 : 您应该首先使用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. 在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() . 更新: @camickr正确地观察到您应该使用TableModelEvent附带的数据,即TableModelEvent.getFirstRow()TableModelEvent.getLastRow()TableModel.getColumn() These methods return model indexes. 这些方法返回模型索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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