I create a demo above. JTable retrieves 'ID', 'Name' and 'Cost' from MySQL database. User can enter a quantity (Qty) and Discount price in 'Discount'.
I want to display total in'Total' when the user press the TAB key on keyboard to move into the next cell.
Total have to be (Cost*Qty)-Discount
How can I implement this?
I Found a Solution :
String sql = "select ItemID,ItemName,CostPrice from druginfo where ItemID=?";
try {
pst=conn.prepareStatement(sql);
pst.setString(1, temp);
rs=pst.executeQuery();
addDataToTable(tableSale,DbUtils.resultSetToTableModel(rs));
IDcombo.setSelectedItem(null);
Namecombo.setSelectedItem(null);
exptxt.setText(null);
instock.setText(null);
//getting user input for selling qty
String Qty=JOptionPane.showInputDialog("Insert Selling Quantity :");
double sellingqty=Double.parseDouble(Qty);
//setting qty input value to table sale
tableSale.getModel().setValueAt(sellingqty,i, 3);
//getting user input for specific item discount
String discount = JOptionPane.showInputDialog("Insert Item Discount");
double idiscount=Double.parseDouble(discount);
//setting input value to table sale
tableSale.getModel().setValueAt(idiscount,i, 4);
//getting item cost from the table sale
double icost =(double) tableSale.getModel().getValueAt(i,2);
//calculating Gross Total
double grosstotal = (sellingqty*icost)-idiscount;
//setting grosstotal value to table sale
tableSale.getModel().setValueAt(grosstotal,i, 5);
String invoice = InvoiceNo_txt.getText();
String id = (String) tableSale.getValueAt(i, 0);
String name = (String) tableSale.getValueAt(i, 1);
double dcost = (double) tableSale.getValueAt(i, 2);
String cost = String.valueOf(dcost);//converting double to string
double dqty = (double) tableSale.getValueAt(i, 3);
String qty = String.valueOf(dqty);//converting double to string
double ditemDiscount = (double) tableSale.getValueAt(i, 4);
String itemDiscount = String.valueOf(ditemDiscount);//converting double to string
double dgrossTotal = (double) tableSale.getValueAt(i, 5);
String grossTotal = String.valueOf(dgrossTotal);//converting double to string