Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding ActionListener to buttons

Status
Not open for further replies.

jackz15

Programmer
Jun 28, 2006
103
US
The following code that i used adds 100 buttons onto the JPanel. But because there are so many buttons, it would not bode for me to manually make a variable name to all of them. So i'm wondering what I should do to be able to add actionlistener to all of the buttons. Since the way that i did it doesn't permit me to use "JButton.addActionListener(new MyAction(button))", since i have not set a variable to any of the variables(button).


int numButton=100;
...
JPanel pane = new JPanel(new GridLayout(10, 10));
while(numButton>0){
b_array[numButton]=pane.add(new JButton(" "+numButton));



label.setLabelFor(button);

numButton--;


}
pane.add(label);
pane.setBorder(BorderFactory.createEmptyBorder(
30, //top
30, //left
30, //bottom
30) //right
);

return pane;
}

I haven't done Java in a while, so i'm sorry if i'm doing something obvious wrong.
Thanks for replies in advance!
 
You can use a anon. inline class, or use the following to add any action listener of your choice:
Code:
for(int i = 0; i < b_array.length; ++i)
{
  b_array[i].setActionCommand(/*command string goes here*/);
  b_array[i].setActionListener(new ActionListener(){
    public void ActionPerformed(ActionEvent e)
    {
      String action = e.getActionCommand();
      //stuff goes here
    }
  });
}

[plug=shameless]
[/plug]
 
i did something like this:


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SwingApplication implements ActionListener {
private static String labelPrefix = "clicks:";
private int numClicks = 0;
final JLabel label = new JLabel("0 ");
private int numButton = 100;
private int b_array[]=new int[9];


public Component createComponents() {




JPanel pane = new JPanel(new GridLayout(11,10));

while(numButton>0){
JButton button = new JButton("");
button.setPreferredSize(new Dimension(5,10));

pane.add(button);

button.addActionListener(this);



label.setLabelFor(button);

numButton--;





}

pane.add(label);
pane.setBorder(BorderFactory.createEmptyBorder(
50, //top
50, //left
10, //bottom
50) //right
);

return pane;
}

public void actionPerformed(ActionEvent e) {
numClicks++;
label.setText(labelPrefix + numClicks);
}
this results in 100 buttons arranged neatly, and all of them being able to count clicks. But because i'm working on a game(somewhat like Nim: ), and i need to be able to specifically refer to a single button by using its row and column number, I'm not sure if this is the best approach. What do you think?
 
for(int i = 0; i < b_array.length; ++i)
{
b_array.setActionCommand(/*command string goes here*/);
b_array.setActionListener(new ActionListener(){
public void ActionPerformed(ActionEvent e)
{
String action = e.getActionCommand();
//stuff goes here
}
});
}

how do you set an array value to a button?
i can't do this for each of the buttons
JButton b_array = new JButton(words);

 
Please, for everyone's sanity use code tags when posting code. Do you mean how do you decleare an array of buttons? Alright, I'm not bothering to compile this so it may contain typos...

Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class TestButtonArray extends JFrame implements ActionListener
{
  public static void main(String[] args)
  {
    new TestButtonArray().show();
  }

  public TestButtonArray()
  {
    super();
    int n = 10;
    JButton[] buttons = new JButton[n];
    for(int i = 0; i < n; ++i)
    {
      buttons[n] = new JButton("Button " + n);
      buttons[n].setActionListener(this);
      buttons[n].setActionCommand("Button "+n);
    }
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }

  public void ActionPerformed(ActionEvent e)
  {
    System.out.println(e.getActionCommand() + 
                       " was pressed.")
  }
}

[plug=shameless]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top