• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?



 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you it worked perfectly, it had slipped my mind that an applet can have attributes just like any other class.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

thank you it worked perfectly,


No problem, glad I could help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic