Results 1 to 6 of 6
  1. #1
    Kimya is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Question 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.

    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);
        }    
    }
    I appreciate any help you could offer. Thank you very much!
    Last edited by Kimya; 06-30-2012 at 03:31 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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 04:09 AM. Reason: clarification

  3. #3
    Kimya is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default Re: GUI waiting for ActionEvent

    Quote Originally Posted by doWhile View Post
    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)
    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!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: GUI waiting for ActionEvent

    Is there any way I can make the class "wait" for the button to be clicked at some point in the code before continuing?
    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)
    Last edited by doWhile; 06-30-2012 at 06:10 PM. Reason: clarification

  6. #6
    Kimya is offline Member
    Join Date
    Jun 2012
    Posts
    3
    Rep Power
    0

    Default 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

  1. Wait for ActionEvent
    By Razion in forum New To Java
    Replies: 2
    Last Post: 05-31-2012, 01:08 PM
  2. Need help with ActionEvent/ItemEvent
    By patriotsfan in forum AWT / Swing
    Replies: 2
    Last Post: 06-05-2011, 03:17 PM
  3. What does (this) and ActionEvent e mean?
    By africanhacker in forum New To Java
    Replies: 3
    Last Post: 02-06-2011, 06:08 PM
  4. StringBuilder/ActionEvent help!
    By Red727 in forum New To Java
    Replies: 8
    Last Post: 12-07-2010, 04:01 AM
  5. ActionEvent example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 12:00 AM

Tags for this Thread

Posting Permissions

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