I am working on a payroll program for school. I only two weeks into working with GUIs so my skills have a lot of room for growth. Everything works fine except for this method. I am trying to get the method to clear the JTextAreas if the number entered is out of range and skip the remaining code that sends the data to the JTextArea. As of now, it prints out the name, dept and 0.00 if out of range but I do not want it to display anything unless it is within range.
public void buildAgain() {
// declare variables to hold the JTextField input as string
String rateStr, hoursStr, firstName, lastName, dept;
firstName = (String) fnameField.getText();
lastName = (String) lnameField.getText();
rateStr = rateField.getText();
// convert the rate field into double in order to calculate it
rate = Double.parseDouble(rateStr);
hoursStr = hoursField.getText();
// convert the hours into double in order to calculate it
hoursWorked = Double.parseDouble(hoursStr);
// Check how many hours worked to make sure they are within range
if(hoursWorked >= 0 && hoursWorked <=60)
hours = hoursWorked;
// Display message if hours are not within range
else
JOptionPane.showMessageDialog(null, "You have entered an invalid number of hours. \n"
+ " Please enter a number between 0 and 60.", "Hour Entry Error",
JOptionPane.ERROR_MESSAGE);
// Clears JTextFields after the error message
fnameField.setText("");
lnameField.setText("");
hoursField.setText("");
rateField.setText("");
// check the hourly rate to make sure it is within range
if (rate >= 6 && rate <=150)
payRate = rate;
// display an error message if rate entered not within range
else
JOptionPane.showMessageDialog(null, "You have entered an invalid pay rate. \n "
+ "Please enter the rate between 6 and 150.", "Pay Rate Error",
JOptionPane.ERROR_MESSAGE);
// clear the JTextFields after error message
fnameField.setText("");
lnameField.setText("");
hoursField.setText("");
rateField.setText("");
// calculate the pay regular hours
if (hours >= 0 && hours <= 40){
weeklyPay = hours*payRate;
// Calculate overtime pay
} else if (hours > 40 && hours <= 60){
overTime = (hours-40) * (payRate*1.5);
weeklyPay = overTime + (40*payRate);
}
// Display the total pay in uneditable table
totalPayField.setText("$"+dollar.format(weeklyPay));
// Send name to JTextArea
list.append(firstName + " ");
list.append(lastName);
// Get the selected department
if(hr.isSelected())
dept = hr.getText();
else if(accounting.isSelected())
dept = accounting.getText();//"Accounting";
else if(marketing.isSelected())
dept = marketing.getText();
else if(sales.isSelected())
dept = sales.getText();
else
dept = shipping.getText();
// Send selected department and pay to JTextArea
list.append("\t" + dept);
list.append("\t$" + dollar.format(weeklyPay) + "\n");
}
You're almost there.
Move the section of code where you clear your JTextFields before the section of code where you check hours worked are within range. Then return just after you show your error message. That way execution of the method does not continue.
//Clears JTextFields before checking the error message
fnameField.setText("");
...
//check how many hours worked to make sure they are within range
if(hoursWorked >= 0 && hoursWorked <=60)
hours = hoursWorked;
//Display message if hours are not within range
else
JOptionPane.showMessageDialog(null, "error msg.\n", JOptionPane.ERROR_MESSAGE);
return
Note: If you only want the fields cleared after theres an error then move them into the else statement where the error message is shown.
Ex:
//check how many hours worked to make sure they are within range
if(hoursWorked >= 0 && hoursWorked <=60) {
hours = hoursWorked;
//Display message if hours are not within range
} else {
JOptionPane.showMessageDialog(null, "error msg.\n", JOptionPane.ERROR_MESSAGE);
//Clears JTextFields before checking the error message
fnameField.setText("");
...
return
}