To do that I've got this Selector class that shows the end user a combo box, a label and a button. In the combo box the user selects the language (English,Spanish,French,..) and when he clicks in the button shows the label in the language selected.
This works fine but it's just one form, I need to change the language of all the menues in an application, so now I've got the Menus class which shows some items one of them is FILE, when you select FILE, then CONFIGURATION you are in the Selector class.
Now I need to change the items in the menu class when you change the language in the Selector. Do I have to declare all the Menus items in the Selector class to do that?
Any help will be appreciated
Expand|Select|Wrap|Line Numbers
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.*;
- public class Selector extends javax.swing.JFrame
- {
- //private ResourceBundle resources;
- private javax.swing.JLabel jEtSaludo;
- private javax.swing.JButton jBtSaludo;
- private javax.swing.JComboBox langList;
- private javax.swing.JButton jbtAceptar;
- private javax.swing.JLabel jlbGradosC;
- public javax.swing.JMenu jmnuArchivo;
- public Selector() // constructor
- { //loadResources();
- setSize(300, 200); // tamaño del formulario
- setTitle("Language Selector"); // título del formulario
- initComponents(); // iniciar controles o componentes
- }
- private void initComponents()//GEN-BEGIN:initComponents
- {
- getContentPane().setLayout(null);
- addWindowListener(new java.awt.event.WindowAdapter()
- {
- public void windowClosing(java.awt.event.WindowEvent evt)
- {
- exitForm(evt);
- }
- });
- jlbGradosC = new javax.swing.JLabel();
- jlbGradosC.setText("TestLabel");
- getContentPane().add(jlbGradosC);
- jlbGradosC.setBounds(12,28,116,16);
- String[] langStrings = { "Español", "French", "Dutch", "Catalan", "Ingles" };
- langList = new JComboBox(langStrings);
- langList.setSelectedIndex(4);
- getContentPane().add(langList);
- langList.setBounds(42,90,204,30);
- jbtAceptar= new javax.swing.JButton();
- jbtAceptar.setText("Aceptar");
- getContentPane().add(jbtAceptar);
- jbtAceptar.setBounds(195,20,85,26);
- jbtAceptar.addActionListener(new java.awt.event.ActionListener()
- {
- public void actionPerformed(java.awt.event.ActionEvent evt)
- {
- jbtAceptarActionPerformed(evt);
- }
- });
- }//GEN-END:initComponents
- private void jbtAceptarActionPerformed(java.awt.event.ActionEvent evt)
- {
- Object nombre;
- nombre = langList.getSelectedItem();
- System.out.println("Language--> " + nombre);
- if(nombre.equals("Ingles")){
- Locale localizacion = Locale.US; //en inglés
- ResourceBundle mensajes = ResourceBundle.getBundle("Datos",localizacion);
- jlbGradosC.setText(mensajes.getString("Etiquetaprueba"));
- }
- else if(nombre.equals("Español")){
- Locale localizacion2;
- localizacion2 = new Locale("es", "ES");
- ResourceBundle mensajes = ResourceBundle.getBundle("Datos",localizacion2);
- jlbGradosC.setText(mensajes.getString("Etiquetaprueba"));
- }
- else if(nombre.equals("Catalan")){
- Locale localizacion3;
- localizacion3 = new Locale("ca", "ES");
- ResourceBundle mensajes = ResourceBundle.getBundle("Datos",localizacion3);
- jlbGradosC.setText(mensajes.getString("Etiquetaprueba"));
- }
- }
- /** Exit the Application */
- private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
- System.exit (0);
- }//GEN-LAST:event_exitForm
- public static void main (String args[])
- {
- new Selector().setVisible(true);
- }
- }
Expand|Select|Wrap|Line Numbers
- import java.awt.event.*;
- import java.util.ResourceBundle;
- public class Menus extends javax.swing.JFrame
- {
- //ResourceBundle res = ResourceBundle.getBundle("Resources");
- public Menus()
- {
- setSize(500, 300);
- //String title = res.getString("TITLE");
- this.setTitle("APPLICATION TITLE");
- initComponents();
- }
- private void initComponents()
- {
- jmbarBarraDeMenus = new javax.swing.JMenuBar();
- jmnuArchivo = new javax.swing.JMenu();
- String fi = res.getString("FILE");
- jmnuArchivo.setText("FILE");
- //jmbarBarraDeMenus.add(jmnuArchivo);
- jmnuHelp = new javax.swing.JMenu();
- //String hel = res.getString("HELP");
- jmnuHelp.setText("HELP");
- jmbarBarraDeMenus.add(jmnuHelp);
- jmnuLang = new javax.swing.JMenu();
- //String lang = res.getString("LANGUAGE");
- jmnuLang.setText("ABOUT");
- jmbarBarraDeMenus.add(jmnuLang);
- jmnuAbrir = new javax.swing.JMenuItem();
- //String op = res.getString("OPEN");
- jmnuAbrir.setText("OPEN");
- jmnuArchivo.add(jmnuAbrir);
- jmnuConfigura = new javax.swing.JMenuItem();
- //String co = res.getString("CONFIGURATION");
- jmnuConfigura.setText("CONFIGURATION");
- jmnuArchivo.add(jmnuConfigura);
- jmnuConfigura.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- jMenuConf_actionPerformed(e);
- }
- });
- getContentPane().setLayout(null);
- addWindowListener(new java.awt.event.WindowAdapter()
- {
- public void windowClosing(java.awt.event.WindowEvent evt)
- {
- exitForm(evt);
- }
- });
- setJMenuBar(jmbarBarraDeMenus);
- }//GEN-END:initComponents
- /** Exit the Application */
- private void exitForm(java.awt.event.WindowEvent evt) {
- System.exit (0);
- }
- void jMenuConf_actionPerformed(ActionEvent e) {
- Selector dlg = new Selector();
- dlg.setVisible(true) ;
- }
- public static void main (String args[])
- {
- new Menus().setVisible(true);
- }
- private javax.swing.JMenuBar jmbarBarraDeMenus;
- public javax.swing.JMenu jmnuArchivo;
- private javax.swing.JMenu jmnuHelp;
- private javax.swing.JMenu jmnuLang;
- private javax.swing.JMenuItem jmnuAbrir;
- private javax.swing.JMenuItem jmnuConfigura;
- }