I created JTable with Five columns. Name, Unit, Price, Quantity & Amount are Column Names. Data Filled to Table Using JText Fields. It works properly when click 'ADD' Button. I wrote a code to get 'Amount' using 'Price' column Cell value Multiply by 'Quantity' column Cell value. Program runs but calculation not happened. Help me to solve this problem is highly appreciate. Thanks in advance.
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);
}
}
}
}
Actually, the calculation does happen. You can't see the update, because an Exception is thrown - because you're trying to get a Double from TableModel, which contains Strings. It will start to work, when you modify your tableChanged model value extraction to:
double price = Double.parseDouble(model.getValueAt(row, 2).toString());
double quantity = Double.parseDouble(model.getValueAt(row, 3).toString());
But as a goal, this should probably be done in a separate method, with validation if the value is null, or contains something else than a double.