JTable prepareRenderer not always working

/*

*

* Here I have a problem in the third column of the table:

*when selecting a line the rendereing only works when uncommenting the commented lines.

* Do I really have to duplicate the code of the prepareRenderer() method ?

*

*/

import java.awt.*;

import java.text.*;

import java.util.*;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.table.*;

publicclass TableRowRenderingextends JFrame{

public TableRowRendering(){

Object[] columnNames ={"Type","Date","Company","Shares","Price"};

Object[][] data =

{

{"Buy",new Date(),"IBM\nCorporation",new Integer(1000),new Double(80.50)},

{"Sell",new Date(),"MicroSoft",new Integer(2000),new Double(6.25)},

{"Sell",new Date(),"Apple",new Integer(3000),new Double(7.35)},

{"Buy",new Date(),"Nortel",new Integer(4000),new Double(20.00)}

};

DefaultTableModel model =new DefaultTableModel(data, columnNames){

public Class getColumnClass(int column){

return getValueAt(0, column).getClass();

}

};

table =new JTable( model ){

public Component prepareRenderer(

TableCellRenderer renderer,int row,int column){

Component c = super.prepareRenderer(renderer, row, column);

if (!isRowSelected(row))

c.setBackground(row % 2 == 0 ?new Color(237,243,254) :null );

return c;

}

};

table.setRowHeight(50);

table.getColumnModel().getColumn(2).setCellRenderer(new MyTableCellRenderer());

table.setPreferredScrollableViewportSize(table.getPreferredSize());

getContentPane().add(new JScrollPane( table ), BorderLayout.CENTER);

}

private JTable table;

publicstaticvoid main(String[] args){

TableRowRendering frame =new TableRowRendering();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setLocationRelativeTo(null );

frame.setVisible(true);

}

publicclass MyTableCellRendererextends JTextAreaimplements TableCellRenderer{

public Component getTableCellRendererComponent( JTable table,

Object value,boolean isSelected,boolean hasFocus,

int row,int column ){

setText(value.toString());

//setBackground(row % 2 == 0 ? new Color(237,243,254) : null );

//if(isSelected) setBackground(table.getSelectionBackground());

//setBorder(hasFocus ? selected : null);

returnthis;

}

Border selected = BorderFactory.createLineBorder(new Color(99,130,191));

}

}

[5678 byte] By [Andre_Uhresa] at [2007-10-2 10:59:44]
# 1

Where is your MyTableCellRenderer is used? I don't see the line in your code.

Usually, if you override prepareRenderer(), you don't need to write a custom renderer class anew.

And if you set a background color for some rows, you should set it for other rows even if you want

them to be the default bg color.

hiwaa at 2007-7-13 3:28:26 > top of Java-index,Desktop,Core GUI APIs...
# 2
Sorry the duplication of this line is not necessary:setBackground(row % 2 == 0 ? new Color(237,243,254) : null );So, I think there isn't really a problem.
Andre_Uhresa at 2007-7-13 3:28:26 > top of Java-index,Desktop,Core GUI APIs...
# 3
> Where is your MyTableCellRenderer is used? I don't see the line in your code.Sorry. I retract the line.
hiwaa at 2007-7-13 3:28:26 > top of Java-index,Desktop,Core GUI APIs...
# 4
Anyway, thanks for your reply, hiwa.
Andre_Uhresa at 2007-7-13 3:28:26 > top of Java-index,Desktop,Core GUI APIs...