How to render JTable headers as icons?

B

BettyV

Hi All,

I'm trying to use a TableCellRenderer to render text and icons in my column
headers.

I setup a vector of strings and ImageIcons for my JTable constructor.
When my TableCellRenderer goes to render them, they are all instanceof
String whether they were originally ImageIcons, or Strings.

Do I have to setup different renderers for each column?

Thanks,

-Betty
 
N

Nick Pomfret

BettyV said:
Hi All,

I'm trying to use a TableCellRenderer to render text and icons in my column
headers.

I setup a vector of strings and ImageIcons for my JTable constructor.
When my TableCellRenderer goes to render them, they are all instanceof
String whether they were originally ImageIcons, or Strings.

Do I have to setup different renderers for each column?

Thanks,

-Betty

Create a CellRenderer (by extending DefaultTableCellRenderer) containing
your collection of images and set that renderer in your JTable:

tableHeader.setDefaultRenderer(myRenderer);

In your CellRenderer.getTableCellRendererComponent method you can use the
columnIndex argument to determine which of your images to place in the
header.
 
N

Nick Pomfret

BettyV said:
Hi All,

I'm trying to use a TableCellRenderer to render text and icons in my column
headers.

I setup a vector of strings and ImageIcons for my JTable constructor.
When my TableCellRenderer goes to render them, they are all instanceof
String whether they were originally ImageIcons, or Strings.

Do I have to setup different renderers for each column?

Thanks,

-Betty

You only need only Renderer class for your headers, something like:

JTable table = new JTable();
table.getTableHeader().setDefaultRenderer(new
DefaultTableCellRenderer() {
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean
isSelected, boolean hasFocus,
int row, int
column) {
JLabel headerLabel = (JLabel)
super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
headerLabel.setIcon(getIcon(column));
return headerLabel;
}
});

where the getIcon method perhaps pulls the icon object from your vector.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top