2017-08-27 81 views
-1

我有多個JSpinner其中有項1-10和一個JTable。點擊JSpinner後,該值將被添加到JTable。我想獲得行號,所以我可以在setValueAt上使用它。但當JSpinner點擊超過一次時,我會收到錯誤消息。java.lang.ArrayIndexOutOfBoundsException

代碼

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

隨着從零開始的指數,像'JTable' /'TableModel','getRowCoun()'返回的行數,比最後一行的指數大。 EG如果你有3行,它們索引爲0; 1; 2.沒有索引3 ... –

+0

@UsagiMiyamoto我以爲2是列? – Tony

+0

stacktrace顯示調用了'setValueAt()'方法,並調用了'Vector.elementAt()',這引發了異常。調用'setValueAt()'方法的列值爲常量3 ... –

回答

1

我認爲你的設置不正確。

您使用數量來「猜測」索引。如果數量變爲1,則向該表添加新行,如果它不是1,則嘗試修改表中的一行。這有幾個故障:

  • 如果有多個微調,單擊每個,最後再次點擊其中任何一個,哪一行應該被修改?
  • 如果你在旋轉器上點擊兩次,然後點擊其-按鈕降低,而且再次成爲1,將增加一個新的行...

爲了解決上述問題,有幾個選項。
最微不足道的一個會實現自己的TableModel不是基於DefaultTableModel ...
另一種方法,可以添加一列到現有DefaultTableModel用於搜索,並使其不可見。該列可以是EG的JSpinner實例。

因此,嘗試這樣的事:

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; 
     } 
    } 
} 
+0

它註銷了什麼? –

+0

它顯示_Adding row_ – Tony

+0

謝謝,它的工作原理! – Tony

相關問題