Results 1 to 6 of 6
Thread: GUI waiting for ActionEvent
- 06-30-2012, 02:28 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
GUI waiting for ActionEvent
Hello,
I am new to Java and have a question. I am writing a very simple GUI with radio buttons for a game (with "easy", "medium", and "hard" as options) and a "next" button.
All I want it to do is to show the menu and wait until the user selects one of the options and clicks on the "next" button.
Once the button has been clicked, it returns which option was selected ("easy", "medium", or "hard") as a String.
I am having two particular issues here:
- The program does not wait until the user selects and option and clicks next; it returns the difficulty value immediately.
- Is there any way to return that difficulty value directly from the actionPerformed method? The only way I could get around it was declaring it as a global variable and, as you can see, it's not very elegant.
I appreciate any help you could offer. Thank you very much!Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DifficultyLevel implements ActionListener { private JFrame frame; private JButton nextButton; private String difficulty; // radio buttons and button group private JRadioButton easy, medium, hard; private JPanel difficultyPanel; private ButtonGroup difficultyButtonGroup; public void start() { frame = new JFrame("Menu"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS)); // main layout difficultyPanel = new JPanel(); difficultyPanel.setLayout(new BoxLayout(difficultyPanel,BoxLayout.Y_AXIS)); difficultyPanel.setBorder(BorderFactory.createTitledBorder("Difficulty")); difficultyPanel.setPreferredSize(new Dimension(200,0)); difficultyPanel.setAlignmentX(Component.CENTER_ALIGNMENT); difficultyPanel.setAlignmentY(Component.CENTER_ALIGNMENT); // radio buttons difficultyButtonGroup = new ButtonGroup(); easy = new JRadioButton("Easy", true); difficultyButtonGroup.add(easy); difficultyPanel.add(easy); medium = new JRadioButton("Medium"); difficultyButtonGroup.add(medium); difficultyPanel.add(medium); hard = new JRadioButton("Hard"); difficultyButtonGroup.add(hard); difficultyPanel.add(hard); contentPane.add(difficultyPanel); // "Next" Button nextButton = new JButton("Next"); contentPane.add(nextButton); nextButton.setAlignmentX(Component.CENTER_ALIGNMENT); nextButton.setAlignmentY(Component.CENTER_ALIGNMENT); nextButton.addActionListener(this); frame.setSize(200, 200); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == nextButton) { if (easy.isSelected()) difficulty = "easy"; else if (medium.isSelected()) difficulty = "medium"; else difficulty ="hard"; } else System.out.println("Should not get here!\n");} public String returnDifficulty () { this.start(); return (difficulty); } public static void main (String[] args) { DifficultyLevel difficulty = new DifficultyLevel(); String diff = difficulty.returnDifficulty(); System.out.println("Difficulty: " + diff); } }Last edited by Kimya; 06-30-2012 at 02:31 AM.
- 06-30-2012, 03:07 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: GUI waiting for ActionEvent
What do you wish to do with the difficulty value? The main method will not wait until your GUI is 'finished' - think in an event driven manner, when the button is pressed (eg registered ActionListener's are fired and their respective actionPerformed method called) then do something with the difficulty value (whatever that may be)
Last edited by doWhile; 06-30-2012 at 03:09 AM. Reason: clarification
- 06-30-2012, 07:08 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
Re: GUI waiting for ActionEvent
Hi doWhile,
Thank you for replying so quickly. The difficulty value will be returned to the main class and will be used throughout the game. Different settings of the game will be changed depending on the difficulty value.
My main idea for this class was to simply have a class that presents the user with a menu. When they select one of the three options and click on send, the value they chose is sent back to the main menu.
However, the class is not waiting for the user to select the value and click on "next" -- It's sending the result right away, which is obviously still empty.
Is there any way I can make the class "wait" for the button to be clicked at some point in the code before continuing?
Thanks again!
- 06-30-2012, 11:44 AM #4
Re: GUI waiting for ActionEvent
As doWhile has already said, in an event-driven GUI program, you don't wait for something to happen, you respond to events.
Read the API for JButton and follow the link to the Swing tutorial on How to Use Buttons.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 06-30-2012, 05:06 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: GUI waiting for ActionEvent
You could use a JOptionPane (see How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) ) modal dialog. But keep in mind if you are doing this in the main method you should consider a redesign, keeping in mind swing single threading and the event driven model of your application (and - if you are as it seems your code implies - mixing two UI's: command line with GUI)Is there any way I can make the class "wait" for the button to be clicked at some point in the code before continuing?Last edited by doWhile; 06-30-2012 at 05:10 PM. Reason: clarification
- 07-01-2012, 05:07 AM #6
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
Re: GUI waiting for ActionEvent
Thank you, doWhile. I will try this option. You are correct; this is not my main class. The only reason why I was using a main method and the command line here was to make sure it was giving me the correct results. The idea was to not use it as a main class; the idea was to have another class (with the main program) calling this one, and having this one return the difficulty result to the main one.
I will look into the JOptionPane idea. Thanks again!
Similar Threads
-
Wait for ActionEvent
By Razion in forum New To JavaReplies: 2Last Post: 05-31-2012, 12:08 PM -
Need help with ActionEvent/ItemEvent
By patriotsfan in forum AWT / SwingReplies: 2Last Post: 06-05-2011, 02:17 PM -
What does (this) and ActionEvent e mean?
By africanhacker in forum New To JavaReplies: 3Last Post: 02-06-2011, 05:08 PM -
StringBuilder/ActionEvent help!
By Red727 in forum New To JavaReplies: 8Last Post: 12-07-2010, 03:01 AM -
ActionEvent example
By Java Tip in forum Java TipReplies: 0Last Post: 03-11-2008, 11:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks