Recently I was visiting a friend and we talked about doing a web application. We took a look at some sample sites and some templates for inspiration. I love applets and started talking about how cool it would be if we did the front end as a series of applets. So I proceeded to show off an applet that I did a couple of years ago that is still deployed on a client's web site. When I went to the web page, the applet didn't launch and showed errors. So I looked at the Java console and it spewed some unexpected exception that didn't make sense. I took a few minutes to debug the problem but could not figure it out. On top of that, my friend was starting to get annoyed, so I momentarily gave up.
This ordeal made me think about the poor user who has no clue what a Java console is or what to do when the applet does not launch properly. That user might never return to the applet after encountering this unrecoverable error. Could I write and deploy applets with confidence in the underlying JVM? Not with the same
confidence that I had when I tried to show my perfectly working applet. Could I do this applet as Javascript instead? Probably so, and if I did, it would be smooth and error-free.
So what was the solution? It's something that the typical user might not be willing to do. The fix is to uninstall Java and install the latest version. Once I did that, the problem went away. I hope Oracle is able to make this fix smoother with future releases of Java. Issues like this are what give applets a bad reputation.
Thursday, July 7, 2011
Saturday, March 26, 2011
Java Applet Background Color
Problem: How do I set the background in a Java Applet?
Background: I was accustomed to treating an Applet like a JPanel. With a JPanel, you set its background color with a call to setBackground(Color). That was fine before JDK 1.6, because it was no more than a JPanel with a few differences that allow it to be rendered in a web browser. In JDK 1.6 the Applet became a top level container, which put it on the same level as the JFrame.
Solution: Since JApplet is now a top level container, I have to treat it like one.
There are two options:
Option one:
Get its ContentPane, set the background color and build your GUI on top of it.
Option two:
Build your GUI on a JPanel and make it the ContentPane.
Background: I was accustomed to treating an Applet like a JPanel. With a JPanel, you set its background color with a call to setBackground(Color). That was fine before JDK 1.6, because it was no more than a JPanel with a few differences that allow it to be rendered in a web browser. In JDK 1.6 the Applet became a top level container, which put it on the same level as the JFrame.
Solution: Since JApplet is now a top level container, I have to treat it like one.
There are two options:
Option one:
Get its ContentPane, set the background color and build your GUI on top of it.
Container cp = this.getContentPane();
Then set the ContentPane's background color:
cp.setBackground(Color.blue);
cp.add(new JLabel("Hello content pane"));
Option two:
Build your GUI on a JPanel and make it the ContentPane.
JPanel pnl = new JPanel();
pnl.add(new JLabel("Hello jpanel");
pnl.setBackground(Color.blue);
this.setContentPane(pnl);
Subscribe to:
Posts (Atom)