-1

So I want a JButton to alternate between 2 colors when clicked. So say it is originally yellow. When it is clicked it changes to red and when it is clicked again, it changes back to yellow and then next click = red and so on.

Please fill in the code for me in between this block: Assume the JButton variable is btn and use Color.RED and Color.YELLOW

        btnSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
             ...
            }
        }
btrballin
  • 1,420
  • 3
  • 25
  • 41
  • 3
    You've mistaken `SO` for a coding service. Read [this](http://stackoverflow.com/help/how-to-ask) – Reimeus Nov 29 '15 at 23:16
  • Use a `boolean` flag to determine which color you should use, flip it's state on each click. – MadProgrammer Nov 29 '15 at 23:16
  • I tried that in a `public void actionPerformed(ActionEvent e){ ... }` block but it's not letting me access global variables inside it for some reason... – btrballin Nov 29 '15 at 23:27
  • See: http://stackoverflow.com/questions/5808022/changing-the-background-color-of-a-selected-jtogglebutton for some suggestions. – cliff2310 Nov 29 '15 at 23:41
  • Alright, I implemented a integer variable where it turns 1 color when odd and another when even. – btrballin Nov 30 '15 at 01:14

2 Answers2

0

Try this:

    btnSearch.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e){

                //create pressed as a global boolean
                if(pressed){

                        buttonExample.setBackground(Color.yellow); 
                        pressed = false;

                }else{

                        buttonExample.setBackground(Color.red);
                        pressed = true;
                }
        }
    }

Not 100% sure if this will work, if not, create a function out of the listener class (In your User Interface class) and add the exact same code, then call that function from the listener.

UPDATED: Changed wrong calls:

JButton.setBackground(Color.yellow);
JButton.setBackground(COlor.red);

to: buttonExample.setBackground(Color.yellow); buttonExample.setBackground(Color.red);

Mayuso
  • 1,291
  • 4
  • 19
  • 41
  • `setBackground()` is not a static method (and it is not reasonable to be static), hence calling `JButton.setBackground()` is illegal – Adrian Shum Dec 01 '15 at 01:49
  • What do you mean with illegal? I use all the time and works fine, you mean it´s not a good practice? – Mayuso Dec 01 '15 at 07:35
  • `setBackground()` is an instance method. You cannot call an instance method by `ClassName.methodName()`, unless so happened your variable is also named `JButton` – Adrian Shum Dec 01 '15 at 08:56
  • Oh, yes, of course. My mistake, i already knew that, I will update. Was obvious for me, but can be misunderstood, sorry. – Mayuso Dec 01 '15 at 10:57
0

I hate giving direct answer. However this is an example that, if you actually give a second thought, will lead you to your solution:

btnSearch.addActionListener(new ActionListener() {
    @Override public void actionPerformed(ActionEvent e){
        btnSearch.setText(btnSearch.getText() + "!");
    }
}

The above example will update the text on the button by appending ! when you click.

It will be your job to figure out how to determine the existing state of button, and set new background accordingly.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131