Hey all,

I have a JApplet that runs in browser and I can't get it to create a new JFrame popup when a button is clicked. It works fine in my IDE (Eclipse) but not when I upload it. Here's the code for the JFrame I'm trying to create:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.jdesktop.swingx.*;

public class SchedWind {

    private String selectedDate;
    private PHPHandler phpMain;
    private ResponseObj[] serverPHPresponse;
    private final JLabel hotStatus;

    public SchedWind() {
        hotStatus = new JLabel("Waiting for user...");
        serverPHPresponse = null;
        selectedDate = null;
        phpMain = new PHPHandler();
        showGUI();
    }

    public void showGUI() {
        final JFrame frame = new JFrame("Schedule");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(false);
        GridBagLayout gLayout = new GridBagLayout();
        frame.setLayout(gLayout);
        GridBagConstraints c = new GridBagConstraints();

        hotStatus.setForeground(Color.BLUE);
        hotStatus.setVerticalAlignment(JLabel.CENTER);
        hotStatus.setHorizontalAlignment(JLabel.CENTER);

        JLabel selectDayLabel = new JLabel("Please select day:");
        selectDayLabel.setOpaque(false);
        selectDayLabel.setPreferredSize(new Dimension(170,30));
        selectDayLabel.setHorizontalAlignment(JLabel.CENTER);
        selectDayLabel.setVerticalAlignment(JLabel.CENTER);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        frame.add(selectDayLabel, c);

        final JXDatePicker datePicker = new JXDatePicker();
        datePicker.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                selectedDate = datePicker.getDate().toString();
                //System.out.println(selectedDate);
            }
        });
        c.gridx = 1;
        c.gridy = 0;
        frame.add(datePicker, c);

        JButton grabSched = new JButton("OK");
        c.gridx = 0;
        c.gridy = 1;
        c.anchor = GridBagConstraints.CENTER;
        c.insets = new Insets(20,10,0,0);
        grabSched.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(selectedDate == null) {
                    hotStatus.setText("Please enter a date.");
                    hotStatus.setForeground(Color.RED);
                }
                else {
                    hotStatus.setText("Contacting server...");
                    hotStatus.setForeground(Color.BLUE);
                    //System.out.println(selectedDate);
                    grabSchedFromServer();
                }
            }
        });
        frame.add(grabSched, c);

        JButton cancel = new JButton("Cancel");
        c.gridx = 1;
        c.gridy = 1;
        frame.add(cancel, c);
        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
            }
        });


        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 2;
        frame.add(hotStatus, c);

        frame.pack();
        frame.setVisible(true);
    }

    protected void grabSchedFromServer() {
        String[] conArr = selectedDate.split(" ");
        String mo = conArr[1];
        String con = conArr[5] + "-";
        if(mo.equals("Jan"))
            con += "01-";
        else if(mo.equals("Feb"))
            con += "02-";
        else if(mo.equals("Mar"))
            con += "03-";
        else if(mo.equals("Apr"))
            con += "04-";
        else if(mo.equals("May"))
            con += "05-";
        else if(mo.equals("Jun"))
            con += "06-";
        else if(mo.equals("Jul"))
            con += "07-";
        else if(mo.equals("Aug"))
            con += "08-";
        else if(mo.equals("Sep"))
            con += "09-";
        else if(mo.equals("Oct"))
            con += "10-";
        else if(mo.equals("Nov"))
            con += "11-";
        else if(mo.equals("Dec"))
            con += "12-";
        con += conArr[2];
        con += "@BREAK";
        con += con.substring(0,con.indexOf("@BREAK"));
        VideoDate sel = new VideoDate(con);
        serverPHPresponse = phpMain.sendPOSTReadRequest(sel);
        hotStatus.setText("Query successful!");
        hotStatus.setForeground(Color.GREEN);
        //System.out.println(serverPHPresponse[0].printPretty());
        if(serverPHPresponse[0].blank) {
            hotStatus.setText("No video requests were found for the selected day.");
            hotStatus.setForeground(Color.BLACK);
        }
        else {
            reportWind pop = new reportWind(serverPHPresponse);
            pop.toString();
        }
    }
}

