Setting selected cell colors

[nobr]Hello

Selected cells in my JTable have white background and foreground color, which is annoying as one cannot see the cellcontent. I use a custom tablecellrenderer, and guess that is part of the reason. Setting the tables setSelectionBackground() does nothing. I suspect that some change in my tablecellrenderer is appropriate, but do not know how to check if the cell being rendered is selected or not. My cellrenderer is below.

import javax.swing.table.*;

import javax.swing.*;

import java.awt.*;

import java.util.*;

import no.jer.library.toolbox.FMT;

publicclass MyTableCellRendererextends DefaultTableCellRenderer{

//private final Color orig = new Color(255, 255, 255);

privatefinal Color orig = super.getBackground();

private FMT fmt =new FMT();

/** Sets default format from current locale as

* correct way to render a number.

*/

public MyTableCellRenderer(){

super();

}

public MyTableCellRenderer(FMT fmt){

super();

this.fmt = fmt;

}

/** Overrides DefaultTableCellRenderer method.<br>

*/

publicvoid setValue(Object value){

//logg.append(orig.toString()+"\n");

if ((value !=null) && (valueinstanceof Number)){

if(valueinstanceof Double){

Double val = (Double) value;

if(val.isNaN() || val.doubleValue()<=this.fmt.getNanLimit()){

this.setBackground(Color.RED);

}else{

//this.setBackground(super.getBackground());

this.setBackground(orig);

}

this.setHorizontalAlignment(JLabel.RIGHT);

value = fmt.fdf(val).trim();

}elseif(valueinstanceof Integer){

this.setHorizontalAlignment(JLabel.RIGHT);

this.setBackground(orig);

}

}elseif((value !=null) && (valueinstanceof String)){

this.setBackground(orig);

this.setHorizontalAlignment(JLabel.LEFT);

}elseif((value!=null) && (valueinstanceof Date)){

this.setBackground(orig);

value = fmt.f((Date)value).trim();

this.setHorizontalAlignment(JLabel.LEFT);

}elseif((value !=null) && (valueinstanceof GregorianCalendar)){

GregorianCalendar greg = (GregorianCalendar) value;

this.setBackground(orig);

this.setHorizontalAlignment(JLabel.LEFT);

value = fmt.f(greg).trim();

greg =null;

}else{

this.setBackground(orig);

this.setHorizontalAlignment(JLabel.LEFT);

value = fmt.f(value);

}

super.setValue(value);

}

public FMT getFmt(){

return fmt;

}

publicvoid setFmt(FMT fmt){

this.fmt = fmt;

}

}//END

Regards

Fluid[/nobr]

[5248 byte] By [Fluida] at [2007-10-3 3:23:08]
# 1

Note the method whose signature is

public void Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

Note that it has a parameter "isSelected".

I'm sure I learned how to use this method from the JTable tutorial.

DrClapa at 2007-7-14 21:15:38 > top of Java-index,Java Essentials,Java Programming...
# 2

1) Swing related questions should be posted in the Swing forum.

2) JTable comes with renderers that support Numbers, Ingtegers, Dates. You should not be checking the instanceof in the renderer. Instead you should be using the default renderers, or create a custom renderer for each object type.

I don't know what your logic is for coloring cells. if you search the Swing forum you should find my "TablePrepareRender" example which shows one simple way to color cells. My "TableColor" example shows a more complicated way.

camickra at 2007-7-14 21:15:38 > top of Java-index,Java Essentials,Java Programming...
# 3

I cannot use the setDefaultRenderer() as my cells may be of varying classes. So I tried do implement the getCellRenderer(int row, int col), without luck.

jtable = new JTable(getTable_model()){

public TableCellRenderer getCellRenderer(int row, int column) {

if (table_model.getValueAt(row, column) instanceof Number) {

return numrend;

}else if (table_model.getValueAt(row, column) instanceof Calendar) {

return calrend;

}

// else...

return super.getCellRenderer(row, column);

}

};

It is strange as this method is never queried during running. I was under the impression that the above construct would create a full-fledge JTable, but overriding this specific method. Java Studio does not show that method as overriding either. It is just a copypaste from the tutorial. Any idea why this overriding does not work?

camickr: Obviously I do not want to use the default renderer. And if I need custom renderer, is it not the same if I use 1 class instead of several classes? As to the forum search I did not find any of your examples.

Fluida at 2007-7-14 21:15:38 > top of Java-index,Java Essentials,Java Programming...
# 4

> Obviously I do not want to use the default renderer.

Like I said, I don't know your exact requirement, so I was providing some alternatives which may give you some ideas.

> And if I need custom renderer, is it not the same if I use 1 class instead of several classes?

In general if you need to keep using "instanceof" to determine logic flow, then you are not taking advantage of polymorphism.

The JDK was designed with a different renderer for each type of class.

I'll let decide what the better approach is.

> As to the forum search I did not find any of your examples.

a) javascript needs to be turned on

b) you need to use the "Search Forums" on the left, not the "Search" on the top right

c) you need to be in the Swing forum

camickra at 2007-7-14 21:15:38 > top of Java-index,Java Essentials,Java Programming...