tags:

views:

56

answers:

4
A: 

Are you calling repaint() anywhere? Also, when a window becomes visible (uncovered or deminimized) or is resized, the "system" automatically calls the paintComponent() method for all areas of the screen that have to be redrawn.

Amir Afghani
Nope. But something is firing off the update event, I just don't know what...
Stephane Grenier
+2  A: 

I don't know which component this is, but setting an icon on a button from within a paint routine is a bad idea. It will definitely cause the button to be repainted. If the button is a child of your component then setting the button invalidate the component too causing an infinite loop.

Set the icon somewhere else such as where the dialog / window is set up initially.

locka
Close!! You're very close. It won't definitely cause it to be repainted.
Stephane Grenier
+1  A: 

The problem is that you are setting the icon in the paintComponent() method. You should never set a property in this method.

Swing components are smart enough to repaint themselves whenever a property changes. In this case you have the problem of the component repainting itself because the Icon changes, but you are also rereading the Icon every time the component repaints itself which is also not very efficient.

camickr
The reason to set it here is so that if I change the Locale, all I need to do is fire an event on the parent component and all children component will be repainted with the new text.
Stephane Grenier
+1  A: 

The setIcon(ImageIcon) will revalidate and repaint itself ONLY if the ImageIcon is another instance.

When working with Locales, most people are use to the ResourceBundle, which returns Strings, which in turn are immutable. Therefore setting the text over and over doesn't matter.

However, in this case, the ResourceLoader (custom class) returned a new instance of an ImageIcon. Sure it was the same Image, but it was another instance. And if you decompile the code, you'll see that setIcon (at least for JButtons), it will repaint and revalidate if newIcon != oldIcon.

The solution was to use a HashMap in the ResourceLoader, this way it saves from loading the images more than once since most of the images are used very frequently (might as well reuse the instances if you can). Turns out that overall this quick adjustment also saved a decent amount of overall memory consumption as an added bonus.

Stephane Grenier
It is still not a good idea to change a property in the paintComponent() method. You can't control, when or how often the method is invoked by the RepaintManager. If you have functionality the allows the user to change locales dynamically, then you should also have a routine that loops through all components that are affected and have them update themselves rather than customizing the paintComponent() method of every component.
camickr
I thought about that, but this can get really insanely complex very quickly. Imagine trying to keep track of every component in Microsoft Outlook. By going this route, I can force a repaint on the main JFrame/JWindow which can then force a repaint on all sub-components. And as an added bonus, there's very little code, and most importantly no code sprinkled everywhere to help the Locale Manager keep track of all the components, etc.
Stephane Grenier