如何解决如何在 Java 中的 JTable 中正确显示二维数组的值?
我无法在 JTable 中显示二维数组的值 似乎每次我在现有输入之后添加另一个新输入时,它都会在某种程度上显示添加到二维数组中的所有输入,其中它连续显示每次添加新输入时添加的所有现有输入。>
流程是:
- 用户向文本字段输入值。
- 用户单击“新建产品”按钮,然后显示输入的值(均存储在二维数组中)
(但我的问题是,我无法按顺序正确显示值,我想我有索引问题或其他问题,但我似乎无法指出它在哪里。)
这是我的源代码:
获取文本字段值并将其存储到二维数组中的函数:
public static String [][] getPInputs(){ // 2D array that gets the values from the textfields
String getPID = pid.getText();
String getPType = ptype.getText();
String getPDesc = pdesc.getText();
String getSupplier = supplier.getText();
String getQuantity = quantity.getText();
String Orders = "0";
String [][] stringPinputs = {{getPID,getPType,getPDesc,getSupplier,getQuantity,Orders}};
return stringPinputs;
}
在将二维数组的值显示到 JTable 时执行的按钮事件:
private void newproductBtnActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel tblmodel1 = (DefaultTableModel)firstTable.getModel();
pid.setText(String.valueOf(pidIncreSave++));
int Product2DINDEX = 0; //index of 2D array
String[][] ProductAllData1 = new String[10][6]; //2D array(stores only up to 10 unique elements)
String[][] receivedPInputs = getPInputs(); // 2D array that calls the function and gets its values(from a 2D array)
for(int q = 0; q < receivedPInputs.length; q++){ // Inserts 2D array(receivedPInputs) into the ProductAllData1 2D array
ProductAllData1[Product2DINDEX] = receivedPInputs[q];
Product2DINDEX++;
}
for(int m = 0; m < ProductAllData1.length; m++){ // Prints the elements of ProductAllData1 2D array
for(int n = 0; n < ProductAllData1[n].length; n++){
System.out.println(ProductAllData1[m][n] +",");
}
}
for(int a = 0; a < ProductAllData1.length; a++){ // Displays 2D array in first table
if(ProductAllData1[a][0] != null){
if(setRowCountID == 0)
tblmodel1.setRowCount(0);
tblmodel1.addRow(ProductAllData1[a]);
setRowCountID++;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。