Java Reference
In-Depth Information
Caution A common mistake when creating your own renderer is forgetting to make the renderer compo-
nent opaque. This causes the background coloration of the renderer to be ignored and the list container's
background to bleed through. With the DefaultListCellRenderer class, the renderer component is
already opaque.
A sample program that uses the new renderer follows in Listing 13-5. It doesn't do anything
special other than install the custom cell renderer that was just created.
Listing 13-5. Rendering List Cells Sample
import javax.swing.*;
import java.awt.*;
public class CustomBorderSample {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
String labels[] = {"Chardonnay", "Sauvignon", "Riesling", "Cabernet",
"Zinfandel", "Merlot", "Pinot Noir", "Sauvignon Blanc", "Syrah",
"Gewürztraminer"};
JFrame frame = new JFrame("Custom Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList jlist = new JList(labels);
ListCellRenderer renderer = new FocusedTitleListCellRenderer();
jlist.setCellRenderer(renderer);
JScrollPane sp = new JScrollPane(jlist);
frame.add(sp, BorderLayout.CENTER);
frame.setSize(300, 200);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Figure 13-6 shows the output of the sample program.
Figure 13-6. A JList with a custom focus border cell renderer
 
Search WWH ::




Custom Search