Results 1 to 18 of 18
Thread: Applet with swing componets
- 05-21-2012, 05:49 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Applet with swing componets
I'm tying to get to the basics of applets but I'm running in to some seemingly very basic issues here.
I made a new project normally.
My project name is MyApplet.
my package "myapplet" contains two files:
first is a JAppletForm called myAppletForm. It contains one button and one combobox for testing purposes.
It has this following code:
The other file is my Main.java.Java Code:package main; /** * * @author Aleksi */ public class myAppletForm extends javax.swing.JApplet { /** Initializes the applet myAppletForm */ public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JComboBox jComboBox1; // End of variables declaration }
That contains the following:
Java Code:package main; import javax.swing.*; public class Main extends JApplet { private static void showGUI(){ myAppletForm form = new myAppletForm(); form.setSize(500, 450); form.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable(){ public void run(){ showGUI(); } }); } }
The form I'm trying to open through the main method does not appear. However, when I click run on the myAppletForm it opens normally. What am I missing?Last edited by MonkeyMan; 05-21-2012 at 07:42 PM.
- 05-21-2012, 06:12 PM #2
Re: Applet with swing componets
Both of your classes extend JApplet, is that on purpose? An Applet in an Applet?
- 05-21-2012, 06:15 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Applet with swing componets
You can't run applets outside of a container (ie browser or somesuch).
Please do not ask for code as refusal often offends.
- 05-21-2012, 06:25 PM #4
Re: Applet with swing componets
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-21-2012, 06:32 PM #5
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
- 05-21-2012, 06:34 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
- 05-21-2012, 06:38 PM #7
Re: Applet with swing componets
The answer to that encompasses more than can fit in a forum thread. Go through the Lesson: Java Applets (The Java™ Tutorials > Deployment)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-21-2012, 07:25 PM #8
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Re: Applet with swing componets
Alright according to those instructions "Launching without JNLP" I made an html file like this:
<html>
<body>
<applet code = "Main.class" archive = 'MyApplet.jar' width = "300" height = "300"/>
</body>
</html>
to the build directory where all the classes are and pasted the MyApplet jar there as well. Running the html file turns error:
Java Plug-in 1.6.0_31
Using JRE version 1.6.0_31-b05 Java HotSpot(TM) Client VM
User home directory = C:\Users\Aleksi
----------------------------------------------------
c: clear console window
f: finalize objects on finalization queue
g: garbage collect
h: display this help message
l: dump classloader list
m: print memory usage
o: trigger logging
q: hide console
r: reload policy configuration
s: dump system and deployment properties
t: dump thread list
v: dump thread stack
x: clear classloader cache
0-5: set trace level to <n>
----------------------------------------------------
java.lang.NoClassDefFoundError: Main (wrong name: Main/Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NoClassDefFoundError: Main (wrong name: Main/Main)
java.lang.NoClassDefFoundError: myAppletForm (wrong name: Main/myAppletForm)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NoClassDefFoundError: myAppletForm (wrong name: Main/myAppletForm)
java.lang.NoClassDefFoundError: Main (wrong name: Main/Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknow n Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.NoClassDefFoundError: Main (wrong name: Main/Main)
So there's still something very wrong here.
- 05-21-2012, 07:34 PM #9
Re: Applet with swing componets
It looks like Your class is in the package: Main. The full classname is: Main.Mainjava.lang.NoClassDefFoundError: Main (wrong name: Main/Main)
Why does the posted code have: package myapplet;If you don't understand my response, don't ignore it, ask a question.
- 05-21-2012, 07:41 PM #10
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
- 05-21-2012, 07:44 PM #11
Re: Applet with swing componets
You need to change where you reference the classname to use the correct classname that includes the package.
If you don't understand my response, don't ignore it, ask a question.
- 05-21-2012, 08:27 PM #12
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Re: Applet with swing componets
I tried to change the html file to say:
<html>
<body>
<applet code = "Main.Main" archive = "MyApplet.jar" width = "300" height = "300"/>
</body>
</html>
Like the instructions I was given. It seperated package and class by dot like that. However, I receive the following:
load: class Main.Main not found.
java.lang.ClassNotFoundException: Main.Main
at sun.plugin2.applet.Applet2ClassLoader.findClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(U nknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Un known Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception: java.lang.ClassNotFoundException: Main.Main
- 05-21-2012, 08:33 PM #13
Re: Applet with swing componets
You're a little sloppy with the case of the names:
package main;
code = "Main.Main"
Look in the jar file with a zip utility and see where the class files are located and how they are named.The correct package n class are indeed main and main.
They must be in a folder with the name of the package.If you don't understand my response, don't ignore it, ask a question.
- 05-21-2012, 08:52 PM #14
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Re: Applet with swing componets
Grr I refactored the package back to "myapplet" to avoid all the main confusion...
The jar file now contains myapplet folder under which are classes Main and myAppletForm
the path I'm using for the index.html is: C:\Users\Aleksi\Documents\NetBeansProjects\MyApple t\build\classes\myapplet
That path now contains the html file, MyApplet.jar file, Main.class and myAppletForm.class.
the html file now says:
<html>
<body>
<applet code = myapplet.Main archive = MyApplet.jar width = 300 height = 300/>
</body>
</html>
error I get now:
java.lang.reflect.InvocationTargetException
at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Un known Source)
at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: myapplet.Main cannot be cast to java.applet.Applet
at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception: java.lang.reflect.InvocationTargetException
- 05-21-2012, 08:57 PM #15
Re: Applet with swing componets
Does myapplet.Main extend the (J)Applet class?ClassCastException: myapplet.Main cannot be cast to java.applet.AppletIf you don't understand my response, don't ignore it, ask a question.
- 05-21-2012, 09:28 PM #16
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Re: Applet with swing componets
No, it doesn't. The extension is in the JAppletForm I'm creating a new out of in the Main class. To avoid the applet within an applet situation quad64 asked me of, removing the extensions from the JAppletForm obviously breaks the inherit JAppletForm initComponents method.
But having both of em extend helps none. Still says:
java.lang.reflect.InvocationTargetException
at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Un known Source)
at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
at sun.plugin2.applet.Plugin2Manager.createApplet(Unk nown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionR unnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: myapplet.Main cannot be cast to java.applet.Applet
at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(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.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception: java.lang.reflect.InvocationTargetException
- 05-21-2012, 09:30 PM #17
Re: Applet with swing componets
The code=class should extend the applet class
If you don't understand my response, don't ignore it, ask a question.
- 05-22-2012, 10:05 AM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Applet with swing componets
You need to decide what you are trying to run.
If it's an applet then, as Norm says, the main class needs to extend Applet or JApplet.
Those are the entry point classes for an applet (they don't use main()).
If it's a desktop Swing application then you don't use (J)Applet, but will need to build your app via a JFrame.
That will use a main() method as an entry point.
At the moment you are munging the two things together and, not surprisingly, it's not working.
You need to go through that tutorial that Darryl linked to above, where all this is explained.Please do not ask for code as refusal often offends.
Similar Threads
-
game componets
By PRW56 in forum New To JavaReplies: 20Last Post: 04-06-2012, 08:38 AM -
How can I to print (printer) a JPanel and its componets (buttons and labels)?
By mad72584 in forum New To JavaReplies: 3Last Post: 08-15-2011, 01:56 PM -
Swing applet problem
By link6790 in forum New To JavaReplies: 9Last Post: 05-24-2011, 02:31 PM -
Snowman Applet - SWING
By Angry@Java in forum New To JavaReplies: 1Last Post: 05-05-2011, 02:19 AM -
Change Applet to Swing
By aryowap in forum AWT / SwingReplies: 4Last Post: 07-08-2010, 09:03 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks