Okay so I am making a 2d array of JToggleButtons. I got the action listener up and going, but I have no way to tell which button is which.

If I click one, all it returns is something like


Is there anyway to stick some sort of item or number in the button object to associate each button?
And then when the button is clicked I can retrieve that item or number that was given to it?

Here is my button generator code. (How could I make "int l" associate (and count) to each button made, when it is called, it will return that number, or something along those lines.

JToggleButton buttons[][] = new JToggleButton[row][col];
int l = 0;
		for (int i = 0; i < row; i++) {
			for (int j = 0; j < col; j++) {
				buttons[i][j] = new JToggleButton("");
				buttons[i][j].addActionListener(new e());



public class e implements ActionListener {

	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();


variable "source" is what I use to get my data, so how can int l, be returned through "source" (as its unique value for the unique button clicked) as a button is clicked?


Recommended Answers

putClientProperty / getClientProperty
methods inherited from JComponent allow you to associate anything you want with any JComponent.

Jump to Post

All 4 Replies

putClientProperty / getClientProperty
methods inherited from JComponent allow you to associate anything you want with any JComponent.

Well said James.

Everything in java is object and though you may not see inside, but they are connected one way or another. JFrames,JButtoms etc.. all externds from the JComponent .
from htc phone.

Yup, I just ran across the clientproperty thing.

Thanks guys!

Ok another question I can't seem to find.
I got everything working, but I also have imageicons to display the bomb if it is hit.
Since my action listener is in a different class, how do I get that to update to my engine? Right now it says the variable is nonexistent, since its declared in the Engine.

public class MyActionListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
		JToggleButton btn = (JToggleButton) e.getSource();
		System.out.println("clicked column " + btn.getClientProperty("column")
				+ ", row " + btn.getClientProperty("row") + ", bomb status "
				+ btn.getClientProperty("bomb"));

		if (btn.getClientProperty("bomb").equals(true)) {


Be a part of the DaniWeb community

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