Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9075503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:52:23+00:00 2026-06-16T18:52:23+00:00

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

  • 0

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

My Question is if I will input numeric values in Text Field i-e (admission fee, monthly fee, transport fee etc) for auto addition and then input numeric values in Text Field i-e (dues) to auto subtract from the above auto addition before clicking submit Button to insert the total values in database so how i will get result of those numeric values in Text Field (Total) before clicking submit Button.

Please check snapshot:

image

My Source code:

try
         {

            String insrt = "Insert into fee (admission, monthly, transport, dues, total) values (?, ?, ?, ?, ?)";

            PreparedStatement pstmt = conn.prepareStatement(insrt);

            pstmt.setString(1, adm_fee.getText());
            pstmt.setString(2, mnth_fee.getText());
            pstmt.setString(3, trnsprt_fee.getText());
            pstmt.setString(4, dues_fee.getText());
            pstmt.setString(5, total_fee.getText());
            pstmt.executeUpdate();

            JOptionPane.showMessageDialog(null,"Record successfully inserted");
        }

        catch (Exception exp)
        {
            JOptionPane.showMessageDialog(null, exp);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:52:24+00:00Added an answer on June 16, 2026 at 6:52 pm

    I suggest use a DocumentFilter this will allow us to kill 2 birds with 1 stone.

    1) we need to filter what is inputted to JTextFields to make sure our calculation wont go wrong

    2) We need to update the total on the fly i.e as more digits are added/removed.

    Here is an example I made which uses DocumentFilter and as you will see the Total field will be updated each time a new digit is entered/added to the JTextField(s) (also it wont allow alphabetic characters etc only digits):

    enter image description here

    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.text.AbstractDocument;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DocumentFilter;
    import javax.swing.text.DocumentFilter.FilterBypass;
    
    public class DocumentFilterOnTheFlyCalculation {
    
        public DocumentFilterOnTheFlyCalculation() {
            createAndShowGui();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new DocumentFilterOnTheFlyCalculation();
                }
            });
        }
    
        private void createAndShowGui() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new GridLayout(4, 2));
    
            JLabel label1 = new JLabel("Add:");
            final JTextField jtf1 = new JTextField();
    
            JLabel label2 = new JLabel("Add:");
            final JTextField jtf2 = new JTextField();
    
            JLabel label3 = new JLabel("Subtract:");
            final JTextField jtf3 = new JTextField();
    
            JLabel totalLabel = new JLabel("Total:");
            final JTextField totalField = new JTextField("0");
            totalField.setEditable(false);
    
            DocumentFilter df = new DocumentFilter() {
                @Override
                public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
    
                    if (isDigit(string)) {
                        super.insertString(fb, i, string, as);
                        calcAndSetTotal();
                    }
                }
    
                @Override
                public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
                    super.remove(fb, i, i1);
                    calcAndSetTotal();
                }
    
                @Override
                public void replace(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;
    
                    if (!jtf1.getText().isEmpty()) {
                        sum += Integer.parseInt(jtf1.getText());//we must add this
                    }
                    if (!jtf2.getText().isEmpty()) {
                        sum += Integer.parseInt(jtf2.getText());//we must add this
                    }
                    if (!jtf3.getText().isEmpty()) {
                        sum -= Integer.parseInt(jtf3.getText());//we must subtract this
                    }
    
                    totalField.setText(String.valueOf(sum));
                }
            };
    
            ((AbstractDocument) (jtf1.getDocument())).setDocumentFilter(df);
            ((AbstractDocument) (jtf2.getDocument())).setDocumentFilter(df);
            ((AbstractDocument) (jtf3.getDocument())).setDocumentFilter(df);
    
            frame.add(label1);
            frame.add(jtf1);
            frame.add(label2);
            frame.add(jtf2);
            frame.add(label3);
            frame.add(jtf3);
            frame.add(totalLabel);
            frame.add(totalField);
    
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear community members, I have a small problem, which involves a text input field
I want to place UItextField in UITableViewCell but I have problem. Text field don't
I have a problem with datePicker. When a make a new text field with
I have a litle problem with my text encoding, when user filed textarea field
I've the following problem. I used to have an input with auto-completion, I removed
I have problem with TinyMCE editor. I have form with few text fields and
I have the following problem: I got a view with two text fields and
I have problem with text drawing around Circle. I found great sample in C#
people. I have slight problem with GD2 text on image. I have everything working
I have a simple problem while putting text in multiple table! please have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.