This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Applets and the fly likes How to switch the Container background from one color to another using one button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "How to switch the Container background from one color to another using one button" Watch "How to switch the Container background from one color to another using one button" New topic
Author

How to switch the Container background from one color to another using one button

danny dich
Greenhorn

Joined: Mar 06, 2013
Posts: 13
Any ideas on this code. l trying to change the background color of the container from one color to another each time the button is clicked, l used the if statement, it checks if the x is even then it chooses the background color to set based on the Boolean expression result, it seems as if the fired event is not recognizing the if statement so it keeps choosing one color yellow. Is there anything wrong with the if statement?



Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2161
    
  47
Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.

Your problem is variable 'x' is local to the actionPerformed method and so every time you press the button a new 'x' is being used which is initialized to 0 and hence the same colour. I suggest you make the 'x' variable an instance variable rather than a local method variable, ie move it out of the method.
danny dich
Greenhorn

Joined: Mar 06, 2013
Posts: 13
thank you it worked perfectly, it had slipped my mind that an applet can have attributes just like any other class.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2161
    
  47
thank you it worked perfectly,

No problem, glad I could help.
 
 
subject: How to switch the Container background from one color to another using one button
 
Similar Threads
how to flash JButton background
JDialogBox
Round Button Border
How to use ComboBox as PropertyEditor for Property of Java Bean Component?
How to know which button being clicked?