Welcome to the Core Java Technologies Tech Tips for November 2006. Core Java Technologies Tech Tips provide tips and hints for using core Java technologies and APIs provided in the Java 2 Platform, Standard Edition (Java SE). This issue provides tips for the following: These tips were developed using the Java 2 Platform, Standard Edition Development Kit 5.0 (JDK 5.0). You can download JDK 5.0 from the Java SE Downloads page. See the Subscribe/Unsubscribe note at the end of this newsletter to subscribe to Tech Tips that focus on technologies and products in other Java platforms.
Java Web Start is a great technology for deploying lightweight client
applications. It even supports automatic updates. One of Web
Start's less understood features is the
First a quick recap: What is Java Web Start? Web Start is a
technology built into the Java platform that lets you deploy
lightweight applications.
First the user downloads an XML file that describes the application,
lists the required jars, and optionally asks for full desktop
permissions. Once everything is downloaded, the application will start.
If your application asks for permissions, the user will see a
warning dialog. If your app does not request permissions, it
will run in a secure sandbox, much like an applet. Running in the
sandbox is the proper, safe, and secure way to write a Java Web Start
application. If your application is inside a secure sandbox, how can
it do unsafe things like save data to disk? That's were Java
Web Start's
The Web Start environment provides the
This example code shows a license approval dialog. If the
user accepts the license, the code saves a note about it so that the
application won't show the license again. All services in the
Web Start environment can be obtained using
the
import javax.jnlp.*;
//....
public void showDialog() {
// show the dialog. after the user accepts do the following
try {
PersistenceService service = (PersistenceService)
ServiceManager.lookup("javax.jnlp.PersistenceService");
service.create(new URL("http://www.myserver.com/CoolApp/ApprovedLicense"),100);
print("created the license");
} catch (Throwable thr) {
print(thr);
}
}
public static void print(Object o) {
System.out.println(o);
if(o instanceof Throwable) {
((Throwable)o).printStackTrace();
}
}
Obtain a
Now that you have created a storage entry, how do you use it? The
code above will create an empty entry. Since the entry's contents
are irrelevant, you can simply check that it exists by calling
the
public void checkLicenseApproved() {
try {
PersistenceService service = (PersistenceService)
ServiceManager.lookup("javax.jnlp.PersistenceService");
FileContents contents =
service.get(new URL("http://www.myserver.com/CoolApp/ApprovedLicense"));
print("The user has already approved the license");
} catch (FileNotFoundException fnfe) {
print("need to show the license");
showDialog();
} catch (Throwable ex) {
print(ex);
}
}
That's all you need to conditionally show a license panel. Notice
that you must use the same URL for both storing and loading data
from the persistence store. If you use the wrong URL, a
The One of the great things about Swing is that you can customize your components to look like whatever you want. It has become quite common to add a slightly custom look to standard desktop applications. In iTunes, for example, song lists look mostly normal but have some very subtle changes that make the interface look nicer. This tip will show you how to style a standard Swing list with an alternating stripe background and a gradient shaded selection.
The In order to stripe the list cells, you must change the background color for every other row. Here's a simple cell renderer which will do just that.
import java.awt.*;
import javax.swing.*;
class StripeRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value,
index, isSelected, cellHasFocus);
if(index%2 == 0) {
label.setBackground(new Color(230,230,255));
}
return label;
}
}
This class simply calls its parent's version of the
You can set this class as the renderer for your
list.setCellRenderer(new StripeRenderer());
You can also create background gradients for selected list cells.
Since you cannot create a gradient by simply setting the background color
of the label, you must implement the list cell drawing yourself.
The
class GradientSelectionRenderer extends DefaultListCellRenderer {
private boolean drawGradient = false;
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value,
index, isSelected, cellHasFocus);
drawGradient = false;
label.setOpaque(true);
if(isSelected) {
drawGradient = true;
label.setForeground(Color.WHITE);
label.setBackground(Color.BLUE);
label.setOpaque(false);
}
return label;
}
protected void paintComponent(Graphics g) {
if(drawGradient) {
Graphics2D gfx = (Graphics2D)g.create();
GradientPaint gradient = new GradientPaint(0,0, Color.BLUE,
0,10,Color.BLACK);
gfx.setPaint(gradient);
gfx.fillRect(0,0,getWidth(),getHeight());
gfx.dispose();
}
super.paintComponent(g);
}
}
Notice the
These two renderers look pretty cool, but wouldn't it be great
if you could have both effects at the same time? You could
combine the two into a single class, but there's an easier way.
You can make the
class StripeRenderer extends DefaultListCellRenderer {
GradientSelectionRenderer gradient = new GradientSelectionRenderer();
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if(isSelected) {
return gradient.getListCellRendererComponent(list, value,
index, isSelected, cellHasFocus);
}
// else, do the usual stripe renderer stuff
...
}
}
...
list.setCellRenderer(new StripeRenderer());
That's it. The two renderers are now combined into one. Cell renderers are a great way to spice up any normal application without requiring significant changes. They can really make your program pop! For more on lists and list cell renderers see the following documents: | |||||||||
|
| ||||||||||||