I'm writing a Java application and I just stuck at one point. I want to make a menu with menu item Language which would obviously change the language of the application by clicking on desired language. So to do that I wrote two .properties files: one default that is called LabelsBundle (englisch) and one extension called LabelsBundle_de (german). I've got class like this:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class MenuBar extends JFrame implements ActionListener
{
private JMenuBar menuBar;
private JMenu language, about;
private JMenuItem UKLang, DELang, FRLang, ESPLang, POLLang;
private ResourceBundle labels;
private Locale currentLocale;
public MenuBar()
{
menuBar = new JMenuBar();
language = new JMenu("Language");
about = new JMenu("About");
UKLang = new JMenuItem("English");
DELang = new JMenuItem("German");
FRLang = new JMenuItem("French");
//ESPLang = new JMenuItem("Spanish");
POLLang = new JMenuItem("Polish");
UKLang.addActionListener(this);
DELang.addActionListener(this);
language.add(UKLang);
language.add(DELang);
language.add(FRLang);
language.add(POLLang);
menuBar.add(Box.createVerticalGlue());
menuBar.add(language);
menuBar.add(about);
}
public JMenuBar getMenu()
{
return menuBar;
}
@Override
public void actionPerformed(ActionEvent e)
{
Object input = e.getSource();
if(input == UKLang)
{
currentLocale = new Locale("en");
labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);
}
else if(input == DELang)
{
currentLocale = new Locale("de");
labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);
}
else if(input == FRLang)
{
currentLocale = new Locale("fr");
labels = ResourceBundle.getBundle("LabelsBundle_fr", currentLocale);
}
else if(input == POLLang)
{
currentLocale = new Locale("pl");
labels = ResourceBundle.getBundle("LabelsBundle_pol", currentLocale);
}
}
}
And I don't know what to do next. My mate told me about
object.setText(resourceBundle.getString("blabla"));
But I really don't know how can I use it with something like this for example
resultWaveLabel = new JLabel("Result wave:");
I assume that I need to write something like
resultWaveLabel.setText(...);
Am I right?
Btw when I click german in the menu list I receive an error
Exception in thread "AWT-EventQueue-0" java.util.MissingResourceException: Can't find bundle for base name LabelsBundle, locale de
EDIT: I'd like to stress that the main issue is how to change labels of such things like above - not to get rid of mentioned error. That's already solved.
Exception
This output was expected, since there is no resource file called “LabelsBundle” in your project or
The same would happen if the resource file exists, but the resource required does not exist.
Also, put this resource file, called LabelsBundle.properties and whatever resource you want, in the same directory as MenuBar
class. Means the java file and properties file should be in same directory.
If this java file and properties files are in a package you have to give the fully qualified path to load it
For example if the properties file is in a package named newpackage then you should load the resource like
ResourceBundle.getBundle("newpackage.LabelsBundle", currentLocale);
Setting value to Label
Refer this link.It is more details
After assigning the ResourceBundle to variable(your case it labels
) just put
resultWaveLabel.setText(labels.getString("blabla"));
To be more precise
@Override
public void actionPerformed(ActionEvent e)
{
Object input = e.getSource();
if(input == UKLang)
{
currentLocale = new Locale("en");
labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);
}
else if(input == DELang)
{
currentLocale = new Locale("de");
labels = ResourceBundle.getBundle("LabelsBundle", currentLocale);
}
else if(input == FRLang)
{
currentLocale = new Locale("fr");
labels = ResourceBundle.getBundle("LabelsBundle_fr", currentLocale);
}
else if(input == POLLang)
{
currentLocale = new Locale("pl");
labels = ResourceBundle.getBundle("LabelsBundle_pol", currentLocale);
}
//Here you go. the magical code to do that
resultWaveLabel.setText(labels.getString("blabla"));///Here that's it your label is updated with the text
}