1

I couldn't find the answer anywhere else online, so I came here. I apologize in advance if the mistake in my code is very obvious; I'm still quite new to java swing. Here's what's going on: I have created a JButton named toggleElevators, and I want it to change text when clicked. I have already created an ActionListener and added it to toggleElevators. All I want right now is for the JButton to change text when clicked from Click me to Clicked.

First, here's a picture of what the JFrame looks like when executed:

JFrame

NOTE: There is a third class, but it is purely for drawing the picture on the left. It has nothing to do with the GridLayout or the JButton.

Run class (created frame and adds toggleElevators JButton:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JFrame;

public class Run extends Input{

Input i = new Input();

public static void main(String[] args) {
    new Run();
}

public Run() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame("Elevators");
            frame.setLayout(new GridLayout(0, 3));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new Elevators(Color.MAGENTA, true));
            frame.add(new Elevators(Color.ORANGE, false));
            frame.setSize(800,600);
            frame.setResizable(false);

            frame.getContentPane().add(toggleElevators); //adds toggleElevators button to JFrame
            i.addButtonListeners(); //calls method defined in Input class, which adds the ActionListener to the toggleElevators button

            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

}

Input class (creates toggleElevators JButton and its ActionListener):

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class Input {
JButton toggleElevators = new JButton("Click me.");

public void addButtonListeners() {
    toggleElevators.addActionListener(new toggleElevatorsListener());
}

class toggleElevatorsListener implements ActionListener {
    public void actionPerformed (ActionEvent event) {
        toggleElevators.setText("Clicked.");
        System.out.println("ActionListener called."); //I know the ActionListener is not being called because this line is not being printed out in the console
    }
}
}
Nick
  • 61
  • 8
  • Why are you complicating things? Why don't you add a `ActionListener` to the `JButton` directly? – nom Jun 18 '15 at 22:57
  • @NabeelOmer Is that what is causing the problem? Created an inner class for the ActionListener? I didn't think that would do anything. – Nick Jun 18 '15 at 22:58
  • I am not near a computer right now (hell, it's 4 in the morning here), but you are complicating things by making a separate class for the `ActionListener` try adding it to the `JButton` directly. And the – nom Jun 18 '15 at 23:03
  • I think that's why anonymous class or lambda functions are for ¿? – Alex S. Diaz Jun 18 '15 at 23:09

3 Answers3

1

Your Run class extends Input, but also HAS an Input named i. You're adding this.toggleElevators to the frame, but you're adding a listener to i.toggleElevators.

Remove the i field from your class. I would also forget completely about defining and extending an Input class. It doesn't serve any purpose, and seems to confuse more than help you.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you. I had completely forgotten that my code referred to basically separate instances of the `toggleElevators` button. I've followed your advice and deleted the `i` object, and that worked. Thanks again for your time! – Nick Jun 18 '15 at 23:08
0

You create a new Input in your Run class, while the Run class also extends Input.

When you call i.addButtonListeners(); the action listeners are added on the toggleElevators from i and not on the toggleElevators you inherited from the Input class.

Try addButtonListeners().

Peter Neyens
  • 9,770
  • 27
  • 33
0

Your Run class extends Input. Therefore it has its own toggleElevators which is the one it sets in the frame. However, i has is own toggleElevators where it sets the event listeners. So they are not set on the one in the frame but on one that never gets used.

You can simply delete the i object. As Run extends Input, it can call the method directly, and then the listener will be added to its own toggleElevators.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79