wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JRadioButton labels don't change on repaint() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JRadioButton labels don Watch "JRadioButton labels don New topic
Author

JRadioButton labels don't change on repaint()

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi Guys,

I'm working on an app that allows the user to select between two languages: English and French. I've set up a JMenu labelled "Language" with two submenus: English and French. When the user selects either one, either of these code is run:



The loadEnglishMenus_Buttons() and loadFrenchMenus_Buttons() methods run just fine changing all the menus, buttons and labels to their appropriate language as provided by the corresponding PNSResource objects. However, for some reason I have two JRadioButtons on my interface whose labels won't change from what they were set to initially. The call to this.repaint() isn't affecting them for some reason. Anybody have any ideas as to why that might be? Please advise.

Alan
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

If you repaint a component, and it appears unchanged from the way it was before, that's because it IS unchanged from the way it was before. In other words, your code (which you didn't show us) doesn't change those components.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

And really, if you're having to call repaint() at all, that means you're doing something wrong. If you change the state of a component from code in the Swing event thread then its visible form (what you see on the screen) will change. You don't have to call repaint(). So if you're finding you do have to call repaint() to get your component changes to appear, then you are doing the changes on some other thread. Which would be the wrong thing to do. The Swing threading tutorial explains how to do threading right in the Swing context.
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi there,

You were right about the call to repaint(). I didn't need it. I removed it and everything changes as expected except those JRadioButtons! The call to loadEnglishMenus_Buttons() and loadFrenchMenus_Buttons() does retrieve the necessary text from the PNSResource classes for all components, and when I step through Eclipse in debug mode the appropriate resource is being returned on the call messages.getString("rbtnSingleKeyword"). But the label in JRadioButton is unaffected from what it was initially.

Here's a snippet of the loadFrenchMenus_Buttons() method:



Alan
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

As I suspected... you aren't changing the radio buttons. Instead you're creating new ones. And since those new ones aren't the ones already displayed in the GUI, you don't see any changes in the GUI.
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Aaah. Now I get you. Get back to you in a bit.

Alan
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
OK...got it figured out. I had to create new methods that just changed the text and also influenced the size of the components without creating "new" components. Thanks for the guidance.

Alan
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4543
    
    5

Alan Shiers wrote:OK...got it figured out. I had to create new methods that just changed the text and also influenced the size of the components without creating "new" components. Thanks for the guidance.

Alan

You would normally* revalidate() the container of the components in order to re-layout the GUI with the new sizes. While not always needed, it's good practice to repaint() after revalidating.

* unless you've fallen into the trap of using a null layout


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JRadioButton labels don't change on repaint()