java.lang.ArrayIndexOutOfBoundsException

时间:2017-08-27 15:31:05

标签: java arrays jtable setvalue

我有多个JSpinner,其中有1-10项,还有一个JTable。单击JSpinner后,该值将添加到JTable。我想得到行号,以便我可以在setValueAt使用它。但是当JSpinner点击一次以上时我收到错误。

代码

public void stateChanged(ChangeEvent e) {  // when JSpinner clicked
        int quantity = (int) ((JSpinner) e.getSource()).getValue();
        int rows = table.getRowCount();

        for (int i = 0; i < ELEMENTS; i++) {
            if (numspinner[i] == e.getSource()) {
                if (quantity == 1) {
                    System.out.println("Row"+rows);
                    dtm.addRow(new Object[] { foodLabel[i].getText(), quantity, price[i] * quantity });
                } else {
                    System.out.println("Row"+rows);
                    dtm.setValueAt(quantity, rows, 3); 
                }
            }
        }
    }

我再点击一次相同的JSpinner然后获得此输出

Row1
Row2

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
        at java.util.Vector.elementAt(Unknown Source)
        at javax.swing.table.DefaultTableModel.setValueAt(Unknown Source)
        at gui.FoodOrdering.stateChanged(FoodOrdering.java:250)
        at javax.swing.JSpinner.fireStateChanged(Unknown Source)
        at javax.swing.JSpinner$ModelListener.stateChanged(Unknown Source)
        at javax.swing.AbstractSpinnerModel.fireStateChanged(Unknown Source)
        at javax.swing.SpinnerNumberModel.setValue(Unknown Source)
        at javax.swing.JSpinner.setValue(Unknown Source)
        at javax.swing.plaf.basic.BasicSpinnerUI$ArrowButtonHandler.actionPerformed(Unknown Source)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

任何帮助都将不胜感激。

我已经移动了一些代码,这里是最新的

  public void stateChanged(ChangeEvent e) {
            int quantity = (int) ((JSpinner) e.getSource()).getValue();
            int rows = 0;

            for (int i = 0; i < ELEMENTS; i++) {
                if (numspinner[i] == e.getSource()) {
                    if (quantity == 1) {
                        System.out.println("Row"+rows);
                        rows = table.getRowCount();
                        dtm.addRow(new Object[] { foodLabel[i].getText(), quantity, price[i] * quantity });
                    } else {
                        System.out.println(" The Row"+rows);
                        dtm.setValueAt(quantity, rows, 1); // obj,column,row
                    }
                }
            }   
    }

如果数量为1,则按预期添加行。再次单击相同的JSpinner时,它始终显示0 !!!

1 个答案:

答案 0 :(得分:1)

我认为您的设置不正确。

您使用数量来预测&#34;猜测&#34;指数。如果数量变为1,则向表中添加新行,如果不是1,则尝试修改表中的行。这有一些缺点:

  • 如果有多个微调器,请单击每个微调器,最后再次单击其中任何一个,应该修改哪一行?
  • 如果您点击微调器两次,然后点击其-按钮减少再次变为1,则会添加一个新行...

为了解决上述问题,有几个选择 最简单的一个是实现自己的TableModel而不是基于DefaultTableModel ...
另一种方法可能是在现有DefaultTableModel中添加一列用于搜索,并使其不可见。此列可以是JSpinner实例的EG。

所以尝试这样的事情:

public void stateChanged(ChangeEvent e) {
   final int quantity = (int) ((JSpinner) e.getSource()).getValue();
   final int rows = table.getRowCount();
   for (int row = 0; row < rows; ++row) {
      // look for the row that has the JSpinner in its last column
      if (dtm.getValueAt(row, 3) == e.getSource()) {
         // log out something
         System.out.println("Modifying row " + row);
         // modifying the value in the model
         dtm.setValueAt(quantity, row, 1); // obj, row, column
         return;
      }
   }
   // there was no row with this JSpinner, so we have to add it
   for (int i = 0; i < ELEMENTS; i++) {
      // looking for the "clicked" JSpinner
      if (numspinner[i] == e.getSource()) {
         // log out something
         System.out.println("Adding row " + rows);
         // adding a new row to the model
         dtm.addRow(new Object[] { foodLabel[i].getText(), quantity, price[i] * quantity, numspinner[i] });
         return;
      }
   }   
}