2

enter image description here

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
tenten
  • 1,276
  • 2
  • 26
  • 54
  • 1
    What have you tried? You shouldn't particularly be looking for working answers but stepping stones for you to learn that. Do you have a code snippet of things you have tried but that are not working? – basic Jun 26 '15 at 15:50
  • I Implemented a method using Input dialog :) – tenten Jul 28 '15 at 10:28

2 Answers2

1

Take a look here how they override TableModelListener#tableChanged method. If you can not find a solution for tab then check it tableChanged event handler does the same job, inside the method probably you will set a new value in total cell.

Community
  • 1
  • 1
hrgiger
  • 11
  • 2
1

You can try looking for a TableModelListener...Add it to your TableModel.In TableModelListener check for update cell and then check for column no which was updated.If Discount column is updated get the value of three columns you need calculate the value and put it the total column. You can refer here Try to do it its very simple or else if in a hurry.Checkout the below code

     table.getModel().addTableModelListener( new TableModelListener() {

        @Override
        public void tableChanged(TableModelEvent e) {
            if (e.getType() == TableModelEvent.UPDATE) {

                int row = e.getFirstRow();
                int column = e.getColumn();
                if (column == 4 ) {
                    TableModel model = table.getModel();

                   int cost =  (model.getValueAt(row, 2)==null)?0:((Integer)model.getValueAt(row, 2)).intValue();
                  int quantity =(model.getValueAt(row, 3)==null)?0: ((Integer) model.getValueAt(row, 3)).intValue();
                  int discount = (model.getValueAt(row, 4)==null)?0:((Integer) model.getValueAt(row, 4)).intValue();

                    Integer total = new Integer((quantity * price)-discount);
                    model.setValueAt(total, row, 5);
                }
            }
        }
    };
Sarfaraz Khan
  • 2,166
  • 2
  • 14
  • 29
  • I tried that code in `private void tableSaleKeyPressed(java.awt.event.KeyEvent evt)`(because i want to navigate in table cells by TAB key) But not working.. @Sarfaraz Khan – tenten Jun 27 '15 at 15:07
  • What is this **tableModelListener** here `table.getModel().addTableModelListener(tableModelListener);` Where to declare it?.@Sarfaraz Khan – tenten Jun 27 '15 at 15:08
  • @tenten Sorry my mistake that line should not be there,I have removed it check – Sarfaraz Khan Jun 27 '15 at 15:30
  • When I run this... `java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double` shows as runtime error.. quantity is use to calculate total and total is a double.. how to fix it.. i tried but not working..@Sarfaraz Khan – tenten Jun 29 '15 at 04:31
  • @tenten I guess instead of using double you are using Integer in cost, quantity and discount field.So changing the Double field to integer will solve the issue.I have update have a look. However check it with you Table model what data type are you using for cost,quantity , discount and total field.If all are Integer then its ok otherwise change it accordingly – Sarfaraz Khan Jun 29 '15 at 04:37
  • cost is rupees in the database table so can't be a int.. :( also discount is in rupees.. :( @Sarfaraz Khan quantity is a int of course – tenten Jun 29 '15 at 04:47
  • @tenten try with Integer the above update that I have made if it gives error paste the error message and mention the line. – Sarfaraz Khan Jun 29 '15 at 04:54
  • I changed into int even in cost in my database (cost is retrives fromthe database) but shows `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException` @Sarfaraz Khan – tenten Jun 29 '15 at 04:57
  • Added null checking in the solution @tenten – Sarfaraz Khan Jun 29 '15 at 05:11
  • worked.. code is error free but not shows the total in total column... I coded inside above code in ` private void tableSaleKeyPressed(java.awt.event.KeyEvent evt)` in events in jtable.. Is it correct or wrong? @Sarfaraz Khan – tenten Jun 29 '15 at 09:30
  • Inside this tableSaleKeyPressed(java.awt.event.KeyEvent evt) check if its tan element and check the column of table then update the table with the code above. – Sarfaraz Khan Jun 29 '15 at 10:01
  • didn't get that..what you mean :( @Sarfaraz Khan – tenten Jun 29 '15 at 10:06
  • inside this method `private void tableSaleKeyPressed(java.awt.event.KeyEvent evt)` change this checking `if (e.getType() == TableModelEvent.UPDATE) ` to check if tab key is pressed and currently the focus is on total column – Sarfaraz Khan Jun 29 '15 at 10:41
  • I get the problem but unable to fix it [here my code](http://pastebin.com/NRHTfkx6)@Sarfaraz Khan – tenten Jun 29 '15 at 12:02
  • @tenten don't put it there..add this where you are creating table object tableSale.getModel().addTableModelListener – Sarfaraz Khan Jun 30 '15 at 05:01
  • stiil not working - [my code](http://pastebin.com/DieXncgc) – tenten Jul 07 '15 at 03:56
  • @tenten can you post your minimal running code in pastebin? – Sarfaraz Khan Jul 07 '15 at 06:14
  • [here](http://pastebin.com/Z6XskfDc) @Sarfaraz Khan – tenten Jul 07 '15 at 06:32