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

  • SEARCH
  • Home
  • 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 8360437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:28:04+00:00 2026-06-09T11:28:04+00:00

I have been working on a personal project to get better with programming. My

  • 0

enter image description hereI have been working on a personal project to get better with programming. My goal is to make it much more robust, I am just starting. I am a current computer science student. Anyway, I am working on making a portion of the program as shown. I calculates the hourly wage and provides some outputs I havent implemented yet. I’m using DocumentListener so it it will automatically calculate. I am getting an error when the the text is removed completely from a box.I tried to fix it with the if statement:

 if (tipMon.equals("") || tipMon == null) {
 tipMon.setText("0");
 }

Here is what I have so far. It’s not done yet and I apologize for the noob code. I started 2 months ago with actual coding.

 import java.awt.GridLayout;
 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;
 import java.awt.event.KeyListener;

 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JTextField;
 import javax.swing.JOptionPane;
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
 import javax.swing.text.Document;
 import javax.swing.text.FieldView;

 public class deliveryDocListener extends JFrame implements ActionListener, 
DocumentListener{

private JLabel mon, tues, wed, thurs, fri, sat, sun, hourlyWage, blank, row2, monWage,
    tuesWage,wedWage,thursWage, friWage, satWage, sunWage, total, totalTips, totalHours, 
    totalHourlyEarnings, totalPay, weekPay;

private JTextField hourlyWageInput, tipMon, tipTues, tipWed, tipThurs, tipFri, tipSat, tipSun,
    hourMon, hourTues, hourWed, hourThurs, hourFri, hourSat, hourSun;


public deliveryDocListener(){
    super("Delivery Helper v0.1 Alpha");
    setLayout(new GridLayout(0,4));

    hourlyWage = new JLabel("Hourly Wage: ");
    add(hourlyWage);
    hourlyWageInput = new JTextField("7.25", 5);

    add(hourlyWageInput);
    blank = new JLabel();
    add(blank);
    blank = new JLabel();
    add(blank);

    row2 = new JLabel("Day of the Week");
    add(row2);
    row2 = new JLabel("Tips");
    add(row2);
    row2 = new JLabel("Hours Worked");
    add(row2);
    row2 = new JLabel("Hourly Earnings");
    add(row2);

    mon = new JLabel("Monday");
    add(mon);



    tipMon = new JTextField("0");

    Document tipMonListener = tipMon.getDocument();
    //Document class doc variable stores what happens in the getDocument()
    //method, getDocument() i think is what checked it real time we shall see
    tipMonListener.addDocumentListener(this);
    //add listener to he text field, this refers to most recent object (tipMon = new JTextField("0");"
    //notice how its purple is the same as new where the object got made?
    add(tipMon);

    hourMon = new JTextField("0");
    Document hourMonListener = hourMon.getDocument();
    hourMonListener.addDocumentListener(this);
    add(hourMon);


    monWage = new JLabel("0");
    add(monWage);

    tues = new JLabel("Tuesday");
    add(tues);
    tipTues = new JTextField("0");
    add(tipTues);
    hourTues = new JTextField("0");
    add(hourTues);
    tuesWage = new JLabel("0");
    add(tuesWage);

    wed = new JLabel("Wednesday");
    add(wed);
    tipWed = new JTextField("0");
    add(tipWed);
    hourWed = new JTextField("0");
    add(hourWed);
    wedWage = new JLabel("0");
    add(wedWage);

    thurs = new JLabel("Thursday");
    add(thurs);
    tipThurs = new JTextField("0");
    add(tipThurs);
    hourThurs = new JTextField("0");
    add(hourThurs);
    thursWage = new JLabel("0");
    add(thursWage);

    fri = new JLabel("Friday");
    add(fri);
    tipFri = new JTextField("0");
    add(tipFri);
    hourFri = new JTextField("0");
    add(hourFri);
    friWage = new JLabel("0");
    add(friWage);

    sat = new JLabel("Saturday");
    add(sat);
    tipSat = new JTextField("0");
    add(tipSat);
    hourSat = new JTextField("0");
    add(hourSat);
    satWage = new JLabel("0");
    add(satWage);

    sun = new JLabel("Sunday");
    add(sun);
    tipSun = new JTextField("0");
    add(tipSun);
    hourSun = new JTextField("0");
    add(hourSun);
    sunWage = new JLabel("0");
    add(sunWage);

    blank = new JLabel();
    add(blank);
    blank = new JLabel();
    add(blank);
    blank = new JLabel();
    add(blank);
    blank = new JLabel();
    add(blank);

    total = new JLabel("Total: ");
    add(total);
    totalTips = new JLabel("totalTipsOutput");
    add(totalTips);
    totalHours = new JLabel("totalHoursOutput");
    add(totalHours);
    totalHourlyEarnings = new JLabel("totalHourlyEarningsOutput");
    add(totalHourlyEarnings);

    blank = new JLabel();
    add(blank);
    blank = new JLabel();
    add(blank);
    blank = new JLabel();
    add(blank);
    blank = new JLabel();
    add(blank);

    blank = new JLabel();
    add(blank);
    blank = new JLabel();
    add(blank);
    totalPay = new JLabel("Gross Income: ");
    add(totalPay);
    weekPay = new JLabel("totalPayOutput");
    add(weekPay);

}

@Override
public void changedUpdate(DocumentEvent e) { 
    // TODO Auto-generated method stub

}

@Override
public void insertUpdate(DocumentEvent e) {
    //executes when someone enters text into input
    String tipMonStr = tipMon.getText();
    //monWage.setText(tipMonStr);
    String hourMonStr = hourMon.getText();

    double x = Double.parseDouble(tipMonStr);
    double y = Double.parseDouble(hourMonStr);
    double z = Double.parseDouble(hourlyWageInput.getText());

    if (tipMonStr.length() == 0) {
        tipMon.setText("0");
    }

    if (hourMonStr.length() == 0) {
        y = 0;
        hourMonStr = "0";
    }

    if (hourlyWageInput.getText().length() == 0) {
        z = 0;
        //String z = "0";
    }

    monWage.setText(Double.toString((z*y+x)/y));

    //bug when nothing in cell because no number (0) to use in math
}

@Override
public void removeUpdate(DocumentEvent e) {
    //executes when someone enters text into input
    String tipMonStr = tipMon.getText();
    //monWage.setText(tipMonStr);
    String hourMonStr = hourMon.getText();

    double x = Double.parseDouble(tipMonStr);
    double y = Double.parseDouble(hourMonStr);
    double z = Double.parseDouble(hourlyWageInput.getText());
    monWage.setText(Double.toString((z*y+x)/y));

    if (tipMon.equals("") || tipMon == null) {
        tipMon.setText("0");
    }

}


public void updateLog(DocumentEvent e, String action) {
    monWage.setText(Double.toString(5));
}

@Override
public void actionPerformed(ActionEvent arg0) {
    monWage.setText(Double.toString(5));

}

}

  • 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-09T11:28:06+00:00Added an answer on June 9, 2026 at 11:28 am

    I’ll make this an answer: I wouldn’t use a DocumentListener for this purpose as it seems to me the wrong tool for the job. For one, it is continually listening and updating the results while the user is still entering data, data that is as yet incomplete, into the JTextField. Much better would be to use an ActionListener added to a JButton or to your JTextFields.

    I suppose you could use a FocusListener, but even that concerns me since it is quite low-level.

    Also: consider using an InputVerifier to validate your input.

    Also: consider displaying your tabular data in a JTable where the 1st and 2nd columns are editable but the others are not.

    Edit
    I’m not sure if this is kosher, but it could work if you do your calculation from within the verifier. For example, updated for generality:

    import javax.swing.*;
    
    /**
    * @see http://stackoverflow.com/a/11818183/522444
    */
    public class VerifierEg {
    
        private static final String ZERO = "0.0";
        private JTextField field1 = new JTextField(ZERO, 5);
        private JTextField field2 = new JTextField(ZERO, 5);
        private JTextField resultField = new JTextField(ZERO, 10);
    
        private void createAndShowGui() {
            resultField.setEditable(false);
            resultField.setFocusable(false);
    
            JPanel mainPanel = new JPanel();
            final JTextField[] fields = {field1, field2};
    
            mainPanel.add(field1);
            mainPanel.add(new JLabel(" x "));
            mainPanel.add(field2);
            mainPanel.add(new JLabel(" = "));
            mainPanel.add(resultField);
    
            for (JTextField field : fields) {
                field.setInputVerifier(new MyInputVerifier(field));
            }
    
            JFrame frame = new JFrame("VerifierEg");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(mainPanel);
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private void calcProduct() {
            double d1 = Double.parseDouble(field1.getText());
            double d2 = Double.parseDouble(field2.getText());
            double prod = d1 * d2;
            resultField.setText(String.valueOf(prod));
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    VerifierEg eg = new VerifierEg();
                    eg.createAndShowGui();
                }
            });
        }
    
        /**
        * @see http://stackoverflow.com/a/11818946/230513
        */
        private class MyInputVerifier extends InputVerifier {
    
            private JTextField field;
            private double value;
    
            public MyInputVerifier(JTextField field) {
                this.field = field;
            }
    
            @Override
            public boolean shouldYieldFocus(JComponent input) {
                if (verify(input)) {
                    field.setText(String.valueOf(value));
                    calcProduct();
                    return true;
                } else {
                    field.setText(ZERO);
                    field.selectAll();
                    return false;
                }
    
            }
    
            @Override
            public boolean verify(JComponent input) {
                try {
                    value = Double.parseDouble(field.getText());
                    return true;
                } catch (NumberFormatException e) {
                    return false;
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on a project in C# (.net4). Project pretty much allows
I've been trying to get WCF security working for my project, and have had
I have a personal project I've been working on in my spare time. It's
Been working on a personal add-in for VS 2008 and have been researching automation
I have been working on this app for at least 3-4 months and just
I have been working on a project on and off, but I haven't touched
I have been working on a huge ETL project with 150+ tables and during
I am working on a personal project as a way of learning more about
For a personal project I have been implementing my own libstdc++. Bit by bit,
Context: As a personal learning project I've been working on a simple computer algebra

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.