Search code examples
javaswingnetbeans-8calculation

JFrame Auto Calculation


I have a problem with Text Field Auto Calculation in JAVA using Netbeans

My Question is if I will input numeric values in Text Field for auto addition and then input numeric values in Text Field to(fare and tax and comm%) where I will be getting to fields auto calculation so how I will get result of those numeric values in Text Field (Comm) and also (Cost Price)before clicking submit Button.

enter image description here

     try {   String sql = "insert into ticketing (Date,LPO,PassName,Route,AirlineCode,TicketNum,SellingPrice, Contact, Officer,Fare,Tax,comm%,comm,CostPrice,System,Remart)" + "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
        pst = conn.prepareStatement(sql);
        //pst.setInt(1, Integer.parseInt(id.getText()));


        pst.setString(1, Date.getText());

        pst.setString(2, LPO.getText());

        pst.setString(3, PassName.getText());

        pst.setString(4, Route.getText());

        pst.setString(5, AirCode.getText());

        pst.setString(6, TikNum.getText());

        pst.setString(7, SellPrice.getText());



        String Conta;
        Conta = Cont.getSelectedItem().toString();
        pst.setString (8,Conta);

        String Officer;
        Officer = Offic.getSelectedItem().toString();
        pst.setString (9,Officer);

        pst.setString(10, Fare.getText());


        pst.setString(11, Tax.getText());


        pst.setString(12, commper.getText());


        pst.setString(13, comm.getText());


        pst.setString(14, CostPrice.getText());


       String Sys;
        Sys = System.getSelectedItem().toString();
        pst.setString (15,Sys);


        pst.setString(16, Remark.getText());




        pst.executeUpdate();
        JOptionPane.showMessageDialog(null, "insertion successful");
        conn.close();

    }catch (SQLException e){
        JOptionPane.showMessageDialog(null, e);
    }

How can it be done.

Thanks ...........


Solution

  • i had to use DocumentFilter to solved I'm just sharing my code it might help someone in future and also some people search for knowledge

    DocumentFilter df = new DocumentFilter() {
            @Override
            public void insertString(DocumentFilter.FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
    
                if (isDigit(string)) {
                    super.insertString(fb, i, string, as);
                    calcAndSetTotal();
                }
            }
             @Override
            public void remove(DocumentFilter.FilterBypass fb, int i, int i1) throws BadLocationException {
                super.remove(fb, i, i1);
                calcAndSetTotal();
            }
    
            @Override
            public void replace(DocumentFilter.FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
                if (isDigit(string)) {
                    super.replace(fb, i, i1, string, as);
                    calcAndSetTotal();
    
                }
            }
    
            private boolean isDigit(String string) {
                for (int n = 0; n < string.length(); n++) {
                    char c = string.charAt(n);//get a single character of the string
                    //System.out.println(c);
                    if (!Character.isDigit(c)) {//if its an alphabetic character or white space
                        return false;
                    }
                }
                return true;
            }
    
            void calcAndSetTotal() {
                int sum = 0;
                int fr = 0;
                int pc = 0;
                int tax = 0;
                int total = 0;
    
                if (!Fare.getText().isEmpty()) {
                    fr= Integer.parseInt(Fare.getText());//we must add this
                }
                if (!Tax.getText().isEmpty()) {
                    tax= Integer.parseInt(Tax.getText());//we must add this
                }
                if (!commper.getText().isEmpty()) {
                    pc= Integer.parseInt(commper.getText());//we must subtract this
                }
                sum =(int) (fr *(pc*0.01));
    
                total = (int) (fr + tax - sum);
                comm.setText(String.valueOf(sum));
                CostPrice.setText(String.valueOf(total));
            }
        };