gpt4 book ai didi

java - 检查 jTable 数据中的重复行

转载 作者:行者123 更新时间:2023-12-01 15:55:09 25 4
gpt4 key购买 nike

下面是我编写的代码,用于帮助执行以下操作:

从要添加到 jTable 的 jComboBox 中进行选择,然后检查它是否满足以下三个条件中的哪一个:如果表为空,则添加它。如果从组合框中选择的项目存在于表中,则现有行的数量值加一。或者,如果它尚未出现在表中,则将其添加。

但是我得到的是非常奇怪的行为,如下所示:

  1. 第一行已添加到表格中,其值为“200”(应为“100”,因为在此选择之前表格为空)。

  2. 从组合框中选择第二个项目,第一行(上面)的值更改为“100”(这应该在第一次选择时发生)。此外,从组合框中选择的第二个项目将两次添加到表中(两个相同的行),值为“300”,值是正确的,但只能是一行。

  3. 组合中的第三个选择与上面的 2 相同,因此值是正确的,但只能是一行

  4. 选择了组合框中的第四个选项,这次是为了匹配表中的现有行,它不是更新现有行中的值,而是添加值为“300”的两行。 .

我想也许我的循环是错误的,但我现在在试图解决这个问题的项目上远远落后,所以任何帮助都会得到极大的帮助......

提前致谢

final DefaultTableModel model = (DefaultTableModel)main.tillPanel.tblTillSale.getModel();
//populate the combo box
for (int d = 0; d < roundStockObj.length ; d++) {
main.tillPanel.cmbTillProdSelect.addItem(roundStockObj[d].getDescription());
}
//add selection listener to combo
main.tillPanel.cmbTillProdSelect.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
String[] addSelectedItem = new String[4];
selectedItem = main.tillPanel.cmbTillProdSelect.getSelectedItem();

for (int d = 0; d < roundStockObj.length; d++) {
//when selction form combo is matched, an array is created to hold the row data
if (roundStockObj[d].getDescription().equals(selectedItem)) {
addSelectedItem[0] = roundStockObj[d].getDescription();
addSelectedItem[2] = Double.toString(roundStockObj[d].getPrice()).trim();
addSelectedItem[3] = Double.toString(roundStockObj[d].getPrice()).trim();
}
}
main.tillPanel.tblTillSale.removeRowSelectionInterval(0, model.getRowCount());

//if table is empty
for (int rowCount = 0 ; rowCount <= model.getRowCount(); rowCount++) {
if (model.getRowCount() == 0 ) {
addSelectedItem[1] = "100";
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
//main.tillPanel.tblTillSale.getModel().setValueAt(selectedItem, tillSelectedRow, tillSelectedRow);
main.tillPanel.lblTotPrice.setText("100");
break;
}
// look for duplicate row and if found increase total column of existing row, and not add this selection
if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getValueAt(rowCount, 0))) {
main.tillPanel.lblTotPrice.setText("200");
int currentValue = Integer.parseInt(addSelectedItem[1].trim());
addSelectedItem[1] = "200";
model.setValueAt(addSelectedItem[1], rowCount, 1);
break;
}
//if no duplicate found add this row to the table
else {
addSelectedItem[1] = "300";
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
main.tillPanel.lblTotPrice.setText("300");
break;
}
}

//clear the current selection array of row data
for (int index = 0; index < 4; index++) {
ddSelectedItem[index] = null;
}
}
});

最佳答案

查看代码,我强烈怀疑问题出在中断处,它们正在将 for 循环转变为 if-else 设置。它们是您只查看第一行的原因,我怀疑您实际上打算使用继续而不是中断(除了表为空的情况)。

假设 block 的内容是正确的,我认为这应该满足您的要求:

    if (model.getRowCount() == 0 ) {
//if table is empty, just add
addSelectedItem[1] = "100";
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
//main.tillPanel.tblTillSale.getModel().setValueAt(selectedItem, tillSelectedRow, tillSelectedRow);
main.tillPanel.lblTotPrice.setText("100");
}else {
//table not empty, look for duplicates first
boolean found = false;
for (int rowCount = 0 ; rowCount < model.getRowCount(); rowCount++) {
// look for duplicate row
if(addSelectedItem[0].equals(main.tillPanel.tblTillSale.getValueAt(rowCount, 0))) {
//found the item : increase total column of existing row
found = true;
main.tillPanel.lblTotPrice.setText("200");
int currentValue = Integer.parseInt(addSelectedItem[1].trim());
addSelectedItem[1] = "200";
model.setValueAt(addSelectedItem[1], rowCount, 1);
}else {//not this row
}
}
if( found == false) {
//checked all rows without finding it : add this selection
addSelectedItem[1] = "300";
model.addRow(new String[]{addSelectedItem[0], addSelectedItem[1], addSelectedItem[2], addSelectedItem[3]});
main.tillPanel.lblTotPrice.setText("300");
} else {
}
}

另外(不知道上下文),看看这些行:

int currentValue = Integer.parseInt(addSelectedItem[1].trim());addSelectedItem[1] = "200";

这意味着:

int currentValue = Integer.parseInt(addSelectedItem[1].trim());addSelectedItem[1] = ""+ (当前值+100);

此时,您解析的这个值将在 block 的末尾被丢弃。
另请注意:

公共(public)静态 int parseInt(String s) 抛出 NumberFormatException

您需要在任何解析中包含 try/catch block

关于java - 检查 jTable 数据中的重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5232022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com