Java 在JTable中对相似项进行分组
我正在试着做一个POS系统。 我想让我的JTable在调整数量价格的同时将现有项目添加到同一行 例如:表中已存在产品1,我想添加相同的产品1,但数量不同。该附加产品1将其数量添加到表中已有的先前产品1中,然后调整产品1的价格Java 在JTable中对相似项进行分组,java,swing,jtable,Java,Swing,Jtable,我正在试着做一个POS系统。 我想让我的JTable在调整数量价格的同时将现有项目添加到同一行 例如:表中已存在产品1,我想添加相同的产品1,但数量不同。该附加产品1将其数量添加到表中已有的先前产品1中,然后调整产品1的价格 希望我说清楚 基于我对你问题的理解以及评论中的内容。我认为您基本上需要使用表模型操作JTable中的数据。() 逻辑是 使用“单击”按钮添加产品和数量时,您应该了解该产品是否已在表中 model.getValueAt(i,PRODUCT_COL)!=null&&model.
希望我说清楚 基于我对你问题的理解以及评论中的内容。我认为您基本上需要使用表模型操作JTable中的数据。() 逻辑是
public class POS extends JFrame {
String[] columns = {"Product", "Quantity"};
private final static int PRODUCT_COL = 0;
private final static int QUANTITY_COL = 1;
TableModel tableModel = new DefaultTableModel(columns, 0);
JTable table = new JTable(tableModel);
JComboBox comboBox = new JComboBox(new String[]{"Apple", "Banana"});
JTextField quantityJtf = new JTextField(10);
JButton jButton = new JButton("Add");
POS() {
this.setLayout(new FlowLayout());
this.add(new JScrollPane(table));
this.add(comboBox);
this.add(quantityJtf);
this.add(jButton);
jButton.addActionListener(new MyActionListener());
}
private class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String product = comboBox.getSelectedItem().toString();
int quantity = Integer.parseInt(quantityJtf.getText().trim());
DefaultTableModel model = (DefaultTableModel) table.getModel();
int rowCount = model.getRowCount();
int existingProductRowIndex = -1;
for (int i = 0; i < rowCount; ++i) {
if (model.getValueAt(i, PRODUCT_COL) != null && model.getValueAt(i, PRODUCT_COL).equals(product)) {
existingProductRowIndex = i;
break;
}
}
if (existingProductRowIndex > -1) {
int orignialQuantity = Integer.parseInt(model.getValueAt(existingProductRowIndex, QUANTITY_COL).toString());
model.setValueAt(orignialQuantity + quantity, existingProductRowIndex, QUANTITY_COL);
} else {
model.addRow(new Object[]{product, quantity});
}
}
公共类POS扩展JFrame{
字符串[]列={“产品”,“数量”};
私有最终静态整数乘积_COL=0;
专用最终静态整数数量=1;
TableModel TableModel=新的DefaultTableModel(列,0);
JTable table=新的JTable(tableModel);
JComboBox comboBox=newjcombobox(新字符串[]{“Apple”,“Banana”});
JTextField quantityJtf=新的JTextField(10);
JButton JButton=新JButton(“添加”);
POS(){
this.setLayout(新的FlowLayout());
添加(新的JScrollPane(表));
这个.add(组合框);
添加(数量JTF);
添加(jButton);
addActionListener(新的MyActionListener());
}
私有类MyActionListener实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
String product=comboBox.getSelectedItem().toString();
int quantity=Integer.parseInt(quantityJtf.getText().trim());
DefaultTableModel=(DefaultTableModel)table.getModel();
int rowCount=model.getRowCount();
int existingProductRowIndex=-1;
对于(int i=0;i-1){
int orignialQuantity=Integer.parseInt(model.getValueAt(existingProductRowIndex,QUANTITY\u COL.toString());
model.setValueAt(原始数量+数量,现有产品行索引,数量列);
}否则{
addRow(新对象[]{product,quantity});
}
}
我只在jtable中添加产品,但相同的产品不会相互添加,以至于会被相同的产品挤得满满的。例如,我为这个数量添加p1,然后添加另一个不同数量的p1,但相同的产品不会添加其数量以节省jtable中的空间。在表中插入新数据是如何工作的?它只是添加了产品名称,然后如果我添加了相同的产品,它会再次出现在表格中。我的意思是用户界面如何。你有对话框吗?还是直接在表格中输入?我有一个组合框、一个按钮、一个表格和一个文本框。首先我在文本框中输入一个整数,然后在组合框中选择一个产品框并点击按钮,然后它将出现在表中,这就是我的代码的工作方式。。