Colonder
Colonder

Reputation: 1576

Changing labels with resource bundle

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.

Upvotes: 2

Views: 3239

Answers (2)

Colonder
Colonder

Reputation: 1576

As an answer to my own question with help of Madhan my class looks like this now (Sounds is the name of that other class!):

package GuitarPro;

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, program;
private JMenuItem UKLang, DELang, FRLang, ESPLang, POLLang, RUSLang, about, exit;
private ResourceBundle labels;
private Locale currentLocale;
private String aboutApplication;

public  MenuBar()
{
    menuBar = new JMenuBar();
    language = new JMenu("Language");
    program = new JMenu("Program");

    UKLang = new JMenuItem("English");
    DELang = new JMenuItem("German");
    FRLang = new JMenuItem("French");
    ESPLang = new JMenuItem("Spanish");
    POLLang = new JMenuItem("Polish");
    RUSLang = new JMenuItem("Russian");
    exit = new JMenuItem("Exit");
    about = new JMenuItem("About");

    UKLang.addActionListener(this);
    DELang.addActionListener(this);
    POLLang.addActionListener(this);
    FRLang.addActionListener(this);
    ESPLang.addActionListener(this);
    RUSLang.addActionListener(this);
    about.addActionListener(this);
    exit.addActionListener(this);

    language.add(UKLang);
    language.add(DELang);
    language.add(FRLang);
    language.add(POLLang);
    language.add(ESPLang);
    language.add(RUSLang);

    program.add(about);
    program.add(exit);

    menuBar.add(Box.createVerticalGlue());
    menuBar.add(program);
    menuBar.add(language);

}

public JMenuBar getMenu()
{
    return menuBar;
}

public void setLanguage()
{
    Sounds.amplitudeLabel.setText(labels.getString("amplitude_label"));
    Sounds.frequencyLabel.setText(labels.getString("frequency_label"));
    Sounds.phaseLabel.setText(labels.getString("phase_label"));
    Sounds.componentWavesLabel.setText(labels.getString("component_waves_label"));
    Sounds.resultWaveLabel.setText(labels.getString("result_wave_label"));
    Sounds.play.setText(labels.getString("play_button"));
    Sounds.playTriade.setText(labels.getString("play_triade_button"));
    Sounds.saveImage.setText(labels.getString("save_image_button"));
    Sounds.saveWave.setText(labels.getString("save_sample_button"));
    Sounds.info_message = labels.getString("save_success_message");
    Sounds.err_message = labels.getString("save_error_message");
    Sounds.wave_1 = labels.getString("wave_1");
    Sounds.wave_2 = labels.getString("wave_2");
    Sounds.wave_3 = labels.getString("wave_3");
    aboutApplication = labels.getString("about");
}

@Override
public void actionPerformed(ActionEvent e) 
{
    Object input = e.getSource();


    if(input == UKLang)
    {
        currentLocale = new Locale("en");

        labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_en", currentLocale);
        setLanguage();
    }

    else if(input == DELang)
    {
        currentLocale = new Locale("de");

        labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_de", currentLocale);
        setLanguage();
    }

    else if(input == FRLang)
    {
        currentLocale = new Locale("fr");
        labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_fr", currentLocale);
        setLanguage();
    }

    else if(input == POLLang)
    {
        currentLocale = new Locale("pl");
        labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_pl", currentLocale);
        setLanguage();
    }

    else if(input == ESPLang)
    {
        currentLocale = new Locale("esp");
        labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_esp", currentLocale);
        setLanguage();
    }

    else if(input == RUSLang)
    {
        currentLocale = new Locale("rus");
        labels = ResourceBundle.getBundle("GuitarPro.LabelsBundle_rus", currentLocale);
        setLanguage();
    }

    else if(input == about)
        JOptionPane.showMessageDialog(null, aboutApplication);

    else if(input == exit)
        dispose();

}

}

One of my bundle resource file looks like this:

# This is the default LabelsBundle.properties file

amplitude_label = Amplitude
frequency_label = Frequency (Hz)
phase_label = Phase (Degrees)
component_waves_label = Component Waves
result_wave_label = Result Wave
play_button = Play
play_triade_button = Play Triad
save_image_button = Save Image
save_sample_button = Save Sample
save_success_message = File saved successfully
save_error_message = File could not be saved
wave_1 = Wave 1
wave_2 = Wave 2
wave_3 = Wave 3
about = test

To achieve this I had to make all objects in other classes static:

public static JLabel amplitudeLabel, frequencyLabel, phaseLabel, componentWavesLabel, resultWaveLabel;
public static JButton play, playTriade, saveImage, saveWave;
public static String info_message = "File saved successfully";
public static String err_message = "File could not be saved";
public static String wave_1 = "Wave 1";
public static String wave_2 = "Wave 2";
public static String wave_3 = "Wave 3";

Then I could do exactly what I wanted. Thanks and good luck!

Upvotes: 0

Madhan
Madhan

Reputation: 5818

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 
}

Upvotes: 1

Related Questions