Reputation:
I'm a beginner programmer and I wrote this for my school class. But somehow even my JOptionPane.showMessageDialog under displaypay() won't work!!! I expect that a message box will pop up, but instead after I enter the amount of hours for each day, nothing happens! It doesn't even skip the part, the whole program just pauses! I am so confused! Instead if I use System.out.println(), it works fine.
Also I don't want to do System.out.println for the displaypay(), I have to use showMessageDialog.
package payroll.program;
import java.util.Scanner;
import javax.swing.JOptionPane; //imports
class Employee
{
int hourlypay;
int totalhours = 0;
String name; //variables
void getname()
{
Scanner scan = new Scanner(System.in);
System.out.println("What is this employee's name?");
name = scan.next(); //gets name
}
void calculatepay()
{
int[] hours = new int[5]; //creates array for hours
Scanner scan = new Scanner(System.in);
System.out.println("How much is " + name + " paid per hour?");
hourlypay = scan.nextInt(); //gets hourly pay
for (int i = 0; i < 5; i++)
{
System.out.println("How many hours did " + name + " work on day " + (i + 1) + "?");
hours[i] = scan.nextInt(); //gets hour on each day
totalhours = totalhours + hours[i]; //adds hours up
}
}
void displaypay()
{
JOptionPane.showMessageDialog(null, "You have to pay " + " $" + totalhours * hourlypay + " to " + name + "!"); //displays total pay
}
}
public class PayrollProgram {
public static void main(String[] args) {
int numberofemployees; //variable for # of employees
System.out.println("Welcome to the Payroll Program!"); //welcomes user
Scanner scan = new Scanner(System.in);
System.out.println("How many employees do you have?");
numberofemployees = scan.nextInt(); //gets input for # of employees
Employee[] ArrayofEmployees = new Employee[numberofemployees]; //creates array of employees with the same size as the number of employees
for (int i = 0; i < numberofemployees; i++)
{
ArrayofEmployees[i] = new Employee(); //creates an Employee for each space in the array
}
for (int i = 0; i < numberofemployees; i++)
{
ArrayofEmployees[i].getname();
ArrayofEmployees[i].calculatepay();
ArrayofEmployees[i].displaypay(); //runs functions in class Employee for each employee
}
}
}
This is my program and the JOptionPane.showMessageDialog doesn't work.
Please help!
Upvotes: 2
Views: 2465
Reputation: 36473
When you try to display swing components, you are expected to do so from an event dispatching thread. Otherwise, you won't get reliable results. So, in this case, wrap all your code inside your main inside a call to SwingUtilities.invokeLater:
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
int numberofemployees; //variable for # of employees
System.out.println("Welcome to the Payroll Program!"); //welcomes user
Scanner scan = new Scanner(System.in);
System.out.println("How many employees do you have?");
numberofemployees = scan.nextInt(); //gets input for # of employees
Employee[] ArrayofEmployees = new Employee[numberofemployees]; //creates array of employees with the same size as the number of employees
for (int i = 0; i < numberofemployees; i++) {
ArrayofEmployees[i] = new Employee(); //creates an Employee for each space in the array
}
for (int i = 0; i < numberofemployees; i++) {
ArrayofEmployees[i].getname();
ArrayofEmployees[i].calculatepay();
ArrayofEmployees[i].displaypay(); //runs functions in class Employee for each employee
}
}
});
}
More info can be found here: The Event Dispatch Thread.
In particular, notice what it says can happen if you fail to run Swing components from the event dispatcher thread:
Programs that ignore this rule may function correctly most of the time, but are subject to unpredictable errors that are difficult to reproduce.
Upvotes: 1