How to link text with JLabels in java?

I have several GUI forms in my Java application. All forms have texts. I installed texts for my components from a common object called a dictionary. I need a function in my program; to switch the language at the request of the user; so that all texts in all forms are replaced by another language. I have all the texts in a common dictionary. Is there any clean way to change the language in a clean way? I know about netbeans internationalization tool, but I want to use a different method.

Edit: for example:

label1.setText (Dictionary.Hello);

and the dictionary class is defined as:

public class Dictionary { public static String Hello = "hello"; } 

and for another language:

  public class DictionaryPersian extends Dictionary { public DictionaryPersian(){ Hello = "درود"; } } 

I want to find a way to bind the Dictionary.hello field to jlabel1 so that when you change the value of the variable, it will affect the text of jlabel1.

+4
source share
1 answer

For example, you can subclass JLabel by setting the text directly from the dictionary using some id. It can also act as a listener to change text or language to automatically change the displayed text accordingly.

To explain some code fragments (not tested and incomplete, the dictionary character can be implemented in different ways, if you like, your dictionaries should also implement IDictionary )

 public interface IDictionaryListener { void dictionaryChanged(IDictionary from, IDictionary to); } public interface IDictionary { String getString(String forKey); } public final class DictionaryManager { private static final DictionaryManager INSTANCE=new DictionaryManager(); private final List<IDictionaryListener> listeners=new ArrayList<>(); private IDictionary dictionary; private DictionaryManager() {}; public static synchronized void setDictionary(IDictionary dict) { IDictionary old = INSTANCE.dictionary; INSTANCE.dictionary=dict; fireDictionaryChanged(old, dict); } public static synchronized IDictionary getDictionary() { return INSTANCE.dictionary; } public static synchronized void addDictionaryListener(IDictionaryListener l) { INSTANCE.listeners.add(l); } public static synchronized void removeDictionaryListener(IDictionaryListener l) { INSTANCE.listeners.remove(l); } private static void fireDictionaryChanged(IDictionary from, IDictionary to) { for (IDictionaryListener l:INSTANCE.listeners) { l.dictionaryChanged(from, to); } } } public class DictionaryLabel extends JLabel implements IDictionaryListener { private String key; public DictionaryLabel(String dictKey) { super(); key = dictKey; DictionaryManager.addDictionaryListener(this); super.setText(DictionaryManager.getDictionary().getString(key)); } @Override public final void setText(String text) { throw new RuntimeException("Not supported! Dictionary is used for this!"); } @Override public void dictionaryChanged(final IDictionary from, final IDictionary to) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { DictionaryLabel.super.setText(to.getString(key)); } }); } } 

As I said, only one example bounced, I hope you understand this idea.

+1
source

Source: https://habr.com/ru/post/1399461/


All Articles