I have been having some trouble with this action listener scenareo, and I am doing it exactly like what my java book says to do, and something is still not working. In the action listener the if statement is not working correctly. Here is an example of my code. The label in the center of the frame should be changing, but doesnt.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class Test extends JFrame implements ActionListener{

    private JButton team1;
    private JLabel label;

    public static void main(String[] args) {
        new Test();
    }//end main

    public Test(){
        super("Favorite Teams");
        this.setVisible(true);
        this.setSize(300, 300);
        this.setLocationRelativeTo(null);//center the frame 
        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Initialize the JButtons
        JButton team1 = new JButton("Zerg");//video game starcraft

        //initialize the JLabel
        label = new JLabel("Please select a team. ");
        label.setHorizontalAlignment(JLabel.CENTER);

        //add the JButtons to the frame
        this.add(team1, BorderLayout.LINE_START);

        //add JLabel to the frame
        this.add(label, BorderLayout.CENTER);

        //add ActionListeners to the buttons
        team1.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source == team1)//Zerg
            label.setText("Ok, if you like critters. ");
    }//end method

}//end class

Recommended Answers

All 3 Replies

Howdy Friend;
After a few moments of tinkering with some old GUI code I had on my computer here is what I conjured up.

package test1;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 *
 * @author Patrick
 */

public class Test1 extends JFrame implements ActionListener
{
    private JLabel sampleJLabel;
    private JButton sampleJButton;
    public Test1()
    {
        super("Example GUI");                           //creates your title

        setDefaultCloseOperation(EXIT_ON_CLOSE);        //defines default close

        setPreferredSize(new Dimension(400, 90));       //default dimensions

        setLayout(new FlowLayout());                    //standard flowLayout

        sampleJButton = new JButton("Change");          //create a button with
                                                        //the visible text "Change"
                                                        //this will be the button
                                                        //we click in order to
                                                        //change the text of the
                                                        //JLabel

        sampleJButton.setActionCommand("testButton");   //this just declares the
                                                        //name of your action
                                                        //in this case 'testButton'

        sampleJButton.addActionListener(this);          //now we need to register
                                                        //the sampleJButton
                                                        //this way we can alter
                                                        //the JLabel text

        sampleJLabel = new JLabel("Temporary Label");   //basic declaration of a JLabel
                                                        //this enables you to see the
                                                        //change that will occur

        add(sampleJButton);                             //add to frame
        add(sampleJLabel);                              //add to frame
        pack();                                         //finish 'packing'
        setVisible(true);                               //allow everyone to see
        setResizable(false);                            //this can be true or false... doesn't matter
    }
    @Override                                           //this will override the actionPerformed method
                                                        //meaning use the actionPerformed that we define
                                                        //and not the actionPerformed that defaults with
                                                        //our ActionListener
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("testButton"))   //thanks to us naming our action
                                                        //we can reference that in order
                                                        //to tell us what to do through
                                                        //a simple if...else block
        {
            sampleJLabel.setText("Your desired String");
        }
    }
    public static void main(String[] args)
    {
        Test1 test1 = new Test1();
    }
}

I included some comments to help you out. Please keep in mind, I am not the best at GUIs and this is only one way to do the task that you requested. Hope I helped

There is a big difference between a field and a local variable. You must not treat them lightly. When you say JButton team1 on line 28 you are creating a local variable called team1 and every time you use the name team1 in the method after that you are using the local variable, not the field of the same name. The type JButton is not something you can sprinkle into your source code at will without consequences.

In other words, when you say if(source == team1) on like 46, it is not the same team1 as the one you used in the constructor. This team1 is still null, so source == team1 is never going to happen.

commented: I over-complicated it. +0
commented: Good catch +0

I see the error of my ways. Good catch bguild. I still cant believe that the local variable declaration even made it into my code in the first place. Arg!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.