Results 1 to 3 of 3
  1. #1
    jezer08 is offline Member
    Join Date
    Mar 2011
    Posts
    7
    Rep Power
    0

    Default how can i make my main class to wait for JFrame to return something

    I have 3 classes, 1 for abstract(named Voter) ,1 for the gui components(named LayOut) and 1 for main(named Election).


    On my main I created an object of the gui class and set the visibility to true and the usual stuffs.

    but the problem is, on my main I have a JOptionPane code after my gui stuff and every time the Jframe runs, the main continues to execute the next code after the gui (JFame), which is the JOptionPane. I want the JOptionPane to wait for the JFrame's submit button to be clicked before it executes.

    here is the pic how can i make my main class to wait for JFrame to return something-capture.jpg
    the JFrame and the JOptionPane runs at the same time

    I try having a statement on my submit button's actionlistener that says Voter.setEndGui(true);(The initial value of this is false)

    and in my main i put an if statement before the JOptionpane stuffs. so the main will not immediately read them on runtime, it must wait for the submit button to be click and validate if Voter.isEndGui()==true;

    if(Voter.isEndGui()==true){//like this
    //JOptionPane stuff here

    }

    but that never worked, when i put an if statement before JOptionPane, it never executes even though the condition on the if statement became true because the user clicked the submit button;





    This is my code snippet on my main class
    Java Code:
    else {
    			// GUI components
    
    			LayOut gui = new LayOut();
    			gui.setSize(648, 590);
    			gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			gui.setVisible(true);
    						
    						
    				}
    	//if(Voter.isEndGui()==true){
    					
    		Object obj1 = "Continue or Exit? \n\n";
    		Object stringArray1[] = { "Continue", "Exit" };
    		if (JOptionPane.showOptionDialog(null, obj1,
    		"Continue or Exit",
    		JOptionPane.YES_NO_OPTION,
    		JOptionPane.QUESTION_MESSAGE, null,
    		stringArray1, obj1) == JOptionPane.YES_OPTION) {
    
    		      Voter.setDone(true);
    		}
    
    			else
    		System.exit(0);
    
    						
    					//}

    here is the actionlistener for the submit button in my JFrame

    Java Code:
    private class SubmHandler implements ActionListener {
    
    		public void actionPerformed(ActionEvent arg0) {
    
    			if(counter>5)
    				JOptionPane.showMessageDialog(null, "You Have Selected More than 5 Senators, Please Choose Only 5");
    		
    			else{
    				JOptionPane.showMessageDialog(null, "Thank you For Voting");
    				Voter.setVotedYes(Voter.getCurrentIndex());	
    				Voter.setEndGui(true);
    				Voter.setDone(true);
    				dispose();							
    			}
    Last edited by jezer08; 09-25-2011 at 09:32 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how can i make my main class to wait for JFrame to return something

    Welcome to the world of event-driven programming. If you want your code to respond to the closing of a JFrame, then one way to solve this is to listen to that event, and this can be done with a WindowListener.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: how can i make my main class to wait for JFrame to return something

    I have trouble understanding the purpose of the option pane.

    If it is to confirm clicking on the "Submit" button, then the option pane should be displayed when the "Submit" button is clicked.

    If it is to confirm closing of the JFrame then you can check out Closing an Application Java Tips Weblog which explains Fubarable's suggestion in more detail and provides an alternate solution.

Similar Threads

  1. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  2. Replies: 4
    Last Post: 07-29-2010, 02:53 PM
  3. Could not find main class: Jframe. Program will exit.
    By egbert95 in forum Advanced Java
    Replies: 8
    Last Post: 07-29-2010, 02:53 PM
  4. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  5. Replies: 4
    Last Post: 02-02-2009, 04:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •