• 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 can I trigger the repainting of the components?

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Where does a nanny get ground to air missles? Protect this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic