By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,977 Members | 1,352 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

How to set language in other page once we did the changes in main page

P: 2
Hi im newbie to java, and i want to set the language change according to main page..how to create it?

here is my coding:

main page:


Expand|Select|Wrap|Line Numbers
  1.  import org.eclipse.swt.SWT;
  2. import org.eclipse.swt.widgets.Button;
  3. import org.eclipse.swt.widgets.Display;
  4. import org.eclipse.swt.widgets.Label;
  5. import org.eclipse.swt.widgets.Shell;
  6. import org.eclipse.swt.widgets.Text;
  7. import org.eclipse.swt.events.SelectionAdapter;
  8. import org.eclipse.swt.events.SelectionEvent;
  9.  
  10.  
  11. public class Trial {
  12.  
  13.     private static Text text;
  14.  
  15.     static String main = "main";
  16.     static String jadual = "jadual";
  17.     static String tarikh = "tarikh";
  18.     static String cari = "cari";
  19.  
  20.  
  21.     public static void main(String[] args) {
  22.         final Local local = new Local();
  23.         final Display display = Display.getDefault();
  24.         final Shell shell = new Shell();
  25.         shell.setSize(500, 375);
  26.         shell.open();
  27.         shell.setText(("")+local.bahasa(main));
  28.  
  29.         final Button pressButton = new Button(shell, SWT.NONE);
  30.         pressButton.setText(("")+local.bahasa(jadual));
  31.         pressButton.setBounds(64, 63, 77, 34);
  32.         pressButton.addSelectionListener(new SelectionAdapter() {
  33.             public void widgetSelected(SelectionEvent e) {
  34.  
  35.                         Trial2 trial = new Trial2();
  36.                         trial.run(display);
  37.                     }}
  38.                     );
  39.  
  40.         final Label tarikhLabel = new Label(shell, SWT.CENTER);
  41.         tarikhLabel.setText(("")+local.bahasa(tarikh));
  42.         tarikhLabel.setBounds(44, 197, 49, 22);
  43.  
  44.         text = new Text(shell, SWT.BORDER);
  45.         text.setBounds(99, 194, 80, 25);
  46.  
  47.  
  48.         final Button cariButton = new Button(shell, SWT.NONE);
  49.         cariButton.setText(("")+local.bahasa(cari));
  50.         cariButton.setBounds(204, 197, 44, 23);
  51.         cariButton.addSelectionListener(new SelectionAdapter() {
  52.             public void widgetSelected(SelectionEvent e) {
  53.                 Kalender2 kal = new Kalender2();
  54.                 kal.run(display);
  55.                 text.setText(""+kal.open());        
  56.                     }}
  57.                     );
  58.  
  59.         final Button bmButton = new Button(shell, SWT.RADIO);
  60.         bmButton.setText("bm");
  61.         bmButton.setBounds(75, 273, 83, 16);
  62.         bmButton.setSelection(true);
  63.         bmButton.addSelectionListener(new SelectionAdapter() {
  64.             public void widgetSelected(SelectionEvent e) {
  65.  
  66.                 shell.setText(("")+local.bahasa(main));
  67.                 pressButton.setText(("")+local.bahasa(jadual));
  68.                 tarikhLabel.setText((""+local.bahasa(tarikh)));
  69.                 cariButton.setText(("")+local.bahasa(cari));
  70.             }
  71.         }
  72.         );    
  73.  
  74.  
  75.  
  76.         final Button enButton = new Button(shell, SWT.RADIO);
  77.         enButton.setText("en");
  78.         enButton.setBounds(230, 273, 83, 16);
  79.         enButton.addSelectionListener(new SelectionAdapter() {
  80.             public void widgetSelected(SelectionEvent e) {
  81.  
  82.                 shell.setText(("")+local.english(main));
  83.                 pressButton.setText(("")+local.english(jadual));
  84.                 tarikhLabel.setText((""+local.english(tarikh)));
  85.                 cariButton.setText(("")+local.english(cari));
  86.             }
  87.         }
  88.         );
  89.  
  90.  
  91.  
  92.         shell.layout();
  93.         while (!shell.isDisposed()) {
  94.             if (!display.readAndDispatch())
  95.                 display.sleep();
  96.         }
  97.     }
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. the 2nd page:
  106.  
  107. import org.eclipse.swt.widgets.Display;
  108. import org.eclipse.swt.widgets.Shell;
  109. import org.eclipse.swt.SWT;
  110. import org.eclipse.swt.events.SelectionAdapter;
  111. import org.eclipse.swt.events.SelectionEvent;
  112. import org.eclipse.swt.widgets.Button;
  113.  
  114.  
  115. public class Trial2 {
  116.  
  117.     public static Display display2;
  118.  
  119.     static String main2 = "main2";
  120.     static String tutup = "tutup";
  121.  
  122.     static Local local = new Local();
  123.     static Trial trial = new Trial();
  124.  
  125.     public void run(Display display2){
  126.  
  127.         final Shell shell = new Shell();
  128.         shell.setSize(500, 375);
  129.  
  130.         //I want to set the language change according to first page 
  131.         shell.setText(("")+);
  132.  
  133.  
  134.         shell.open();
  135.  
  136.         final Button closeButton = new Button(shell, SWT.NONE);
  137.         closeButton.setText(("")+local.bahasa(tutup));
  138.         closeButton.setBounds(176, 246, 44, 23);
  139.         closeButton.addSelectionListener(new SelectionAdapter() {
  140.             public void widgetSelected(SelectionEvent e) {
  141.                   shell.close();
  142.  
  143.  
  144.                     }}
  145.                     );
  146.  
  147.         shell.layout();
  148.         while (!shell.isDisposed()) {
  149.             if (!display2.readAndDispatch())
  150.                 display2.sleep();
  151.         }
  152.  
  153.     }    
  154.  
  155. }
  156.  
  157.  
  158.  
  159. Class for setting language:
  160.  
  161. Local.java
  162.  
  163. import java.util.*;
  164.  
  165.  
  166. public class Local {
  167.  
  168.  
  169.     static Locale currentLocale;
  170.     static ResourceBundle messages;
  171.  
  172.  
  173.  
  174.     public Object bahasa(String key){
  175.          String p = key;
  176.         currentLocale = new Locale("bm", "MS");
  177.          messages = ResourceBundle.getBundle("MessagesBundle",currentLocale);
  178.         p=(String) messages.getString(key);
  179.          //messages.getString("main");
  180.          return p;
  181.     }             
  182.  
  183.  
  184.      public Object english(String key){
  185.          String en = key;
  186.          currentLocale = new Locale("en","USA");
  187.          messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
  188.         en = (String) messages.getString(key);
  189.          return en;
  190.      }            
  191.  
  192.  
  193. }
  194.  
  195.  
now i want to mention the changes in "shell.setText()" in Trial2.java. how to set it.Thanks in advance
Sep 26 '07 #1
Share this Question
Share on Google+
2 Replies


Nepomuk
Expert 2.5K+
P: 3,112
Hi im newbie to java, and i want to set the language change according to main page..how to create it?

here is my coding:

main page:
Expand|Select|Wrap|Line Numbers
  1.  import org.eclipse.swt.SWT;
  2. import org.eclipse.swt.widgets.Button;
  3. import org.eclipse.swt.widgets.Display;
  4. import org.eclipse.swt.widgets.Label;
  5. import org.eclipse.swt.widgets.Shell;
  6. import org.eclipse.swt.widgets.Text;
  7. import org.eclipse.swt.events.SelectionAdapter;
  8. import org.eclipse.swt.events.SelectionEvent;
  9.  
  10.  
  11. public class Trial {
  12.  
  13.     private static Text text;
  14.  
  15.     static String main = "main";
  16.     static String jadual = "jadual";
  17.     static String tarikh = "tarikh";
  18.     static String cari = "cari";
  19.  
  20.  
  21.     public static void main(String[] args) {
  22.         final Local local = new Local();
  23.         final Display display = Display.getDefault();
  24.         final Shell shell = new Shell();
  25.         shell.setSize(500, 375);
  26.         shell.open();
  27.         shell.setText(("")+local.bahasa(main));
  28.  
  29.         final Button pressButton = new Button(shell, SWT.NONE);
  30.         pressButton.setText(("")+local.bahasa(jadual));
  31.         pressButton.setBounds(64, 63, 77, 34);
  32.         pressButton.addSelectionListener(new SelectionAdapter() {
  33.             public void widgetSelected(SelectionEvent e) {
  34.  
  35.                         Trial2 trial = new Trial2();
  36.                         trial.run(display);
  37.                     }}
  38.                     );
  39.  
  40.         final Label tarikhLabel = new Label(shell, SWT.CENTER);
  41.         tarikhLabel.setText(("")+local.bahasa(tarikh));
  42.         tarikhLabel.setBounds(44, 197, 49, 22);
  43.  
  44.         text = new Text(shell, SWT.BORDER);
  45.         text.setBounds(99, 194, 80, 25);
  46.  
  47.  
  48.         final Button cariButton = new Button(shell, SWT.NONE);
  49.         cariButton.setText(("")+local.bahasa(cari));
  50.         cariButton.setBounds(204, 197, 44, 23);
  51.         cariButton.addSelectionListener(new SelectionAdapter() {
  52.             public void widgetSelected(SelectionEvent e) {
  53.                 Kalender2 kal = new Kalender2();
  54.                 kal.run(display);
  55.                 text.setText(""+kal.open());        
  56.                     }}
  57.                     );
  58.  
  59.         final Button bmButton = new Button(shell, SWT.RADIO);
  60.         bmButton.setText("bm");
  61.         bmButton.setBounds(75, 273, 83, 16);
  62.         bmButton.setSelection(true);
  63.         bmButton.addSelectionListener(new SelectionAdapter() {
  64.             public void widgetSelected(SelectionEvent e) {
  65.  
  66.                 shell.setText(("")+local.bahasa(main));
  67.                 pressButton.setText(("")+local.bahasa(jadual));
  68.                 tarikhLabel.setText((""+local.bahasa(tarikh)));
  69.                 cariButton.setText(("")+local.bahasa(cari));
  70.             }
  71.         }
  72.         );    
  73.  
  74.  
  75.  
  76.         final Button enButton = new Button(shell, SWT.RADIO);
  77.         enButton.setText("en");
  78.         enButton.setBounds(230, 273, 83, 16);
  79.         enButton.addSelectionListener(new SelectionAdapter() {
  80.             public void widgetSelected(SelectionEvent e) {
  81.  
  82.                 shell.setText(("")+local.english(main));
  83.                 pressButton.setText(("")+local.english(jadual));
  84.                 tarikhLabel.setText((""+local.english(tarikh)));
  85.                 cariButton.setText(("")+local.english(cari));
  86.             }
  87.         }
  88.         );
  89.  
  90.  
  91.  
  92.         shell.layout();
  93.         while (!shell.isDisposed()) {
  94.             if (!display.readAndDispatch())
  95.                 display.sleep();
  96.         }
  97.     }
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. the 2nd page:
  106.  
  107. import org.eclipse.swt.widgets.Display;
  108. import org.eclipse.swt.widgets.Shell;
  109. import org.eclipse.swt.SWT;
  110. import org.eclipse.swt.events.SelectionAdapter;
  111. import org.eclipse.swt.events.SelectionEvent;
  112. import org.eclipse.swt.widgets.Button;
  113.  
  114.  
  115. public class Trial2 {
  116.  
  117.     public static Display display2;
  118.  
  119.     static String main2 = "main2";
  120.     static String tutup = "tutup";
  121.  
  122.     static Local local = new Local();
  123.     static Trial trial = new Trial();
  124.  
  125.     public void run(Display display2){
  126.  
  127.         final Shell shell = new Shell();
  128.         shell.setSize(500, 375);
  129.  
  130.         //I want to set the language change according to first page 
  131.         shell.setText(("")+);
  132.  
  133.  
  134.         shell.open();
  135.  
  136.         final Button closeButton = new Button(shell, SWT.NONE);
  137.         closeButton.setText(("")+local.bahasa(tutup));
  138.         closeButton.setBounds(176, 246, 44, 23);
  139.         closeButton.addSelectionListener(new SelectionAdapter() {
  140.             public void widgetSelected(SelectionEvent e) {
  141.                   shell.close();
  142.  
  143.  
  144.                     }}
  145.                     );
  146.  
  147.         shell.layout();
  148.         while (!shell.isDisposed()) {
  149.             if (!display2.readAndDispatch())
  150.                 display2.sleep();
  151.         }
  152.  
  153.     }    
  154.  
  155. }
  156.  
  157.  
  158.  
  159. Class for setting language:
  160.  
  161. Local.java
  162.  
  163. import java.util.*;
  164.  
  165.  
  166. public class Local {
  167.  
  168.  
  169.     static Locale currentLocale;
  170.     static ResourceBundle messages;
  171.  
  172.  
  173.  
  174.     public Object bahasa(String key){
  175.          String p = key;
  176.         currentLocale = new Locale("bm", "MS");
  177.          messages = ResourceBundle.getBundle("MessagesBundle",currentLocale);
  178.         p=(String) messages.getString(key);
  179.          //messages.getString("main");
  180.          return p;
  181.     }             
  182.  
  183.  
  184.      public Object english(String key){
  185.          String en = key;
  186.          currentLocale = new Locale("en","USA");
  187.          messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
  188.         en = (String) messages.getString(key);
  189.          return en;
  190.      }            
  191.  
  192.  
  193. }
  194.  
  195.  
now i want to mention the changes in "shell.setText()" in Trial2.java. how to set it.Thanks in advance
Hi erag! Welcome to TSDN!

How about adding a setLanguage method like this:
Expand|Select|Wrap|Line Numbers
  1. public class Trial2 {
  2. private String language = "english";
  3. ...
  4. public void setLanguage(String language)
  5. {
  6.    this.language = language;
  7. }
  8.  
  9. public void run(Display display2){
  10. ...
  11.    //I want to set the language change according to first page 
  12.    if (language.equals("english"))
  13.       shell.setText("Hello!");
  14.    else if (language.equals("german"))
  15.       shell.setText("Hallo!");
  16.    else if (language.equals("french"))
  17.       shell.setText("Bonjour!");
  18.    else ...
  19. ...
  20. }
  21.  
You could, if you don't have many languages, use a char to define the language and use switch to set the Text correctly (because char is a basic Type, so you can use switch, but String isn't).

Greetings,
Nepomuk
Sep 26 '07 #2

P: 2
Thanks nepomuk but i still have problems....the thing is i want to know whether if i select a button from one class file n want to get that selection for the other class file, can do like that? it is because when i run the application i want to set the language using button in main class n when i open the other class, it automatically
set language followed by the button selection from other class..
can i do that in java swt? do i hv to import any library?
PLEASE answer coz i have no idea at all....THANKS....
Sep 27 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.