Results 1 to 10 of 10
Thread: What's wrong with this applet?
- 11-22-2009, 04:28 PM #1
Member
- Join Date
- Oct 2009
- Location
- Rotterdam
- Posts
- 52
- Rep Power
- 0
What's wrong with this applet?
I just made my first applet. The purpose of it is to show a color map of red horizontally and blue vertically.
It compiles, but it doesn't work on a html page or a browser.
Anyone know what's wrong with it?
Java Code:import java.awt.*; import javax.swing.*; class colors extends JApplet { public void init() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(64, 64)); for (short i = 0; i < 0x100; i += 4) { for (short j = 0; j < 0x100; j += 4) { JPanel subpanel = new JPanel(); subpanel.setBackground(new Color(j, 0, i)); panel.add(subpanel); } } getContentPane().add(panel); } }
-
You may have a Swing threading issue here. What if you use SwingUtiltities.invokeAndWait to try to create the applet on the EDT (event dispatch thread) as I believe is recommended in the tutorials?
Java Code:import java.awt.Color; import java.awt.GridLayout; import javax.swing.JApplet; import javax.swing.JPanel; public class Colors2 extends JApplet { public void init() { try { javax.swing.SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { System.err.println("createGUI didn't successfully complete"); } } private void createGUI() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(64, 64)); for (short i = 0; i < 0x100; i += 4) { for (short j = 0; j < 0x100; j += 4) { JPanel subpanel = new JPanel(); subpanel.setBackground(new Color(j, 0, i)); panel.add(subpanel); } } getContentPane().add(panel); } }
- 11-22-2009, 04:47 PM #3
Member
- Join Date
- Oct 2009
- Location
- Rotterdam
- Posts
- 52
- Rep Power
- 0
No it still doesn't work.
Hope it's not my PC!
-
How does it "not work"? What specifically goes wrong?
- 11-22-2009, 04:57 PM #5
Member
- Join Date
- Oct 2009
- Location
- Rotterdam
- Posts
- 52
- Rep Power
- 0
The java console on my browser gives the following message, but I can't decode it:
When I run AppletViewer, it simply does nothing.Java Code:java.lang.reflect.InvocationTargetException at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source) at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source) at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source) at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.RuntimeException: java.lang.IllegalAccessException: Class sun.plugin2.applet.Plugin2Manager$12 can not access a member of class colors with modifiers "" at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(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.IllegalAccessException: Class sun.plugin2.applet.Plugin2Manager$12 can not access a member of class colors with modifiers "" at sun.reflect.Reflection.ensureMemberAccess(Unknown Source) at java.lang.Class.newInstance0(Unknown Source) at java.lang.Class.newInstance(Unknown Source) ... 9 more Exception: java.lang.reflect.InvocationTargetException java.lang.NullPointerException at sun.plugin2.applet.Plugin2Manager.findAppletJDKLevel(Unknown Source) at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source) at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Exception: java.lang.NullPointerException
-
Are you absolutely 100% sure that this error is coming from my code above? That your HTML is requesting my class? Please double check since my changes solved this very error that I was getting with your code.
-
For more information on why invokeAndWait is important here, please see this tutorial:
Concurrency in Swing
On the second page, you'll find this:
In an applet, the GUI-creation task must be launched from the init method using invokeAndWait; otherwise, init may return before the GUI is created, which may cause problems for a web browser launching an applet. In any other kind of program, scheduling the GUI-creation task is usually the last thing the initial thread does, so it doesn't matter whether it uses invokeLater or invokeAndWait.
- 11-22-2009, 05:12 PM #8
Member
- Join Date
- Oct 2009
- Location
- Rotterdam
- Posts
- 52
- Rep Power
- 0
-
It shouldn't. It should create a Colors2$1.class instead. This is for the anonymous inner class that I create with the new Runnable().... statement.
- 11-22-2009, 05:21 PM #10
Member
- Join Date
- Oct 2009
- Location
- Rotterdam
- Posts
- 52
- Rep Power
- 0
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 2Last Post: 11-22-2009, 03:48 PM -
Pass parameter from one applet to another applet in different webpages...
By anubhavranjan in forum Java AppletsReplies: 2Last Post: 09-29-2009, 03:33 PM -
What am I doing wrong??
By NoNickName in forum New To JavaReplies: 3Last Post: 04-23-2009, 11:04 PM -
Calling another applet on click of button in one applet
By niteshwar.bhardwaj in forum Java 2DReplies: 1Last Post: 02-19-2009, 12:54 PM -
Applet, To center text and To open I engage in a dialog in an Applet
By Marcus in forum Java AppletsReplies: 4Last Post: 06-08-2007, 06:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks