Results 1 to 3 of 3
- 09-25-2011, 09:25 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 7
- Rep Power
- 0
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
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.
-
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.
- 09-25-2011, 05:41 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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
-
Running main method class from another main class
By tlrocketman in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 AM -
Could not find main class: Jframe. Program will exit.
By egbert95 in forum New To JavaReplies: 4Last Post: 07-29-2010, 02:53 PM -
Could not find main class: Jframe. Program will exit.
By egbert95 in forum Advanced JavaReplies: 8Last Post: 07-29-2010, 02:53 PM -
How to make a Jframe un-focusable when another Jframe is active?
By Robert_85 in forum Advanced JavaReplies: 4Last Post: 04-22-2009, 11:02 PM -
how to make server Wait for clients further requests after establishing connection
By nadia in forum NetworkingReplies: 4Last Post: 02-02-2009, 04:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks