• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I trigger the repainting of the components?  RSS feed

 
Dmitry Zhuravlev
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone!

Guys, I have been googling a lot about my problem and found a number of similar issues, but they have not helped me

What I want is to refresh my components when something on which my components depend occures.

I have created a special class called MyLabel and overridden a repaint() and setText(..) methods there. Also I have overridden repaint() method in MyButton class. Now when someone calls that methods MyButton should become disabled, and MyLabel should change its caption, both depending on the value of boolean status variable.

I change that variable using another button and I use validate, revalidate, repaintall methods to repaint components, but they do not produce any result. Where I am wrong?

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a reason to subclass JLabel or JButton just yet. Perhaps one exists, but to justify this, you may with to provide more details. I think that you may have a misunderstanding of what repaint does. As far as I understand it, it marks a region of the app (or the whole app) as "dirty" and in need of painting, and that request is placed in a queue that will usually result in a call to paint the component and also may result in calls to paint the held component, but the latter is not necessarily achieved through calling repaint on the held component. In fact, when I tested this out, calling repaint in the containing component calls the paint and paintComponent method of the held component, not repaint (though resizing the app calls all three to be called).
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dmitry Zhuravlev wrote:I have overridden repaint() method in MyButton class. Now when someone calls that methods MyButton should become disabled, and MyLabel should change its caption, both depending on the value of boolean status variable.

Change your design. Painting methods are for painting and you should never never never change the state of the component in any painting method.
 
Dmitry Zhuravlev
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, guys so what is the correct approach to my problem?

Here is a real situation: in my app I use database and internet. I check database and internet connections and if they are NOT VALID I want to make some of my buttons disabled and some labels changed. I can see two ways of implementing this:

1) The straightforward solution is to create a method in my JFrame that runs through some of the components and disables or changes them.
2) I was trying to implement the changing of the components by theirself, so after the connection check I could just run one method - repaint or something - and see the result automatically. But I failed to implement this.

Finally, what is your advice?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!