mercredi 26 août 2015

How to deploy JFrame in web page?

So I have done my research about deploying an applet or jframe in a web page. I have created my jar file and the html file to launch it. I get an error when trying to load the page that says Java Runtime Error java.lang.reflect.InvocationTargetException...I have searched this error and from what I understand it means that the class that I am calling is not an applet.

Here is some code snippets on what I am doing.

HTML file-

<!DOCTYPE html>
<html>
<head>
<title>Monitor</title>
</head>
<body>
<applet code="data.simulation.front.Main.class" archive="Monitor2.jar" height="844" width="1244"></applet>
</body>
</html>

And here is my Main class that has my main() function.

public class Main implements DBConstants {

// insets set the space between all the components
private static final Insets insets = new Insets(12, 12, 12, 12);
private static JPanel innerFrame; // panel that all sub panels are snapped
                                    // to
private static ColorFrame cFrame; // panel that displays line colors
private static VoltageFrame vFrame; // panel that displays the voltage graph
private static FrequencyFrame fFrame; // panel that displays the frequency
                                        // graph
private static DisturbanceFrame dFrame; // panel that displays the
                                        // disturbance frame
private static Toolbar toolbar; // contains all the JButtons
final static JFrame applicationFrame = new JFrame(); // application frame
                                                        // that contains
                                                        // innerFrame and
                                                        // toolbar

// global variables to open connection of db and keep open for duration of
// applet
// connection gets closed only when the frame is closed by the exit button
// will not close conn when end program is clicked from within eclipse
public static Connection dbConnection = null; //
public static PreparedStatement preparedStatement = null; //

/**
 * Main is the starting point of the application and will open a connection
 * to the database first and then set up the application frame which will
 * hold all of the inner components
 * 
 * @param args
 *            param args not used
 */
public void main(String[] args) {
    // start connection of database
    try {
        dbConnection = DriverManager.getConnection(DB_URL, USER, PASS);
        System.out.println("DB CONNECTION OPENED");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int toolsHeight = 25;
    int appWidth = 1244;
    int appHeight = toolsHeight + 819;

    applicationFrame.setPreferredSize(new Dimension(appWidth, appHeight));
    applicationFrame.setTitle("Phasor Measurement Unit Display");
    applicationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    applicationFrame.setResizable(true);
    applicationFrame.getContentPane().setLayout(new BorderLayout());

    toolbar = new Toolbar();
    Main.setInnerFrame(new JPanel());
    Main.getInnerFrame().setLayout(new GridBagLayout());
    Main.getInnerFrame().setBackground(new Color(190, 190, 190));

    applicationFrame.add(toolbar, BorderLayout.NORTH);

    cFrame = new ColorFrame(GridBagConstraints.NORTHEAST);
    vFrame = new VoltageFrame(GridBagConstraints.NORTHWEST);
    fFrame = new FrequencyFrame(GridBagConstraints.SOUTHWEST);
    dFrame = new DisturbanceFrame(GridBagConstraints.SOUTHEAST);

    applicationFrame.add(innerFrame, BorderLayout.CENTER);

    applicationFrame.pack();
    applicationFrame.setLocationRelativeTo(null);
    applicationFrame.setVisible(true);
    innerFrame.revalidate();
    applicationFrame.validate();
    applicationFrame.addWindowListener(new WindowAdapter() {
        /*
         * Window listener to listen to when the window is closed via the x
         * button. When the window is closed the connection the database is
         * also closed
         */
        public void windowClosing(WindowEvent evt) {
            if (dbConnection != null) {
                try {
                    dbConnection.close();
                    System.out.println("DB CONNECTION CLOSED");
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });
}

The class Main does not extend Applet but I have tried that with no luck. I have tried moving the jar and html file into my classes folder. I have tried to extend applet and change main() to start(). I have tried many other things to get this to work.

Any help or recommendations is much appreciated. This is my first question on SO so excuse me if it is not structured properly.

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire