Reputation: 8276
I've got a problem with checking for duplicate values in JTable
. User enters a 'product code' and an 'amount', app is looking for the item name using 'product code' and adds to the table values. I want to check for duplicates in JTable
, but I don't know how to do it. I want the user to choose for example code: 55 and amount 3, then it's added to JTable
but if the user enters again 55 and amount 5, it is adding another row, what I want is to update the amount 3 + 5.
That's what I did so far.
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();
}
}
}
Upvotes: 2
Views: 1108
Reputation: 3225
There are a lot of ways to do this. The best ones involve a clean separation between the data model (a list of stocks with amounts) and the table model (an adapter to express that in a way JTable understands).
Start with a List<StockHolding>
where StockHolding
is a super-simple class you make to hold the data for one row.
public class StockHolding {
private String product;
private int quantity;
}
(add in the constructor and getProduct/getQuantity)
Now when you enter a new product+amount...
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());
}
The key to making this work is to express your table model in terms of this list. getRowCount()
returns myList.size()
and the cell values are getValue(myList.get(rowNum), columnNum)
where getValue()
maps the column number to product or amount respectively.
This important lesson here is that isolating the express-as-a-table problem from the store-data problem make each problem easier to solve.
Upvotes: 1