java - Why isn't my JOptionPane.showMessageDialog working? -


i'm beginner programmer , wrote school class. somehow joptionpane.showmessagedialog under displaypay() won't work!!! expect message box pop up, instead after enter amount of hours each day, nothing happens! doesn't skip part, whole program pauses! confused! instead if use system.out.println(), works fine.

also don't want system.out.println displaypay(), have 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 employee's name?");     name = scan.next(); //gets name }  void calculatepay() {     int[] hours = new int[5]; //creates array hours      scanner scan = new scanner(system.in);      system.out.println("how " + name + " paid per hour?");     hourlypay = scan.nextint(); //gets hourly pay      (int = 0; < 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     } } void displaypay() {     joptionpane.showmessagedialog(null, "you have pay " + " $" + totalhours * hourlypay + " " + name + "!"); //displays total pay }  }   public class payrollprogram {      public static void main(string[] args) {      int numberofemployees; //variable # of employees      system.out.println("welcome payroll program!"); //welcomes user      scanner scan = new scanner(system.in);     system.out.println("how many employees have?");     numberofemployees = scan.nextint(); //gets input # of employees      employee[] arrayofemployees = new employee[numberofemployees]; //creates array of employees same size number of employees      (int = 0; < numberofemployees; i++)     {         arrayofemployees[i] = new employee(); //creates employee each space in array     }      (int = 0; < numberofemployees; i++)     {         arrayofemployees[i].getname();         arrayofemployees[i].calculatepay();         arrayofemployees[i].displaypay(); //runs functions in class employee each employee     } } 

}

this program , joptionpane.showmessagedialog doesn't work.

please help!

when try display swing components, expected event dispatching thread. otherwise, won't reliable results. so, in case, wrap code inside main inside call swingutilities.invokelater:

public static void main(string[] args) {     javax.swing.swingutilities.invokelater(new runnable() {         public void run() {             int numberofemployees; //variable # of employees              system.out.println("welcome payroll program!"); //welcomes user              scanner scan = new scanner(system.in);             system.out.println("how many employees have?");             numberofemployees = scan.nextint(); //gets input # of employees              employee[] arrayofemployees = new employee[numberofemployees]; //creates array of employees same size number of employees              (int = 0; < numberofemployees; i++) {                 arrayofemployees[i] = new employee(); //creates employee each space in array             }              (int = 0; < numberofemployees; i++) {                 arrayofemployees[i].getname();                 arrayofemployees[i].calculatepay();                 arrayofemployees[i].displaypay(); //runs functions in class employee each employee             }         }     }); } 

more info can found here: the event dispatch thread.

in particular, notice says can happen if fail run swing components event dispatcher thread:

programs ignore rule may function correctly of time, subject unpredictable errors difficult reproduce.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -