Hello World in SwingME

Here is a hello world app written in Swing and SwingME side by side, as you can see they are almost the same

Hello World Swing Hello World SwingME
  1 package hello.world;
  2 
  3 import java.awt.event.ActionListener;
  4 import javax.swing.UIManager;
  5 import javax.swing.JButton;
  6 import javax.swing.JFrame;
  7 import javax.swing.JLabel;
  8 import javax.swing.JPanel;
  9 import java.awt.event.KeyEvent;
 10 import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
 11 import java.awt.BorderLayout;
 12 import java.awt.event.ActionEvent;
 13 
 14 /**
 15  * @author Yura Mamyrin
 16  */
 17 public class HelloWorld {
 18 
 19     public static void main(String[] args) throws Exception {
 20 
 21         UIManager.setLookAndFeel( new NimbusLookAndFeel() );
 22 
 23         JFrame frame = new JFrame("Swing app");
 24 
 25         frame.getContentPane().add( new JLabel("Hello World") );
 26 
 27         JButton exit = new JButton("Exit");
 28         exit.addActionListener( new ActionListener() {
 29             public void actionPerformed(ActionEvent e) {
 30                 System.exit(0);
 31             }
 32         });
 33         exit.setMnemonic( KeyEvent.VK_X ); // press Alt+X to exit
 34 
 35         JPanel panel = new JPanel();
 36         panel.add(exit);
 37         frame.getContentPane().add( panel, BorderLayout.SOUTH );
 38 
 39         //frame.pack(); // shrink to the smallest size
 40         //frame.setLocationRelativeTo(null); // center
 41 
 42         frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
 43         frame.setVisible(true);
 44     }
 45 }
 46 
  1 package hello.world;
  2 
  3 import net.yura.mobile.gui.ActionListener;
  4 import net.yura.mobile.gui.DesktopPane;
  5 import net.yura.mobile.gui.components.Button;
  6 import net.yura.mobile.gui.components.Frame;
  7 import net.yura.mobile.gui.components.Label;
  8 import net.yura.mobile.gui.components.Panel;
  9 import net.yura.mobile.gui.KeyEvent;
 10 import net.yura.mobile.gui.plaf.nimbus.NimbusLookAndFeel;
 11 import javax.microedition.lcdui.Graphics;
 12 import net.yura.mobile.gui.Midlet;
 13 
 14 /**
 15  * @author Yura Mamyrin
 16  */
 17 public class HelloWorld extends Midlet {
 18 
 19     protected void initialize(DesktopPane dp) {
 20 
 21         dp.setLookAndFeel( new NimbusLookAndFeel() );
 22 
 23         Frame frame = new Frame("SwingME app");
 24 
 25         frame.getContentPane().add( new Label("Hello World") );
 26 
 27         Button exit = new Button("Exit");
 28         exit.addActionListener(new ActionListener() {
 29             public void actionPerformed(String arg0) {
 30                 Midlet.exit();
 31             }
 32         });
 33         exit.setMnemonic( KeyEvent.KEY_END ); // press Esc to exit
 34 
 35         Panel panel = new Panel();
 36         panel.add(exit);
 37         frame.getContentPane().add( panel, Graphics.BOTTOM );
 38 
 39         //frame.pack(); // shrink to the smallest size
 40         //frame.setLocationRelativeTo(null); // center
 41 
 42         frame.setMaximum(true);
 43         frame.setVisible(true);
 44     }
 45 }
 46 

Now lets run this. for J2ME you will need to setup a J2ME project in Netbeans, please read the J2ME Setup Guide to see how to do this.

Here is what it looks like:

Hello World Swing Hello World SwingME

SwingME app on Java SE

Now lets run in ME4SE so we can use the same SwingME code to run on Desktops. You can find the "midletrunner.jar" inside the lib dir of the SwingMETest project.

java -cp lib/midletrunner.jar;dist/HelloWorld.jar org.me4se.MIDletRunner -width 200 -height 100 hello.world.HelloWorld
Hello World Java SE

SwingME app on Android

Please read the Android Setup Guide to see how to build a SwingME project with Android Eclipse IDE.

After you have followed those steps you should get something like this:

Hello World Android

Android Native Look And Feel in SwingME

And now lets try the native android Look and Feel, add this code to where you set your LookAndFeel

         try {
             dp.setLookAndFeel( (LookAndFeel)Class.forName("net.yura.android.plaf.AndroidLookAndFeel").newInstance() );
         }
         catch (Exception ex) {
             dp.setLookAndFeel( new NimbusLookAndFeel() );
         }

Here is what it looks like:

Hello World Android Native