Okay getting quite frustrated! I am trying to get an if statement to implement a pop up window when the user fails to enter any information in one of the JTextFields like fnameTF. Here the if statement but where do I place it to work in my program?
if (fnameTF.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "First Name is reqired ",
"Invalid Entry",JOptionPane.ERROR_MESSAGE);
fnameTF.requestFocusInWindow();
}
AND HERE IS THE PROGRAM CODE THAT IS COMPLETED THUS FAR.
import java.io.*;
import java.util.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.JOptionPane.*;
import java.awt.*;
import java.awt.event.*;
public class PayrollProgram extends JFrame
{
//public static void main(String[] args) throws FileNotFoundException
private JLabel fnameL, lnameL, hoursL, hrateL, idL, weeknumL, netPayL;
//left column of GUI component for field names
private JTextField fnameTF, lnameTF, hoursTF, hrateTF, idTF, weeknumTF, netPayTF;
//right column of GUI component for entering information in the fields
private JButton calculateB, resetB, addB, exitB; //Buttons listed on GUI component
private CalculateButtonHandler cbHandler;
private ResetButtonHandler rbHandler;
private AddButtonHandler abHandler;
private ExitButtonHandler ebHandler;
//methods objects for submit buttons on GUI
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
//height and width of GUI component
public PayrollProgram()
{
//Create the seven labels, right justified
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);
netPayL = new JLabel("Calculated Net Pay: ", SwingConstants.RIGHT);
//Create the seven text fields, ten characters in length
fnameTF = new JTextField(10);
lnameTF = new JTextField(10);
hoursTF = new JTextField(10);
hrateTF = new JTextField(10);
idTF = new JTextField(10);
weeknumTF = new JTextField(10);
netPayTF = new JTextField(10);
//Create Calculate Button to determine and show calculated net pay
calculateB = new JButton("Calculate");
cbHandler = new CalculateButtonHandler();
calculateB.addActionListener(cbHandler);
//Create Reset Button to clear text in field and re-enter information
resetB = new JButton("Reset");
rbHandler = new ResetButtonHandler();
resetB.addActionListener(rbHandler);
//Create Add to File Button which will print all informatio to a file
addB = new JButton("Add to File");
abHandler = new AddButtonHandler();
addB.addActionListener(abHandler);
//Create Exit Button closes the GUI screen when complete
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 of rows and columns
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(netPayL);
pane.add(netPayTF);
pane.add(calculateB);
pane.add(addB);
pane.add(resetB);
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 hours, rate, netPay;
//calculate pay and display in caluculated net pay
hours = Double.parseDouble(hoursTF.getText());
rate = Double.parseDouble(hrateTF.getText());
netPay = hours * rate;
netPayTF.setText("" + netPay);
}
}
private class ResetButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//resets JTextFields
fnameTF.setText("");
lnameTF.setText("");
hoursTF.setText("");
hrateTF.setText("");
idTF.setText("");
weeknumTF.setText("");
netPayTF.setText("");
}
}
//Add to File code
private class AddButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Stream to write file
FileOutputStream fout;
try
{
// Open an output stream
fout = new FileOutputStream ("myfile.txt");
// Print a line of text
new PrintStream(fout).println ("hello world!" + fnameTF.getText());
// Close the output stream
fout.close();
}
catch (IOException f)
{
System.err.println ("Unable to write to file");
System.exit(-1);
}
}
}
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args) throws FileNotFoundException
{
PayrollProgram rectObject = new PayrollProgram();
}
}