Why there is no setCellRenderer in JTable?

R

RC

Hi there,

So far I only found

JTable.setDefaultRenderer(Class, TableCellRenderer);

I would like set foreground color in 1st column of my table,
and also want to detected the selection listener.

Here are what I did as following, No errors, but nothing
is work.

Please help.
Thank Q!

table.setDefaultRenderer(Color.class,
new LabelDefaultTableCellRenderer());



public class LabelDefaultTableCellRenderer extends JLabel
implements TableCellRenderer {

private static final long serialVersionUID = 1L;

public LabelDefaultTableCellRenderer() {
super();
setOpaque(true);
}

public Component getTableCellRendererComponent(JTable table,
Object color,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
Color newColor = (Color)color;

if (column == 1) {
setForeground(Color.red);

}

if (isSelected) {
System.out.println("Cell selected at " +
row + ", " + column);
}
return (this);
}
}
 
T

Tom Hawtin

RC said:
public class LabelDefaultTableCellRenderer extends JLabel

That's an odd class name. DefaultTableCellRenderer is so called, because
it's the implementation you get by default.

I wouldn't subclass JLabel. You don't need to subclass it, so having a
reference to a label is probably a better idea. Although
DefaultTableCellRenderer takes a different view.

The common thing to do is subclass DefaultTableCellRenderer.
implements TableCellRenderer {

private static final long serialVersionUID = 1L;

public LabelDefaultTableCellRenderer() {
super();
setOpaque(true);
}

public Component getTableCellRendererComponent(JTable table,
Object color,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
Color newColor = (Color)color;

if (column == 1) {
setForeground(Color.red);

}

And if the column isn't 1? Most (but not all) if statements should
either end in return/break/continue/throw or have an else clause.
if (isSelected) {
System.out.println("Cell selected at " +
row + ", " + column);
}
return (this);
}
}

The obvious thing you are failing to do is set the text of the label.
Without any text, you wont see the change of foreground very well.

Tom Hawtin

[Note: Followup-To: comp.lang.java.gui]
 
A

Adrian Hands, Raleigh NC USA

public class Trash
{
public static void main( String[] args )
{
javax.swing.JTable table = new javax.swing.JTable( 5, 5 );
table.setDefaultRenderer( Object.class, new
LabelDefaultTableCellRenderer() );

javax.swing.JFrame frame = new javax.swing.JFrame();
frame.getContentPane().add( table );
frame.pack();
frame.setVisible( true );
}

public static class LabelDefaultTableCellRenderer
extends javax.swing.table.DefaultTableCellRenderer
{

private static final long serialVersionUID = 1L;

public LabelDefaultTableCellRenderer() {
super();
setOpaque(true);
}

public java.awt.Component
getTableCellRendererComponent( javax.swing.JTable table,

Object color,

boolean isSelected,

boolean hasFocus,
int
row,
int
column ) {

java.awt.Component comp =
super.getTableCellRendererComponent( table,
color,
isSelected,
hasFocus,
row,
column );

// Color newColor = (Color)color;

// note: column 1 is the second column
comp.setForeground( column == 1 ? java.awt.Color.red :
java.awt.Color.black );

// note: this gets written every time the cell is
rendered, e.g. when it's uncovered, etc--very often!
if (isSelected) {
System.out.println("Cell selected at " +
row + ", " + column);
}
return comp;
}

}

}
 
R

Roedy Green

Why there is no setCellRenderer in JTable? you ask.

It is very rare that the renderers for all columns are identical. To
create such a beast, just loop over all columns.
 
T

Tom Hawtin

Roedy said:
Why there is no setCellRenderer in JTable? you ask.

The documentation for getDefaultRenderer seems to imply that you could
set a default renderer for all columns by setting the default renderer
for Object.class, Number.class and Boolean.class.

Looking at the source, you would also need Float.class, Double.class,
Date.class, Icon.class, and ImageIcon.class. Gotta love Swing.

Tom Hawtin

[Followup-to: comp.lang.java.gui]
 

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
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top