Hello everyone. I want to change the background color of the button when I click it.
the problem now is the only the last button works, but not the way I want to.(It changes color I click other buttons.)

here is my button code.

for (int i=0; i<(5; i++) {
        	for (int c=0; c <5; c++){
        		button=new JButton("");
                button.setBackground(Color.WHITE);
                button.setForeground(Color.BLACK);
                button.addMouseListener(new ButtonListener());
                panel1.add(button);
           
        	}
            
        }

here is my listener code.

public class ButtonListener extends MouseAdapter{
		public void mouseClicked(MouseEvent e){
			if(x==0){
				
				button.setBackground(Color.BLACK);
				x=1;
				System.out.println("00000000000000");
			}
			else if(x==1){
				button.setBackground(Color.WHITE);
				x=0;
				System.out.println("1111111111111111");
			}
			
		}

Thanks

you can create arrarys of JButtons but

you have to remove 1st. on 2nd loop,
JButton[] buttons;
buttons = new JButton()...

and add to your ButtonListener class

public void mouseClicked(MouseEvent e){
Object objSource = e.getSource();

then check which buton is clicked

if(objSource == buttons[0]){
buttons[0].set....

Edited 5 Years Ago by mKorbel: n/a

Your current code has a single variable "button" that you use to create all 5 buttons. Nothing wrong with that, although sooner or later you will want to use an array of buttons as per mKorbel. Anyway, after exiting your setup loop "button" is left referring to the last button, and still has that value when you use it in your MouseAdapter. That's why it always changes the last button.

All that x=1, x=0 stuff would be better with a boolean (it has only 2 values!). But to make this work with 5 buttons you need (an array of) 5 booleans. OR just check the current color of the button:

if (button is currently black) set button color white; else set button color black;

If you just want to change the button that was clicked you don't need an array...
you can find out which button was clicked as per mKorbel's code using getSource.
That returns an Object, but you can safely cast that to JButton because you know it can only be a JButton.
Now you have the actual button that was clicked you can trivially change its color.

Its working now. Thank you very much!! mKorbel and JamesCherrill

another question, is there any method or function that I can get the Button's background color? something like Button.getColor?

I am working on a puzzle game. Black color represents the solution. I need to loop the buttons and then compare to my solution to determine whether correct or not.

Given that the method to set a background color is called setBackground I'm going to give you exactly one guess as to what the method to get a background color is called...

Im stuck again.How can I assign 2d array to my buttons
for example. the code above is the initial,
00000
00000
00000
00000

Now i want to create a button when I click it, it should displays as following(I can hard code this );
00010
02000
00040
00200

This article has been dead for over six months. Start a new discussion instead.