当从 Java 中的 JComboBox 中选择一个项目时,我想在 JTable 中添加数据?
I want to add Data in JTable when an item is selected from JComboBox in Java?
我制作了一个 JComboBOx,其中展示了我销售的产品。当用户单击我在数组列表中搜索的项目并返回一个对象时,我想从该对象中将信息放入二维数组中,然后我想将该二维数组添加到 JTable 的行中,但我是在 table 中没有得到任何东西。
谁能帮助我如何在从 JComboBox 选择项目时在 JTable 中添加项目?
public void actionPerformed(ActionEvent arg0) {
String boughtThing = InventoryList.getSelectedItem().toString();
int NumberOfItems = Integer.parseInt(JOptionPane.showInputDialog("Enter the Number Item"));
ImplementInventoryServices service = new ImplementInventoryServices();
Inventory thing = service.searchInventory(boughtThing);
double price = thing.getPricePerUnit();
String nameOfProduct = thing.getInventoryName();
int stock = thing.getNumberOfInventory();
int IDofProduct = thing.getInventoryID();
subtotal = 0;
subtotal = price * NumberOfItems;
stock = stock - NumberOfItems;
data[0][0] = Double.toString(price);
data[0][1] = nameOfProduct;
data[0][2] = Integer.toString(NumberOfItems);
data[0][3] = Double.toString(subtotal);
info = new Information(price, nameOfProduct, IDofProduct, subtotal);
Inventory inventory = new Inventory(nameOfProduct, IDofProduct, price, stock);
ImplementInventoryServices updating = new ImplementInventoryServices();
updating.updateInventory(nameOfProduct, inventory);
subTotalList.add(subtotal);
DataHandlingForInventory.write();
}
});
String [] columns = {"ID","Name","Price Per Unit", "Sub Total"};
OrignalTable = new JTable(data,columns);
OrignalTable.setBounds(10, 119, 425, 219);
InvoiceMake.getContentPane().add(OrignalTable);
这样试试:
创建一个 DefaultTableModel
来保存您的数据,并 link 将其发送到 table:
Object [] columns = {"ID","Name","Price Per Unit", "Sub Total"};
tableModel = new DefaultTableModel(columns, 0);
OrignalTable = new JTable(tableModel);
现在您可以使用该模型(定义为 class 中的一个字段,因此您可以在匿名 ActionListener 中访问)处理数据:
// before
// data[0][0] = Double.toString(price);
// data[0][1] = nameOfProduct;
// data[0][2] = Integer.toString(NumberOfItems);
// data[0][3] = Double.toString(subtotal);
// after
tableModel.addRow(new Object[]{Double.toString(price),
nameOfProduct,
Integer.toString(NumberOfItems),
Double.toString(subtotal)});
请务必阅读 How to Use Tables
上的文档
我制作了一个 JComboBOx,其中展示了我销售的产品。当用户单击我在数组列表中搜索的项目并返回一个对象时,我想从该对象中将信息放入二维数组中,然后我想将该二维数组添加到 JTable 的行中,但我是在 table 中没有得到任何东西。 谁能帮助我如何在从 JComboBox 选择项目时在 JTable 中添加项目?
public void actionPerformed(ActionEvent arg0) {
String boughtThing = InventoryList.getSelectedItem().toString();
int NumberOfItems = Integer.parseInt(JOptionPane.showInputDialog("Enter the Number Item"));
ImplementInventoryServices service = new ImplementInventoryServices();
Inventory thing = service.searchInventory(boughtThing);
double price = thing.getPricePerUnit();
String nameOfProduct = thing.getInventoryName();
int stock = thing.getNumberOfInventory();
int IDofProduct = thing.getInventoryID();
subtotal = 0;
subtotal = price * NumberOfItems;
stock = stock - NumberOfItems;
data[0][0] = Double.toString(price);
data[0][1] = nameOfProduct;
data[0][2] = Integer.toString(NumberOfItems);
data[0][3] = Double.toString(subtotal);
info = new Information(price, nameOfProduct, IDofProduct, subtotal);
Inventory inventory = new Inventory(nameOfProduct, IDofProduct, price, stock);
ImplementInventoryServices updating = new ImplementInventoryServices();
updating.updateInventory(nameOfProduct, inventory);
subTotalList.add(subtotal);
DataHandlingForInventory.write();
}
});
String [] columns = {"ID","Name","Price Per Unit", "Sub Total"};
OrignalTable = new JTable(data,columns);
OrignalTable.setBounds(10, 119, 425, 219);
InvoiceMake.getContentPane().add(OrignalTable);
这样试试:
创建一个 DefaultTableModel
来保存您的数据,并 link 将其发送到 table:
Object [] columns = {"ID","Name","Price Per Unit", "Sub Total"};
tableModel = new DefaultTableModel(columns, 0);
OrignalTable = new JTable(tableModel);
现在您可以使用该模型(定义为 class 中的一个字段,因此您可以在匿名 ActionListener 中访问)处理数据:
// before
// data[0][0] = Double.toString(price);
// data[0][1] = nameOfProduct;
// data[0][2] = Integer.toString(NumberOfItems);
// data[0][3] = Double.toString(subtotal);
// after
tableModel.addRow(new Object[]{Double.toString(price),
nameOfProduct,
Integer.toString(NumberOfItems),
Double.toString(subtotal)});
请务必阅读 How to Use Tables
上的文档