gpt4 book ai didi

java - 如果在 JTable 中重复,则更新值

转载 作者:行者123 更新时间:2023-11-30 11:34:21 25 4
gpt4 key购买 nike

我在检查 JTable 中的重复值时遇到问题。用户输入“产品代码”和“金额”,应用程序正在使用“产品代码”查找项目名称并将其添加到表值中。我想检查 JTable 中的重复项,但我不知道该怎么做。我希望用户选择例如代码:55 和数量 3,然后它被添加到 JTable 但是如果用户再次输入 55 和数量 5,它会添加另一行,我想要的是更新数量 3 + 5。

这就是我到目前为止所做的。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class StockBasket extends JPanel implements ActionListener {
private JTable table;
private Vector rows, columns;
private DefaultTableModel tabModel;
private JScrollPane scrollPane;
private JButton addBtn, deleteBtn;
private JPanel mainPanel, buttonPanel;
private String productName;
private String spinnerAmount;
private int quantityDbInt;
private int amountInt;
public StockBasket() {
rows = new Vector();
columns = new Vector();
String[] columnNames = { "Amount to update", "Product name",
"Total in stock after update" };
addColumns(columnNames);

tabModel = new DefaultTableModel();
tabModel.setDataVector(rows, columns);

table = new JTable(tabModel);
table.changeSelection(0, 0, false, false);
scrollPane = new JScrollPane(table);// ScrollPane
scrollPane.setPreferredSize(new java.awt.Dimension(500, 200));

buttonPanel = new JPanel();
addBtn = new JButton("ADD TO UPDATE");
deleteBtn = new JButton("DELETE");
deleteBtn.setEnabled(false);

buttonPanel.add(addBtn);
buttonPanel.add(deleteBtn);

addBtn.addActionListener(this);
deleteBtn.addActionListener(this);

mainPanel = new JPanel();
mainPanel.setSize(450, 300);
mainPanel.setLayout(new BorderLayout());
mainPanel.add("Center", scrollPane);
mainPanel.add("North", buttonPanel);
add(mainPanel);
setVisible(true);
}

public void actionPerformed(ActionEvent source) {
if (source.getSource() == (JButton) addBtn) {
addBtnChecks();
} else if (source.getSource() == (JButton) deleteBtn) {
deleteRow(table.getSelectedRow());
}
}

private void addBtnChecks() {
productName = new MyDatabase(3).getName();
amountInt = Integer.parseInt(UpdateStock.getAmount().getValue()
.toString());
if (new MyDatabase(3).getQuantity() == null) {
// if the answer from db is null do not parseInt
} else {
quantityDbInt = Integer.parseInt(new MyDatabase(3).getQuantity());
}
if (productName == null) {
UpdateStock.getErrorMsg().setText("Enter correct code");

} else if (amountInt == 0) {
UpdateStock.getErrorMsg().setText("Please enter correct amount");

} else {
addRow();
UpdateStock.getAmount().setValue(0);
UpdateStock.getStockNo().setText("");
UpdateStock.getErrorMsg().setText("");
deleteBtn.setEnabled(true);
}
}

private void addColumns(String[] colName) {
for (int i = 0; i < colName.length; i++)
columns.addElement(colName[i]);
}

private void addRow() {
Vector r = new Vector();
r = createNewElement();
rows.addElement(r);
table.addNotify();
}

private Vector createNewElement() {
Vector t = new Vector();
spinnerAmount = UpdateStock.getAmount().getValue().toString();
int spinnerAmountInt = Integer.parseInt(spinnerAmount);
// read product name from database
productName = new MyDatabase(3).getName();
// itemsInStock - get value from db and add amount added by user
int itemsInStock = quantityDbInt + spinnerAmountInt;
t.addElement(spinnerAmount);
t.addElement(productName);
t.addElement(itemsInStock);
return t;
}

private void deleteRow(int index) {
// makes the first row always selected
table.changeSelection(0, 0, false, false);
int size = tabModel.getRowCount();
if (size == 1) { // can't delete when no items
deleteBtn.setEnabled(false);
}
if (index != -1) { // At least one Row in Table
rows.removeElementAt(index);
table.addNotify();
}
}

}

最佳答案

有很多方法可以做到这一点。最好的方法涉及数据模型(带有数量的股票列表)和表模型(以 JTable 理解的方式表达它的适配器)之间的清晰分离。

List<StockHolding> 开头其中 StockHolding是一个 super 简单的类,用于保存一行数据。

public class StockHolding {
private String product;
private int quantity;
}

(在构造函数中添加和getProduct/getQuantity)

现在当您输入新产品+金额...

addRequest(String product, int amount) {
StockHolding holding = findInList(product, myList);
if (holding == null) {
myList.add(new StockHolding(product, amount));
} else {
holding.setQuantity(amount+holding.getQuantity());
}

完成这项工作的关键是根据此列表来表达您的表模型。 getRowCount()返回 myList.size()单元格值为 getValue(myList.get(rowNum), columnNum)其中 getValue()将列号分别映射到产品或金额。

这里的重要一课是,将表表达问题与存储数据问题分开可以使每个问题更容易解决。

关于java - 如果在 JTable 中重复,则更新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15720568/

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