Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9246201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:26:25+00:00 2026-06-18T09:26:25+00:00

Hello I am having an issue when linking a single listener to multiple buttons.

  • 0

Hello I am having an issue when linking a single listener to multiple buttons. I’m trying to use inner classes but it seems I’m getting it wrong somewhere. Can someone point me to the right direction?
If it helps the auto-correct thingy ( 😀 ) points to line 59 saying:

“createChampButton cannot be resolved to a variable”

Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;




public class GUI extends JApplet{



    public void init(){
        Container guiContainer = getContentPane();
        LayoutManager layout = new FlowLayout();
        guiContainer.setLayout(layout);


        //Create Championship Button
        final JButton createChampButton = new JButton("Create Championship");
        guiContainer.add(createChampButton);



        //Create Club Button
        final JButton createClubButton = new JButton ("Create Club");
        guiContainer.add(createClubButton);


        //Create Athlete Button
        final JButton createAthleteButton = new JButton ("Create Athlete");
        guiContainer.add(createAthleteButton);


        //Print Athletes Button
        final JButton printAthletesButton = new JButton ("Print all Athletes");
        guiContainer.add(printAthletesButton);


        //The quiet Listener
        ButtonListener aListener = new ButtonListener();
        printAthletesButton.addActionListener(aListener);
        createAthleteButton.addActionListener(aListener);
        createClubButton.addActionListener(aListener);
        createChampButton.addActionListener(aListener);

    }

     class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent event){
            JButton button = (JButton) event.getSource();
            //if (button.equals(printAthletesButton)){
                //JOptionPane.showMessageDialog(null, "Athlete name is: " +anAthlete.GetAthleteName());
            //  JOptionPane.showMessageDialog(null, "Athlete age is: " + anAthlete.GetAge());
                //}
            if(button.equals(createChampButton)){
                    Championship aChampionship = new Championship("","");
                    aChampionship.champName = JOptionPane.showInputDialog("Enter Championship Name: ");
                    aChampionship.duration = JOptionPane.showInputDialog("Enter Championship Duration: ");
            }       
        }
    }
}

thanks in advance,
Chris

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:26:26+00:00Added an answer on June 18, 2026 at 9:26 am

    createChampButton is not defined in your other method, so the scope doesn’t make possible that you access that. I see three options how you can work around that:

    1) You use component.getActionCommand() instead – you can compare it with the text that your JButton holds (something like if( evt.getSource().getActionCommand().equals("Create Championship")

    2) You can define your ActionListener within your init method:

    public void init(){
        Container guiContainer = getContentPane();
        LayoutManager layout = new FlowLayout();
        guiContainer.setLayout(layout);
    
    
        //Create Championship Button
        final JButton createChampButton = new JButton("Create Championship");
        guiContainer.add(createChampButton);
    
    
    
        //Create Club Button
        final JButton createClubButton = new JButton ("Create Club");
        guiContainer.add(createClubButton);
    
    
        //Create Athlete Button
        final JButton createAthleteButton = new JButton ("Create Athlete");
        guiContainer.add(createAthleteButton);
    
    
        //Print Athletes Button
        final JButton printAthletesButton = new JButton ("Print all Athletes");
        guiContainer.add(printAthletesButton);
    
    
        //The quiet Listener
        ActionListener aListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event){
                JButton button = (JButton) event.getSource();
                //if (button.equals(printAthletesButton)){
                    //JOptionPane.showMessageDialog(null, "Athlete name is: " +anAthlete.GetAthleteName());
                //  JOptionPane.showMessageDialog(null, "Athlete age is: " + anAthlete.GetAge());
                    //}
                if(button.equals(createChampButton)){
                        Championship aChampionship = new Championship("","");
                        aChampionship.champName = JOptionPane.showInputDialog("Enter Championship Name: ");
                        aChampionship.duration = JOptionPane.showInputDialog("Enter Championship Duration: ");
                }       
            }
        };
        printAthletesButton.addActionListener(aListener);
        createAthleteButton.addActionListener(aListener);
        createClubButton.addActionListener(aListener);
        createChampButton.addActionListener(aListener);
    
    }
    

    }

    3) You define your JComponents as instance variable – declaring them outside your init() method (but assigning them inside, still)

    Regards,
    Danyel

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hello i am having same issue, i tried your solution but it didnt help
hello fellow java developers. I'm having a very strange issue. I'm trying to read
Hello I've been trying to resolve the issue that I've been having with the
so I'm trying to work out an issue I'm having in designing PHP classes.
Hello I'm having an issue with this check box : <input type=checkbox id=hideTempSeries checked=checked
Using RAILS 2.2 Hello I am having an issue where my dates are being
hello fellow java developers. I'm having a bit of an issue here. I have
Hello guys can anyone help me with the issue im having with installation of
Hello I'm currently having an issue with a timer in a program I'm developing.
Hello i am having a issue with htaccess mod_rewrite, maybe someone here could point

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.