4

I am creating a hangman game. I made a button A - Z using the GUI Toolbars in Netbeans as follows:. enter image description here

My problem is, how can I add an actionlistener to all of it. Is it possible to use a loop? If i click the button A, i will get the character 'a' and so on..

The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
newbie
  • 14,582
  • 31
  • 104
  • 146

3 Answers3

5

Yes it is possible to use a loop, but since your JButtons were created by using NetBeans code-generation, they won't be in an array or collection initially, and so this is something that you'll have to do: create an array of JButton and fill it with the buttons created by NetBeans. Then it's a trivial matter to create a for loop and in that loop add an ActionListener that uses the ActionEvent's actionCommand (as noted above) in its logic.

Having said this, I think that the better solution is to forgo use of the NetBean's GUI builder (Matisse) and instead to create your Swing code by hand. This will give you much greater control over your code and a much better understanding of it as well. For instance, if you do it this way, then in your for loop you can both create your buttons, add the listeners, and add the button to its container (JPanel).

e.g.,

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class Foo2 {
    public static void main(String[] args) {
        JPanel buttonContainer = new JPanel(new GridLayout(3, 9, 10, 10));
        List<JButton> letterButtons = new ArrayList<JButton>(); // *** may not even be necessary
        for (char buttonChar = 'A'; buttonChar <= 'Z'; buttonChar++) {
            String buttonText = String.valueOf(buttonChar);
            JButton letterButton = new JButton(buttonText);
            letterButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String actionCommand = e.getActionCommand();
                    System.out.println("actionCommand is: " + actionCommand);
                    // TODO fill in with your code
                }
            });

            buttonContainer.add(letterButton);
            letterButtons.add(letterButton);
        }

        JOptionPane.showMessageDialog(null, buttonContainer);
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    +1, but since the action is the same for all buttons you only need to create a single ActionListener. The listener should be created outside the loop and then added to the button inside the loop. – camickr Jan 29 '11 at 16:32
3

Well, with some pseudo code, wouldn't this make sence for you?

for(button in bord) {
    button.addActionListener(my_actionlistener);
}

Then in your actionlistener you can see which button was pressed

public void actionPerformed(ActionEvent e) {
    // button pressed
    if ("string".equals(e.getActionCommand()) {
         // do something 
    }
    // and so forth
}
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
3

You'll need to add the buttons to a list of some kind so you can iterate through them, Netbeans doesn't do this for you when you generate the buttons.

After that, just run a for each loop on the list containing all the buttons. To get the values of the characters just cast the relevant ascii value, which starts at 97 for a lower case a or 65 for an upper case A:

int charNum = 97;
for(Button b : board) {
    char charVal = (char)charNum;
    charNum++;
    //add the action listener
}
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • It's much better to avoid magic numbers as you're using. Why worry about what the ascii value is when Java can take care of this for you. I've added code to my post above as an example. – Hovercraft Full Of Eels Jan 29 '11 at 13:24
  • This is true (though I can't see any code added to your post?) My logic was that since it's a homework question it'd be clearer this way to see what was happening underneath with the values and suchlike rather than just seeming like magic. – Michael Berry Jan 29 '11 at 13:27
  • Ah something was caching, it's there now! – Michael Berry Jan 29 '11 at 13:28