3

In relation to my previous problem, I now have a new problem. In order to avoid the inner class, my class now implements an actionListener. My code is as follows:

public class MainGame extends JDialog implements ActionListener {

    public MainGame(JDialog owner) {
        super(owner, true);
        initComponents();
        jPanel1.setLayout(new GridLayout(3, 9, 3, 5));
        for (char buttonChar = 'a'; buttonChar <= 'z'; buttonChar++) {
            String buttonText = String.valueOf(buttonChar);
            letterButton = new JButton(buttonText);
            letterButton.addActionListener(this);
            jPanel1.add(letterButton);
        }

        newGame();
    }

    public void actionPerformed (ActionEvent action){
        if (action.getSource() == letterButton) {
            letterButton.setEnabled(false);
        }
    }

How can I effect the listener to my buttons A to Z? Because all it can listen to is the last button which in this case is button Z.

Thank you.

Community
  • 1
  • 1
newbie
  • 14,582
  • 31
  • 104
  • 146

4 Answers4

5

You listener can listen to events from all buttons just fine. Your problem is that you seem to believe that you can only manipulate class fields. In fact, you don't need the letterButton field at all for what you're trying to do:

public void actionPerformed (ActionEvent action){
    ((JButton)action.getSource()).setEnabled(false);
}
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
2

Your action listener is listening to all your buttons. However you are only checking it against the last button.

Instead do something like this:

if(action.getSource() instanceof JButton){
    ((JButton)action.getSource()).setEnabled(false);
}
jzd
  • 23,473
  • 9
  • 54
  • 76
2

Actually there is lots of code missing in what you gave. I suspect that letterButton is a field in your class. Therefore you assign this field over and over again in your for loop (via letterButton = new JButton(buttonText);).

The ActionListener then compares to you field (which is by then the last button) and therefore is only triggered with button 'z'.

Possible solution: Use actionCommand on your buttons to identify which button was pressed.

Howard
  • 38,639
  • 9
  • 64
  • 83
  • @newbie use letterButton.setActionCommand(buttonText); and in your listener e.g.: ((JButton)action.getSource()).setEnabled(false); System.out.println(action.getActionCommand()); – Howard Jan 30 '11 at 15:16
0

I think you're far better off using either an anonymous inner class or a private inner class for this than doing what you're trying to do right now -- have your gui class implement ActionListener. That being said, isn't this statement backwards?

action.getSource() == letterButton

In fact, does that line even compile? I would be surprised if it did as you're trying to assign a value to a method call which makes no sense.

better would be

letterButton == action.getSource();

Do you see why?

edit: ignore the crap above. not enough sleep or caffeine. sigh....

Also, I've replied to your previous thread about a way to use anonymous inner class and not have to worry about declaring variables as final.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373