![]() |
![]() |
![]() |
![]() |
![]() |
|
The easiest way to change the way that cells
are rendered is to create a subclass of a JComponent subclass that
implements the TableCellRenderer interface.
The following table-cell renderer, for example, uses and reuses a single,
white-background, appropriately right or left-aligned, borderless
JLabel instance:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class MovieTableCellRenderer extends JLabel
implements TableCellRenderer {
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
setText(value.toString());
setOpaque(true);
setBackground(Color.white);
if (value instanceof Integer) {
setHorizontalAlignment(SwingConstants.RIGHT);
}
else if (value instanceof String) {
setHorizontalAlignment(SwingConstants.LEFT);
}
return this;
}
}