• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Changing background color of JButton in another class  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to change the background color of numerous JButtons in two different classes. In one, it is working in the other it isn't . I'm sorry if this question is not really hard, but I just can't figure it out. Here the simplified code:
Main - class

And the ChangeButton - class

I know this version f the code doesn't always make sense, but I just wanted an example that is short and produces the same problem.
 
Master Rancher
Posts: 2758
93
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Peter,

welcome to the Ranch and enjoy the stay!

In your code, you have two Main objects, with ditto buttons. One Main is set in the main method of Main itself, the second Main is created in the setActionListener() method of te class ChangeButton. It is that second button that has its color changed, but the first button, as shown in the frame, is left unchanged. Therefore you see nothing happening with that button.

Now, there are several remarks to be made about this code, but as you say, it is just a quick demo version, so I don't know whether your real version has some better code. Let us know if you would like to have some comments.
 
Peter Seeger
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:

In your code, you have two Main objects, with ditto buttons. One Main is set in the main method of Main itself, the second Main is created in the setActionListener() method of te class ChangeButton. It is that second button that has its color changed, but the first button, as shown in the frame, is left unchanged. Therefore you see nothing happening with that button.


Okay, thanks. This is my first program with multiple classes, so I was not aware this was a problem. But if I remove the mainObject from the main - method how should I call buttonManager without making it static? Or do I have to?
 
Piet Souris
Master Rancher
Posts: 2758
93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what exactly you mean, but here is a possible way to avoid having two Main objects. I pass the button to-be-changed as a parameter to the setActionListener method, and invoke that method every two seconds. I also put in some extra lines regarding the frame, to make it viewable. Again, this is just some demo code, it could be improved quite a lot. But do have a look at the code!

Note: for technical reasons I had to name Main as Main2, but that is irrelevant.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!