Results 1 to 9 of 9
  1. #1
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Question Better way to update GUI language on the fly?

    Hi there,

    I'm writing an application with language support. Right now the language (locale) is taken from a external file and the proper resource bundle is taken. After that all the GUI is constructed getting the texts from there. This way in order to apply a language the app must be restarted so the new bundle is taken before adding the components.

    My intention is to make this language change on the fly and the only way I come up with is by using an array to reference all components with text and when the bundle locale is changed do a loop in this array to update all the texts but it feels like a tacky/inefficient way to do it. Right now my idea would work like this:
    Java Code:
    public class ResourceManager {
    ...
        private ResourceBundle texts;
    ...
        private TextedComponent [] textedComponents = new TextedComponent [100];
        private int textedComponentsCount = 0;
    ...
        public void setLocale(Locale l){
            texts = ResourceBundle.getBundle(textsPath, l);
            updateComponentsText();
        }
    ...
        public void updateComponentsText(){
            for (int i=0; i<textedComponentsCount; i++){
                textedComponents[i].c.setText(texts.getString(textedComponents[i].key));
            }
        }
    ...
    }
    class TextedComponent{
        JTextComponent c;
        String key;
    }
    Anyone can guide me in a better way to update all the texts?

    Thanks in advance, regards.
    Last edited by b0rt; 08-10-2011 at 02:06 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    That seems like a fine way to do it to me.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default

    Thanks Kevin,

    I was just wondering if there was a standard way to do this since components have the "setLocale" method.

    Anyway I just found a problem with my idea and that is JTextComponent only has 5 subclasses so it doesn't work with many components of type such as JMenu, JMenuItem, JLabel, JButton, etc etc.. Is there another superclass that covers all the components with the "setText" method?

    Thanks again, regards.

  4. #4
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default

    Maybe using just JComponent as superclass and reflect to get the method?
    Java Code:
        public void updateComponentsText(){
            for (int i=0; i<textedComponentsCount; i++){
                try{
                    JComponent c = textedComponents[i].c;
                    Method setTextMethod = c.getClass().getMethod("setText", String.class);
                    setTextMethod.invoke(c, textedComponents[i].key);
                  } catch (Exception e) {
                      // Handle this
                  }
                
            }
        }
    But I still see this way inefficient and wonder if there should be a better way to do it...

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Or you could always just use instanceof. It might be slightly gross, but I think it's less gross than using reflection.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default

    But it would have to check instanceof for every type that supports "setText", would that be better than reflection?

    I insist there must be a better way to do all this... and still wonder why components have a Locale attribute...

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default

    Locale is for stuff like text alignment, currency, time, stuff like that. It won't automatically translate text for you (and you wouldn't want it to).

    You could also have a List of each type of setText-supporting component. But really, how many different classes are you talking about here? It's probably not really that many. But whether reflection or instance of or multiple Lists is "better" is really up to you.

    I suppose you could even write your own interface that specifies a setText(String) method, then extend each type of setText-supporting component to implement that interface, then store them that way. Again, whether that's better than just using instanceof or multiple Lists is up to you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default

    You only need to test for JLabel and AbstractButton.

    db

  9. #9
    b0rt is offline Member
    Join Date
    Aug 2011
    Posts
    22
    Rep Power
    0

    Default

    Ok, got it. I just guessed there would be a simpler/standardized way to do it since translating a GUI seems like a quite common process to me.

    Thanks a lot for your suggestions Kevin.

Similar Threads

  1. Update GUI when selecting language
    By ludberg in forum SWT / JFace
    Replies: 0
    Last Post: 06-04-2010, 05:39 PM
  2. Replies: 5
    Last Post: 08-25-2009, 09:41 AM
  3. Is this the right language?
    By rws in forum New To Java
    Replies: 9
    Last Post: 11-15-2008, 03:27 PM
  4. IDE for new language
    By mitra2008 in forum Eclipse
    Replies: 2
    Last Post: 06-23-2008, 06:43 PM
  5. V language 0.004
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-19-2007, 03:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •