Java Reference
In-Depth Information
Figure 18-11. Icons in table headers
To create a flexible renderer that can display icons, have the renderer treat the value data
as a JLabel , instead of using the value to fill the JLabel (or for that, matter any JComponent ).
Listing 18-11 shows one such renderer, which is used in the program that created Figure 18-11.
Listing 18-11. Custom Table Cell Renderer for JComponents
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class JComponentTableCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return (JComponent)value;
}
}
Figure 18-11 shows how this renderer might appear with the DiamondIcon as the Icon . The
source for the sample program is shown in Listing 18-12.
Listing 18-12. Customizing Column Headers with Icons
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.awt.*;
public class LabelHeaderSample {
public static void main(String args[]) {
final Object rows[][] = {
{"one", "ichi - \u4E00"},
{"two", "ni - \u4E8C"},
{"three", "san - \u4E09"},
{"four", "shi - \u56DB"},
{"five", "go - \u4E94"},
{"six", "roku - \u516D"},
{"seven", "shichi - \u4E03"},
{"eight", "hachi - \u516B"},
{"nine", "kyu - \u4E5D"},
 
Search WWH ::




Custom Search