0

I installed the WindowBuilder Plugin for Eclipse 3.7.

I created a very simple GUI with various methods (JFrame, JPanel, Application etc.). I tried to Export my Project as a runnable JAR / a .jar file. I have to choose the Main Class: It does not show anything to select! The Export still works but the file is useless.

How do I export my GUI class properly?

EDIT: Seems I need a main method to make the class show up in the main class list. But the problem is, that WindowBuilder creates a class like this:

import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class myGuiClass extends JApplet {

    /**
     * Create the applet.
     */
    public myGuiClass() {
        getContentPane().setLayout(null);

        JButton btnMybutton = new JButton("myButton");
        btnMybutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                System.out.println("Button pressed!");
            }
        });
        btnMybutton.setBounds(10, 11, 91, 23);
        getContentPane().add(btnMybutton);

    }
}

If I create a main method here, I cannot create my JApplet, as it it not allowed to reference its non-static constructor in the static main method. At least I dont know how to do it. So how would I go about exporting this very simple one class program to a runnable .jar or an applet?

@Andrew: The "Application" meant one of the options in the WindowBuilder->Swing Designer menu. which is in fact called Application Window, and there are also JApplet, JFrame, JDialog and so on. I would like to have something executable that I can bring to another PC or run via web, so I dont have to deal with crude executing things with the cmd window...

2
  • 2
    Does any class have a public static main(String[] args) method? Also, what is an Application? It is not part of the J2SE. BTW - figure this out before considering an applet. Applets are usually not a solution to a problem, but instead introduce more problems. May 31, 2012 at 0:53
  • Hey thanks for your answer. In fact I tried it with just one class, and no it does not have a main method. It seems to need one to show up in the main class list, but there is a problem, I have edited the question above ->
    – br0t
    May 31, 2012 at 10:42

2 Answers 2

3

Try this, this is the way to create a jar or runnable jar in eclipse, all your external libraries in the project will be included

File -> Export-> Java ->Runnbale JAR file

Launch configuration : your Class containing the public static void main(String[] args)

Export destination : Target place

Library Handling:

Package required libraries into generated JAR

FINISH
1
  • Package required libraries into generated JAR Ahh.. 'Fat Jar' (in principle, if not trade name). Not very optimal for a JWS deployed application (which is becoming more common, even for embedded applets). Jun 1, 2012 at 4:36
1

Try this code - works as either applet or free floating (frame based) app.

import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

/** This is an hybrid application/applet.  It can be run
 * as either a JFrame or JApplet. */
public class MyGuiApp extends JApplet {

    /** Construct the GUI for an applet. */
    public void init() {
        try {
            SwingUtilities.invokeAndWait( new Runnable() {
                public void run() {
                    GUI gui = new GUI();
                    getContentPane().add(gui.getGui());
                }
            });
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /** Launch the GUI in a frame. */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override 
            public void run() {
                JFrame f = new JFrame("My GUI");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                GUI gui = new GUI();
                f.getContentPane().add(gui.getGui());
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        });
    }
}

class GUI {

    public JComponent getGui() {
        // Use layouts! ..
        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEADING));
        // ..possibly with borders.
        p.setBorder(new EmptyBorder(10,10,10,10));

        JButton btnMybutton = new JButton("myButton");
        btnMybutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                System.out.println("Button pressed!");
            }
        });
        p.add(btnMybutton);
        return p;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.