![](/img/trans.png)
[英]Adding JCombobox in JTable - ArrayIndexOutOfBoundException
[英]ArrayIndexOutOfBoundException when adding cell values to a JTable
如上圖所示;
當用戶選擇項目和項目名稱並單擊添加按鈕時,會出現以下情況:
但隨后將ArrayIndexOutOfBound
錯誤顯示為:
java.lang.ArrayIndexOutOfBoundException: 1>=1
然后選擇另一個項目和項目名稱並執行上述過程顯示相同的錯誤:
java.lang.ArrayIndexOutOfBoundException: 2>=2
同時將Qty,Discount,Total列重置為最后一行值。
添加按鈕動作執行方法;
private void add_btnActionPerformed(java.awt.event.ActionEvent evt) {
int i=0;
int j=0;
String temp = (String) IDcombo.getSelectedItem();
String temp2 = (String) Namecombo.getSelectedItem();
String sql = "select ItemID,ItemName,CostPrice from druginfo where ItemID=?";
try {
pst=conn.prepareStatement(sql);
pst.setString(1, temp);
rs=pst.executeQuery();
addDataToTable(tableSale,DbUtils.resultSetToTableModel(rs));//this method for adding multiple lines in the table
IDcombo.setSelectedItem(null);
Namecombo.setSelectedItem(null);
exptxt.setText(null);
instock.setText(null);
//getting user input for selling qty
String Qty=JOptionPane.showInputDialog("Insert Selling Quantity :");
double sellingqty=Double.parseDouble(Qty);
//getting user input for specific item discount
String discount = JOptionPane.showInputDialog("Insert Item Discount");
double idiscount=Double.parseDouble(discount);
for(j=0;j<100;j++){
double icost =(double) tableSale.getModel().getValueAt(j,2);
System.out.println(icost);
//calculating Gross Total
double grosstotal = (sellingqty*icost)-idiscount;
System.out.println(grosstotal);
for(i=0;i<100;i++){
//setting qty input value to table sale
tableSale.getModel().setValueAt(sellingqty,i, 3);
//setting input value to table sale
tableSale.getModel().setValueAt(idiscount,i, 4);
//setting grosstotal value to table sale
tableSale.getModel().setValueAt(grosstotal,i, 5);
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "error "+ex);
ex.printStackTrace();
}
}
這是堆棧跟蹤;
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:664)
at com.bit.project.Newsale.add_btnActionPerformed(Newsale.java:720)
java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java:664)
at com.bit.project.Newsale.add_btnActionPerformed(Newsale.java:720)
刪除for循環並使用此代碼
int i = jTable1.getRowCount()-1;
double icost = (double) tableSale.getModel().getValueAt(i, 2);
System.out.println(icost);
//calculating Gross Total
double grosstotal = (sellingqty * icost) - idiscount;
System.out.println(grosstotal);
//setting qty input value to table sale
tableSale.getModel().setValueAt(sellingqty, i, 3);
//setting input value to table sale
tableSale.getModel().setValueAt(idiscount, i, 4);
//setting grosstotal value to table sale
tableSale.getModel().setValueAt(grosstotal, i, 5);
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
表示只有一行,但是你正在訪問第一行的第一行索引行。但是沒有第二行。你添加新的行是真的,但當你調用getValueAt()
細胞應該存在
在我的代碼中
int i = jTable1.getRowCount()-1;
int我將得到最后一行索引,例如,如果只有1行,那么我是0。所以從當前行獲取值使用getValueAt(i, 2);
.same to setValueAt();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.