Search code examples
javaswinguser-interfaceswing-app-framework

Some doubts about Java SWING and Swing Application Framework


I have this simple Main class that use swing to show an Hello World label but I have some doubt about this code because this is my first time that I create GUI in Java:

import javax.swing.JLabel;

import org.jdesktop.application.SingleFrameApplication;

public class Main extends SingleFrameApplication {

    @Override
    protected void startup() {
        // TODO Auto-generated method stub
        show(new JLabel("Hello World"));
    }

    public static void main(String[] args) {
        Main a = new Main();
        a.startup();
    }

}

My doubts are:

  1. From what I have understand the JLabel() method is a pure Swing method that simply create a textual label showing the Hello World message. This code use also the startup() method that, from what I have understand, is a method of the **SingleFrameApplication class that belong to the so called Swing Application Framework ...but...what exactly is this Swing Application Framework ? Is it a separate project from Swing? What give me?

  2. When I run the application as a classic Java application the Hello World message is show but also appear to me the following error message in the Eclipse console:

set 23, 2013 12:35:37 PM org.jdesktop.application.ResourceManager getApplicationResourceMap Avvertenza: getApplicationResourceMap(): no Application class set 23, 2013 12:35:38 PM org.jdesktop.application.SingleFrameApplication initRootPaneContainer Avvertenza: couldn't restore session [mainFrame.session.xml] java.lang.NullPointerException at org.jdesktop.application.LocalStorage.getApplicationId(LocalStorage.java:254) at org.jdesktop.application.LocalStorage.getDirectory(LocalStorage.java:274) at org.jdesktop.application.LocalStorage$LocalFileIO.getFile(LocalStorage.java:450) at org.jdesktop.application.LocalStorage$LocalFileIO.openInputFile(LocalStorage.java:417) at org.jdesktop.application.LocalStorage.openInputFile(LocalStorage.java:68) at org.jdesktop.application.LocalStorage.load(LocalStorage.java:188) at org.jdesktop.application.SessionStorage.restore(SessionStorage.java:381) at org.jdesktop.application.SingleFrameApplication.initRootPaneContainer(SingleFrameApplication.java:210) at org.jdesktop.application.SingleFrameApplication.show(SingleFrameApplication.java:268) at Main.startup(Main.java:11) at Main.main(Main.java:19)

Why? What it exactly means?

Tnx

Andrea


Solution

  • To create and show a window with a Hello World label, you need to do the following at the minimum:

    • Instantiate a JFrame (link).
    • Add your JLabel to it.
    • Show the frame.

    The oracle tutorial on how to make frames (link) shows you exactly how to do that. For easy reference, I copied the specific excerpt that defines/shows your window from there.

    JFrame frame = new JFrame("FrameDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JLabel emptyLabel = new JLabel("");
    emptyLabel.setPreferredSize(new Dimension(175, 100));
    frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
    
    //Display the window.
    frame.pack();
    frame.setVisible(true);