And it's triggered by this in another class:

seeSched.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SchedWind main = new SchedWind();
                main.toString();
            }
        });

The thing is, if I try and get it to open an extremely simple JFrame with nothing in it, it works. Can anyone help?

Thanks!

Recommended Answers

All 18 Replies

Are there any error messages in the browser's java console?

None!

What is the difference between the JFrame that works and the one that doesn't?

Strange there is no error message for the one that does not work.

I got it! It had to do with the fact that I used an external library for the JXDatePicker and that wasn't exporting correctly into the JAR. Derp.

I am unsure about this, However I assume that you maybe to some extent using Entities that basically override the Java Runtime Environment default policy.

Try running the JFrame directly with security features.

java -Djava.security.manager Executable

If that works, then we can consider it to be other factors . However this is just a guess.

I'll try messing with the security, but this time I correctly packaged the library into the JAR and it still didn't work correctly. If it's any help, I'm trying to package in the SwingX libraries.

it still didn't work correctly

Can you explain what it does do? And how that differs from being "correct".

It works fine in Eclipse, but when I export it to a normal JAR file and upload it to my website and click the button to create the JFrame popup, nothing happens. No error, no exception, nothing. This happens even after I included the library in the JAR I created. If I don't reference SwingX (the library causing all this trouble) then it works fine.

I think I found the issue but I don't know what to do. When I click the button in my applet when it's in the web browser, I actually get this error in the java console:

Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError: org/jdesktop/swingx/JXDatePicker
    at VideoApplet$1.actionPerformed(VideoApplet.java:44)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.jdesktop.swingx.JXDatePicker
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 36 more

You need to put the class referenced in the error message on the classpath so the java program can find it.
The archive attribute of the <applet tag can take more than one jar file

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry exported="true" kind="lib" path="lib/swingx-all-1.6.3.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

That's the contents of my .classpath file right now, isn't swingx already included?

How does what you just posted relate to the applet tag in the html page the browser is reading?

It doesn't, I think the problem is that the java console itself can't find the correct class even though the JAR is in the class path. Regardless, here's the HTML code I use on the page containing the applet:

<body>
    <table width=100% height=100%>
    <tr>
    <td style="text-align: center; vertical-align: middle;">
    <object type="application/x-java-applet" height="600" width="800">
    <param name="code" value="VideoApplet" />
    <param name="archive" value="VideoApplet.jar" />
    <param name="archive" value="lib/swingx-all-1.6.3.jar" />
    Applet failed to run.  No Java plug-in was found!
    </object>
    </td>
    </tr>
    </table>
</body>

I tried adding the class but I still get this:

Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError: org/jdesktop/swingx/JXDatePicke

Try using the <applet tag.

Part of this project requires valid HTML5, and the applet tag is deprecated. Is there any other way?

Try using it only for testing to see if it works.

Same error. In console I get this:

Exception in thread "AWT-EventQueue-4" java.lang.NoClassDefFoundError: org/jdesktop/swingx/JXDatePicker

here's what my HTML file looks like:

<body>
    <table width=100% height=100%>
    <tr>
    <td style="text-align: center; vertical-align: middle;">



    <!--<object type="application/x-java-applet" height="600" width="800">
    <param name="code" value="VideoApplet" />
    <param name="archive" value="VideoApplet.jar" />
    Applet failed to run.  No Java plug-in was found!
    </object>-->



    <applet code="VideoApplet.class" width="800" height="600" archive="VideoApplet.jar", "swingx-all-1.6.3.jar">
    Java applet
    </applet>
    </td>
    </tr>
    </table>
</body>

Both VideoApplet.jar and swingx-all-1.6.3.jar are on my server in the same directory.

Try swapping the positions of the jar file references to see if the syntax for the archive attribute is correct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.