I am working on the payroll calculation program below and would like some help with the JTextField area. I would like the program to respond with a GUI window letting the user know that the JTextField is blank and must enter First name, last name, and so on. Any help would be greatly appreciated. Thanks!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class PayrollProgram extends JFrame
{
   private JLabel fnameL, lnameL, hoursL, hrateL, idL, weeknumL, numdepL;

   private JTextField fnameTF, lnameTF, hoursTF, hrateTF, idTF, weeknumTF, numdepTF;

   private JButton calculateB, exitB;

   private CalculateButtonHandler cbHandler;
   private ExitButtonHandler ebHandler;

   private static final int WIDTH = 400;
   private static final int HEIGHT = 300;

   public PayrollProgram()
   {
             //Create the four labels
      fnameL = new JLabel("Enter First Name: ",SwingConstants.RIGHT);
      lnameL = new JLabel("Enter Last Name: ",SwingConstants.RIGHT);
      hoursL = new JLabel("Enter Hours Worked: ", SwingConstants.RIGHT);
      hrateL = new JLabel("Enter Hourly Rate: ", SwingConstants.RIGHT);
      idL = new JLabel("Enter Employee ID: ", SwingConstants.RIGHT);
      weeknumL = new JLabel("Enter Week Number: ", SwingConstants.RIGHT);
      numdepL = new JLabel("Enter Number of Dependants: ", SwingConstants.RIGHT);

             //Create the four text fields
      fnameTF = new JTextField(10);
      lnameTF = new JTextField(10);
      hoursTF = new JTextField(10);
      hrateTF = new JTextField(10);
      idTF = new JTextField(10);
      weeknumTF = new JTextField(10);
      numdepTF = new JTextField(10);

             //Create Calculate Button
      calculateB = new JButton("Calculate");
      cbHandler = new CalculateButtonHandler();
      calculateB.addActionListener(cbHandler);

             //Create Exit Button
      exitB = new JButton("Exit");
      ebHandler = new ExitButtonHandler();
      exitB.addActionListener(ebHandler);

             //Set the title of the window
      setTitle("Calculate a Pay Check");

             //Get the container
      Container pane = getContentPane();

             //Set the layout
      pane.setLayout(new GridLayout(9, 2));

             //Place the components in the pane
      pane.add(fnameL);
      pane.add(fnameTF);
      pane.add(lnameL);
      pane.add(lnameTF);
      pane.add(hoursL);
      pane.add(hoursTF);
      pane.add(hrateL);
      pane.add(hrateTF);
      pane.add(idL);
      pane.add(idTF);
      pane.add(weeknumL);
      pane.add(weeknumTF);
      pane.add(numdepL);
      pane.add(numdepTF);
      pane.add(calculateB);
      pane.add(exitB);

             //Set the size of the window and display it
      setSize(WIDTH, HEIGHT);
      setVisible(true);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
   }

   private class CalculateButtonHandler implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         double width, length, area, perimeter;

         /*length = Double.parseDouble(lengthTF.getText());
         width = Double.parseDouble(widthTF.getText());
         area = length * width;
         perimeter = 2 * (length + width);

         areaTF.setText("" + area);
         perimeterTF.setText("" + perimeter);*/
      }
   }

   private class ExitButtonHandler implements ActionListener
   {
       public void actionPerformed(ActionEvent e)
       {
           System.exit(0);
       }
   }

   public static void main(String[] args)
   {
       PayrollProgram rectObject = new PayrollProgram();
   }
}

Recommended Answers

All 8 Replies

When the user clicks the button, but text fields have not been completed properly, it's normal to use a JOptionPane to display an error message. After the user has closed the error message he can fix his input, and press the button again.
JOptionPane documentation is in the usual API documentation places.
You'll probably want the version that looks like this
JOptionPane.showMessageDialog(null, "You must complete all the text fields", "Error", JOptionPane.ERROR_MESSAGE);

Thanks for your help. I understand what you are doing with the statement you posted but how can I get the error meassage to pop up when the JTextField is left empty. I'm thinking a if statement, but then again I am probably wrong. When I put in the
JOptionPane.showMessageDialog(null, "You must complete all the text fields", "Error", JOptionPane.ERROR_MESSAGE);
The error message window shows up before the GUI window that displays all the input fields. Am i making sense?

Yes, you;ll need an if test to see if the input is OK, and if it's not then display the error message. You should have an action listener somewhere that runs when the user clicks OK (or whatever you call it) and that's where you should test the inputs before trying to calculate the payroll

I kind of get what you are talking about could you please show with some code it sure would save me alot of time. I would really appreciate it. Thanks.

I'm not here to write code for you, I want to help you learn to code for yourself.
Try. If you get stuck post what you've done and say what's stopping you. Someone will help.

Yes I know that and I don't want someone to write the program for me but it just helps to understand when I can see some of the code. Other than that as I am a newbie to Java I was wondering with the payroll program I am trying to write, can a Jbutton have more than one function? For example, the problem you have been helping me with...to get a message box to tell the user that some input fields are empty, can the same button that calculates also provide the error message if there are empty fields where the name and hours and so forth?

You can add button like Apply or "Check Data" and add ActionListener
here is the sample

JButton apply = new JButton();

apply.new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				//do your stuff here
				
			}
		});

after clicking a button your dialog will shows up

I didn't check your code before
add this listener to your's calculate button

I don't quite understand what you mean by, "add this listener to your's calculate button" This is what i have and it still wrong. Not anyones fault, just lacking in experience.

Any and all help would be greatly appreciated.

private class CalculateButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double width, length, area, perimeter;


JButton apply = new JButton();


private class apply implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (fnameTF == null)
{
JOptionPane.showMessageDialog(null, "You must complete all the text           fields", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.