Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Action Listeners within and Action Listener on Panels

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Action Listeners within and Action Listener on Panels

    Hello,

    I am working on creating a game in which after the user ends the session by pressing the "cash out" button, the game then displays a JLabel that asks them if they would like to play again. I also show them a "Yes" and "No" button.

    I am able to get the "Yes" and "No" buttons to display, but I can't get the buttons to do anything (my yes/no button action listeners aren't working).

    So far, I have the "Cash Out" button listener that will display the end points and then asks if they want to play again.

    Any feedback is highly appreciated!

    Thanks!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Action Listeners within and Action Listener on Panels

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

    Show the yes/no button actionListener code (hopefully commented) that isn't giving the desired results. Include enough info (other classes, comments, and supporting code) for someone who has never seen your code to understand what it's supposed to do. A runnable example that demonstrates the problem would be best.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Action Listeners within and Action Listener on Panels

    Here is my code:

     
        // creates listener for cash out button
        public class cashOutButtonListener implements ActionListener {
     
            public void actionPerformed (ActionEvent event) {
                cashOutLabel.setText("Your cash out value is: "+ currentTokens);
     
                //resiezes primary panel
                primary.setPreferredSize (new Dimension(300, 275));
     
                // creates label that asks question if they want to play again
                playAgainLabel = new JLabel("Would you like to keep playing?");
                playAgainLabel.setForeground(Color.MAGENTA);
                buttonPanel.add(playAgainLabel);
     
                //resizes button panel
                buttonPanel.setPreferredSize (new Dimension (275, 150));
     
                //creates yes and no buttons
                yesButton = new JButton ("Yes");
                noButton = new JButton ("No");
                buttonPanel.add(yesButton);
                buttonPanel.add(noButton);        
     
                // if the click "No" button, frame closes
                if ((JButton)event.getSource() == noButton) {
                    System.exit(0);
                }
     
                // if they click "Yes" button, Yes/No button are removed and current tokens set to 5
                if ((JButton)event.getSource() == yesButton) {
                    buttonPanel.remove(yesButton);
                    buttonPanel.remove(noButton);
                    buttonPanel.repaint();
                    spinResult = 0;
                    currentTokens = 5;
     
                }
     
            }


    --- Update ---

    Instead of making the buttons into action listeners, I just added if-statements, but it still won't work.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Action Listeners within and Action Listener on Panels

    still won't work.
    Is the listener added to the button? Add a println() that prints a message in the listener so you can see if it is called.

    BTW the reference from getSource() does not need to be cast to compare with another reference.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. action listeners on java
    By mariosarmy in forum AWT / Java Swing
    Replies: 4
    Last Post: February 11th, 2014, 04:27 PM
  2. [SOLVED] Anonymous inner class with Action Listener and Abstract Action
    By shakes6791 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 13th, 2013, 03:52 PM
  3. Action Listeners and Key Listeners Help
    By xctive in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 09:27 AM
  4. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM

Tags for this Thread