• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JRadioButton labels don't change on repaint()

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaah. Now I get you. Get back to you in a bit.

Alan
 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic