Hi ,Today we are going to see how to load multiple languages in a swing application like , i have a JFrame
which contain JLabel , JRadioButton, JButton and other fields and i have three different labels say English,
Spanish,German on clicking on these respective labels there respective language should get loaded and the
Labels ,Radio buttons and other fields name and there text should get change accordingly in they JFrame.
We can do these by the help of Properties files.
Using of Properties files :
- A Properties file have an extension .properties and read and saved as .properties.
- A Properties file have values in the form of key & value pair ex: name="Jack"
How to load different Languages:
- For each Language we will use different Properties file example for english - english.properties similiar for others.
- We can use java.util.Properties class to load the properties and get the key and there respective value using the getProperty() method .
In this example , we are going to use the following Properties file:
english.properties
spanish.properties
german.properties
All these Properties files will have a key with the same name .
english .properties:
username=Username
password=Password
gender=Gender
male=Male
female=Female
age=Age
address=Address
submit=Submit
message=Successfully Submitted....
spanish.properties :
username=Nombre de usuario
password=contraseña
gender=género
male=macho
female=femenino
age=edad
address=dirección
submit=entregar
message=Presentada con éxito ...
german.properties :
username=Benutzername
password=Kennwort
gender=Geschlecht
male=männlich
female=weiblich
age=alter
address=Anschrift
submit=einreichen
message=Erfolgreich abgegeben ...
Now , we have the properties file ready with the key and pair value, Now we need to follow these steps:
Steps:
- Create a package called properties.
- Place all the Properties file inside this package , otherwise the properties file will not get loaded.
- Create a JFrame GUI window with some labels,textfields and buttons inside the properties package.
- After designing write a method call loadLang(String lang) which take the language as argument and loads the respective properties file.
- Write action events to load different properties.
/* For loading different Languages*/
private void loadLanguages(String lang) {
try
{
Properties p=new Properties();
p.load(loadProperties.class.getResourceAsStream(lang+".properties"));
String username=p.getProperty("username");
String password=p.getProperty("password");
String gender=p.getProperty("gender");
String male=p.getProperty("male");
String female=p.getProperty("female");
String age=p.getProperty("age");
String address=p.getProperty("address");
String submit=p.getProperty("submit");
message=p.getProperty("message");
/* Set the Values Here*/
jLabel2.setText(username);
jLabel3.setText(password);
jLabel4.setText(gender);
jRadioButton1.setText(male);
jRadioButton2.setText(female);
jLabel5.setText(age);
jLabel6.setText(address);
jButton1.setText(submit);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
Full Code:
package properties;
import java.awt.Color;
import java.util.Properties;
import javax.swing.JOptionPane;
public class Load extends javax.swing.JFrame {
String message="";
public Load() {
initComponents();
loadLanguages("english"); // Default load english Language
setSize(600,500);
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
jLabel1.setText("Example - Loading Mutilple Languages .....");
getContentPane().add(jLabel1);
jLabel1.setBounds(40, 10, 520, 40);
jLabel2.setText("jLabel2");
getContentPane().add(jLabel2);
jLabel2.setBounds(20, 100, 110, 20);
jLabel3.setText("jLabel3");
getContentPane().add(jLabel3);
jLabel3.setBounds(20, 140, 120, 20);
jLabel4.setText("jLabel4");
getContentPane().add(jLabel4);
jLabel4.setBounds(20, 180, 130, 20);
jLabel5.setText("jLabel5");
getContentPane().add(jLabel5);
jLabel5.setBounds(20, 220, 120, 30);
jLabel6.setText("jLabel6");
getContentPane().add(jLabel6);
jLabel6.setBounds(20, 260, 110, 30);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(200, 320, 160, 23);
jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel7.setForeground(new java.awt.Color(255, 0, 51));
jLabel7.setText("English");
jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel7MouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
jLabel7MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jLabel7MouseExited(evt);
}
});
getContentPane().add(jLabel7);
jLabel7.setBounds(40, 60, 50, 20);
jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel8.setForeground(new java.awt.Color(255, 0, 51));
jLabel8.setText("Spanish");
jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel8MouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
jLabel8MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jLabel8MouseExited(evt);
}
});
getContentPane().add(jLabel8);
jLabel8.setBounds(130, 60, 50, 20);
jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11));
jLabel9.setForeground(new java.awt.Color(255, 0, 51));
jLabel9.setText("German");
jLabel9.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel9MouseClicked(evt);
}
public void mouseEntered(java.awt.event.MouseEvent evt) {
jLabel9MouseEntered(evt);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
jLabel9MouseExited(evt);
}
});
getContentPane().add(jLabel9);
jLabel9.setBounds(230, 60, 90, 20);
jRadioButton1.setText("jRadioButton1");
getContentPane().add(jRadioButton1);
jRadioButton1.setBounds(140, 180, 130, 23);
jRadioButton2.setText("jRadioButton2");
getContentPane().add(jRadioButton2);
jRadioButton2.setBounds(280, 180, 150, 23);
getContentPane().add(jTextField1);
jTextField1.setBounds(140, 100, 230, 20);
getContentPane().add(jTextField2);
jTextField2.setBounds(140, 140, 230, 20);
getContentPane().add(jTextField3);
jTextField3.setBounds(140, 230, 230, 20);
getContentPane().add(jTextField4);
jTextField4.setBounds(140, 270, 230, 20);
pack();
}
private void jLabel7MouseEntered(java.awt.event.MouseEvent evt) {
jLabel7.setForeground(Color.BLUE);
}
private void jLabel7MouseExited(java.awt.event.MouseEvent evt) {
jLabel7.setForeground(Color.RED);
}
private void jLabel8MouseEntered(java.awt.event.MouseEvent evt) {jLabel8.setForeground(Color.BLUE);
}
private void jLabel8MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseExited
jLabel8.setForeground(Color.RED); // TODO add your handling code here:
}//GEN-LAST:event_jLabel8MouseExited
private void jLabel9MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel9MouseEntered
jLabel9.setForeground(Color.BLUE); // TODO add your handling code here:
}//GEN-LAST:event_jLabel9MouseEntered
private void jLabel9MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel9MouseExited
jLabel9.setForeground(Color.RED); // TODO add your handling code here:
}//GEN-LAST:event_jLabel9MouseExited
private void jLabel7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel7MouseClicked
loadLanguages("english");
// TODO add your handling code here:
}//GEN-LAST:event_jLabel7MouseClicked
private void jLabel9MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel9MouseClicked
loadLanguages("german"); // TODO add your handling code here:
}//GEN-LAST:event_jLabel9MouseClicked
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
JOptionPane.showMessageDialog(this, message);
// TODO add your handling code here:
}//GEN-LAST:event_jButton1ActionPerformed
private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseClicked
loadLanguages("spanish"); // TODO add your handling code here:
}//GEN-LAST:event_jLabel8MouseClicked
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Load.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Load.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Load.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Load.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Load().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
// End of variables declaration//GEN-END:variables
private void loadLanguages(String lang) {
try
{
Properties p=new Properties();
p.load(loadProperties.class.getResourceAsStream(lang+".properties"));
String username=p.getProperty("username");
String password=p.getProperty("password");
String gender=p.getProperty("gender");
String male=p.getProperty("male");
String female=p.getProperty("female");
String age=p.getProperty("age");
String address=p.getProperty("address");
String submit=p.getProperty("submit");
message=p.getProperty("message");
/* Set the Values Here*/
jLabel2.setText(username);
jLabel3.setText(password);
jLabel4.setText(gender);
jRadioButton1.setText(male);
jRadioButton2.setText(female);
jLabel5.setText(age);
jLabel6.setText(address);
jButton1.setText(submit);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}
Note : Place the properties file and Java file in the same folder here under properties folder(name of the package) otherwise it wont able to load the data from properties file.
Compile :
G:\> cd properties
G:\properties>javac *.java
G:\properties>cd..
Run :
G:\>java properties.Load
Result :
<< Prev Index
-----------------------------------------------------------------------------------------------------
blogs
Great Article
ReplyDeleteJava Web Services Online Training | Web Services Course | Web Services Training Courses | Java Web Services Training in Chennai | Online Java Training | Java Training in Chennai
Hi! If you need to localize your app or other piece of software, I would suggest using a collaborative translation management platform like POEditor
ReplyDeletehttps://saglamproxy.com
ReplyDeletemetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
0W3W