Shading in JTables

Hi, i'm trying to shade alternate rows of a JTable. I found this on a website which should work:

public Component prepareRenderer(

TableCellRenderer renderer,

int row,int col){

Component c = super.prepareRenderer(renderer,

row, col);

if (row % 2 == 0 && !isCellSelected(row,col)){

c.setBackground(Color.LIGHT_GRAY);

}else{

c.setBackground(getBackground());

}

return c;

}

however, it requires use of a class which subclasses JTable. the Jtable i am implementing is in a class which subclasses JFrame:

publicclass JBookListFrameextends JFrameimplements ActionListener{

privatestaticfinalint MAX = 4;//initial max value for empty list

private BookList bookList;

private JTable table;

publicstaticvoid main(String[] args)throws java.io.IOException{

JBookListFrame frame =new JBookListFrame();

frame.setVisible(true);

}

public JBookListFrame()throws java.io.IOException{

setTitle("Book Collection Table");

bookList =new BookList(MAX,"books.txt");

table =new JTable(bookList);

JPanel buttonPanel =new JPanel();

addButton("Sort By Author",buttonPanel);

addButton("Sort By Number",buttonPanel);

addButton("Delete",buttonPanel);

addButton("Edit",buttonPanel);

addButton("New",buttonPanel);

addButton("Load",buttonPanel);

addButton("Save",buttonPanel);

getContentPane().add(buttonPanel,"South");

getContentPane().add(new JScrollPane(table),"Center");

addWindowListener(

new WindowAdapter(){

publicvoid windowClosing(WindowEvent event){

System.exit(0);}

} );

setSize(600, 250);

setVisible(true);

}

how can i mix the two together so that i can shade alternate rows of my JTable? because i know i can't extend two classes.........

[3682 byte] By [chrissmith51a] at [2007-11-26 18:21:06]
# 1

I think you've got some wires crossed...

Your JFrame subclass would not extend JTable also. Not just because you can't but because you wouldn't want to, it just doesn't make any sense if you think about it. What you want is a custom JTable, which means you have to subclass JTable. There are two ways to do this:

(1) In Java, you can create anonymous classes, which basically means you can define the class and instantiate it at the same time:

JTable myTable = new JTable() {

public Component prepareRenderer(

TableCellRenderer renderer,

int row, int col) {

Component c = super.prepareRenderer(renderer,

row, col);

if (row % 2 == 0 && !isCellSelected(row,col)) {

c.setBackground(Color.LIGHT_GRAY);

} else {

c.setBackground(getBackground());

}

return c;

}

}

(2) You can create a concrete subclass of JTable as a separate class:

class MyCoolTable extends JTable {

public Component prepareRenderer(

TableCellRenderer renderer,

int row, int col) {

Component c = super.prepareRenderer(renderer,

row, col);

if (row % 2 == 0 && !isCellSelected(row,col)) {

c.setBackground(Color.LIGHT_GRAY);

} else {

c.setBackground(getBackground());

}

return c;

}

}

// and invoke it with

JTable table = new MyCoolTable();

Jasprea at 2007-7-9 5:54:55 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi, yes you are right you can't extend two classes, but then again you don't need to for what you are trying to accomplish.

public class JBookListFrame extends JFrame implements ActionListener {

...

private MySubClassOfJTable table;

}

Then all you need is a seperate class which extends JTable e.g.

public class MySubClassOfJTable extends JTable {

...

This would give you the behaviour you are looking for to use the prepareRender.

However I would say that to change the way you render a JTable for example the background color of a cell, you are much better to use a subclass of TableCellRenderer and its

public Component getTableCellRendererComponent(JTable table, Object value,

boolean isSelected, boolean hasFocus, int row, int column) {

if (row % 2 == 0) {

setBackground(unSelectedColorBlue);

} else {

setBackground(unSelectedColorWhite);

}

method. Then all you need to do is change the column renderer for each of the columns in your table. This means you are seperating your UI from your data, and also means you subclass one method in TableCellRenderer instead of mucking with JTable.

Hope this helps,

Lauren

lauren_dempstera at 2007-7-9 5:54:55 > top of Java-index,Desktop,Core GUI APIs...