将数据添加到 'Price' 和 'Quantity' 列单元格时更新 JTable 中的 'Amount' 列单元格
Update 'Amount' Column cells in JTable when adding data to 'Price' & 'Quantity' Column Cells
我创建了包含五列的 JTable。名称、单位、价格、数量和金额是列名。
使用 JText 字段将数据填充到 Table。单击 'ADD' 按钮时,它可以正常工作。
我写了一个代码来获得 'Amount' 使用 'Price' 列单元格值乘以 'Quantity' 列单元格值。程序运行但未进行计算。非常感谢帮助我解决这个问题。提前致谢。
View-01
public class UnitTable2 extends JFrame implements TableModelListener {
private JFrame frame;
private JTable table;
private JTextField txtName;
private JTextField txtUImp;
private JTextField txtPImp;
private JTextField txtUMetric;
private JTextField txtPMetric;
private JTextField txtQty;
private JComboBox<String> cmbUType;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UnitTable2 window = new UnitTable2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public UnitTable2() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 649, 288);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 526, 181);
frame.getContentPane().add(scrollPane);
table = new JTable();
Object[] columns = { "Name", "Unit", "Price", "Quantity", "Amount" };
DefaultTableModel model = new DefaultTableModel();
scrollPane.setViewportView(table);
model.setColumnIdentifiers(columns);
table.setModel(model);
model.addTableModelListener(this);
JLabel lblName = new JLabel("Name");
lblName.setBounds(10, 201, 96, 13);
frame.getContentPane().add(lblName);
txtName = new JTextField();
txtName.setBounds(10, 224, 96, 19);
frame.getContentPane().add(txtName);
txtName.setColumns(10);
JLabel lblUImp = new JLabel("Unit Imperial");
lblUImp.setBounds(121, 201, 91, 13);
frame.getContentPane().add(lblUImp);
txtUImp = new JTextField();
txtUImp.setBounds(116, 224, 96, 19);
frame.getContentPane().add(txtUImp);
txtUImp.setColumns(10);
JLabel lblPImp = new JLabel("Price Imperial");
lblPImp.setBounds(222, 201, 96, 13);
frame.getContentPane().add(lblPImp);
txtPImp = new JTextField();
txtPImp.setBounds(222, 224, 96, 19);
frame.getContentPane().add(txtPImp);
txtPImp.setColumns(10);
JLabel lblUMetric = new JLabel("Unit Metric");
lblUMetric.setBounds(330, 201, 94, 13);
frame.getContentPane().add(lblUMetric);
txtUMetric = new JTextField();
txtUMetric.setBounds(330, 224, 96, 19);
frame.getContentPane().add(txtUMetric);
txtUMetric.setColumns(10);
JLabel lblPMetric = new JLabel("Price Metric");
lblPMetric.setBounds(434, 201, 102, 13);
frame.getContentPane().add(lblPMetric);
txtPMetric = new JTextField();
txtPMetric.setBounds(434, 224, 96, 19);
frame.getContentPane().add(txtPMetric);
txtPMetric.setColumns(10);
JLabel lblQty = new JLabel("Quantity");
lblQty.setBounds(540, 201, 120, 13);
frame.getContentPane().add(lblQty);
txtQty = new JTextField();
txtQty.setBounds(540, 224, 96, 19);
frame.getContentPane().add(txtQty);
txtQty.setColumns(10);
JButton btnAdd = new JButton("ADD");
Object[] row = new Object[4];
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String type = (String) cmbUType.getSelectedItem();
if (type.equals("Imperial")) {
row[0] = txtName.getText();
row[1] = txtUImp.getText();
row[2] = Double.parseDouble(txtPImp.getText());
row[3] = Double.parseDouble(txtQty.getText());
} else {
row[0] = txtName.getText();
row[1] = txtUMetric.getText();
row[2] = Double.parseDouble(txtPMetric.getText());
row[3] = Double.parseDouble(txtQty.getText());
}
model.addRow(row);
}
});
btnAdd.setBounds(546, 45, 85, 21);
frame.getContentPane().add(btnAdd);
cmbUType = new JComboBox<>();
cmbUType.setModel(new DefaultComboBoxModel<String>(new String[] { "Imperial", "Metric" }));
cmbUType.setBounds(546, 8, 85, 21);
frame.getContentPane().add(cmbUType);
JButton btnDelete = new JButton("DELETE");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = table.getSelectedRow();
if (i >= 0) {
model.removeRow(i);
} else {
JOptionPane.showMessageDialog(null, "Please Select Item to Delete");
}
}
});
btnDelete.setBounds(546, 76, 85, 21);
frame.getContentPane().add(btnDelete);
}
// price multiply with quantity and set to amount
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE) {
int row = e.getFirstRow();
int column = e.getColumn();
if (column == 2 || column == 3) {
TableModel model = table.getModel();
double price = ((Double) model.getValueAt(row, 2)).doubleValue();
double quantity = ((Double) model.getValueAt(row, 3)).doubleValue();
Double value = (price * quantity);
model.setValueAt(value, row, 4);
}
}
}
}
实际上,计算 确实 发生了。您看不到更新,因为抛出了异常 - 因为您正试图从包含字符串的 TableModel 获取 Double。当您将 tableChanged 模型值提取修改为:
时,它将开始工作
double price = Double.parseDouble(model.getValueAt(row, 2).toString());
double quantity = Double.parseDouble(model.getValueAt(row, 3).toString());
但作为一个目标,这可能应该在一个单独的方法中完成,如果值为 null 或包含除双精度以外的其他内容,则进行验证。
我创建了包含五列的 JTable。名称、单位、价格、数量和金额是列名。 使用 JText 字段将数据填充到 Table。单击 'ADD' 按钮时,它可以正常工作。 我写了一个代码来获得 'Amount' 使用 'Price' 列单元格值乘以 'Quantity' 列单元格值。程序运行但未进行计算。非常感谢帮助我解决这个问题。提前致谢。
View-01
public class UnitTable2 extends JFrame implements TableModelListener {
private JFrame frame;
private JTable table;
private JTextField txtName;
private JTextField txtUImp;
private JTextField txtPImp;
private JTextField txtUMetric;
private JTextField txtPMetric;
private JTextField txtQty;
private JComboBox<String> cmbUType;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UnitTable2 window = new UnitTable2();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public UnitTable2() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 649, 288);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 526, 181);
frame.getContentPane().add(scrollPane);
table = new JTable();
Object[] columns = { "Name", "Unit", "Price", "Quantity", "Amount" };
DefaultTableModel model = new DefaultTableModel();
scrollPane.setViewportView(table);
model.setColumnIdentifiers(columns);
table.setModel(model);
model.addTableModelListener(this);
JLabel lblName = new JLabel("Name");
lblName.setBounds(10, 201, 96, 13);
frame.getContentPane().add(lblName);
txtName = new JTextField();
txtName.setBounds(10, 224, 96, 19);
frame.getContentPane().add(txtName);
txtName.setColumns(10);
JLabel lblUImp = new JLabel("Unit Imperial");
lblUImp.setBounds(121, 201, 91, 13);
frame.getContentPane().add(lblUImp);
txtUImp = new JTextField();
txtUImp.setBounds(116, 224, 96, 19);
frame.getContentPane().add(txtUImp);
txtUImp.setColumns(10);
JLabel lblPImp = new JLabel("Price Imperial");
lblPImp.setBounds(222, 201, 96, 13);
frame.getContentPane().add(lblPImp);
txtPImp = new JTextField();
txtPImp.setBounds(222, 224, 96, 19);
frame.getContentPane().add(txtPImp);
txtPImp.setColumns(10);
JLabel lblUMetric = new JLabel("Unit Metric");
lblUMetric.setBounds(330, 201, 94, 13);
frame.getContentPane().add(lblUMetric);
txtUMetric = new JTextField();
txtUMetric.setBounds(330, 224, 96, 19);
frame.getContentPane().add(txtUMetric);
txtUMetric.setColumns(10);
JLabel lblPMetric = new JLabel("Price Metric");
lblPMetric.setBounds(434, 201, 102, 13);
frame.getContentPane().add(lblPMetric);
txtPMetric = new JTextField();
txtPMetric.setBounds(434, 224, 96, 19);
frame.getContentPane().add(txtPMetric);
txtPMetric.setColumns(10);
JLabel lblQty = new JLabel("Quantity");
lblQty.setBounds(540, 201, 120, 13);
frame.getContentPane().add(lblQty);
txtQty = new JTextField();
txtQty.setBounds(540, 224, 96, 19);
frame.getContentPane().add(txtQty);
txtQty.setColumns(10);
JButton btnAdd = new JButton("ADD");
Object[] row = new Object[4];
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String type = (String) cmbUType.getSelectedItem();
if (type.equals("Imperial")) {
row[0] = txtName.getText();
row[1] = txtUImp.getText();
row[2] = Double.parseDouble(txtPImp.getText());
row[3] = Double.parseDouble(txtQty.getText());
} else {
row[0] = txtName.getText();
row[1] = txtUMetric.getText();
row[2] = Double.parseDouble(txtPMetric.getText());
row[3] = Double.parseDouble(txtQty.getText());
}
model.addRow(row);
}
});
btnAdd.setBounds(546, 45, 85, 21);
frame.getContentPane().add(btnAdd);
cmbUType = new JComboBox<>();
cmbUType.setModel(new DefaultComboBoxModel<String>(new String[] { "Imperial", "Metric" }));
cmbUType.setBounds(546, 8, 85, 21);
frame.getContentPane().add(cmbUType);
JButton btnDelete = new JButton("DELETE");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int i = table.getSelectedRow();
if (i >= 0) {
model.removeRow(i);
} else {
JOptionPane.showMessageDialog(null, "Please Select Item to Delete");
}
}
});
btnDelete.setBounds(546, 76, 85, 21);
frame.getContentPane().add(btnDelete);
}
// price multiply with quantity and set to amount
public void tableChanged(TableModelEvent e) {
if (e.getType() == TableModelEvent.UPDATE) {
int row = e.getFirstRow();
int column = e.getColumn();
if (column == 2 || column == 3) {
TableModel model = table.getModel();
double price = ((Double) model.getValueAt(row, 2)).doubleValue();
double quantity = ((Double) model.getValueAt(row, 3)).doubleValue();
Double value = (price * quantity);
model.setValueAt(value, row, 4);
}
}
}
}
实际上,计算 确实 发生了。您看不到更新,因为抛出了异常 - 因为您正试图从包含字符串的 TableModel 获取 Double。当您将 tableChanged 模型值提取修改为:
时,它将开始工作double price = Double.parseDouble(model.getValueAt(row, 2).toString());
double quantity = Double.parseDouble(model.getValueAt(row, 3).toString());
但作为一个目标,这可能应该在一个单独的方法中完成,如果值为 null 或包含除双精度以外的其他内容,则进行验证。