ChrisK
ChrisK

Reputation: 15

Linking single listener to multiple buttons

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 ( :D ) 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

Upvotes: 0

Views: 1685

Answers (2)

Danyel
Danyel

Reputation: 2250

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

Upvotes: 2

SLaks
SLaks

Reputation: 888205

createChampButton is a local variable in init()

To access it elsewhere, you need to change it to a field in the class.

Upvotes: 4

Related Questions