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

javax.swing.JToggleButton[,59,58,19x14,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@53343ed0,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=]

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].setSize(15,15);
				buttons[i][j].addActionListener(new e());
				panel.add(buttons[i][j]);
				l++;

			}
		}

ActionListner

public class e implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		System.out.println(source);
	}

}

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?

Thanks,
-Austin

Recommended Answers

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 {

	@Override
	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)) {
			btn.setIcon(bombimage);
			System.out.println("boom!");

		}
	}

}
Be a part of the DaniWeb community

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