Shading part of a JTable Cell dependent upon the value of the cell

Hi

Was hoping some one woudl be able to provide some help with this. I'm trying to create a renderer that will "shade" part of a JTable cell's background depending upon the value in the cell as a percentage (E.g. if the cell contains 0.25 then a quarter of the cell background will be shaded)

What I've got so far is a renderer which will draw a rectangle whose width is the relevant percentage of the cell's width. (i.e. the width of the column) based on something similar I found in the forum but the part I'm struggling with is getting it to draw this rectangle in any cell other than the first cell. I've tried using .getCellRect(...)

to get the x and y position of the cell to draw the rectangle but I still can't make it work.

The code for my renderer as it stands is:

import java.awt.Component;

import java.awt.Graphics;

import java.awt.Graphics2D;

import javax.swing.JLabel;

import javax.swing.JTable;

import javax.swing.table.TableCellRenderer;

publicclass PercentageRepresentationRendererextends JLabelimplements TableCellRenderer{

double percentageValue;

double rectWidth;

double rectHeight;

JTable table;

int row;

int column;

int x;

int y;

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

if (valueinstanceof Number)

{

this.table = table;

this.row = row;

this.column = column;

Number numValue = (Number)value;

percentageValue = numValue.doubleValue();

rectHeight = table.getRowHeight(row);

rectWidth = percentageValue * table.getColumnModel().getColumn(column).getWidth();

}

returnthis;

}

publicvoid paintComponent(Graphics g){

x = table.getCellRect(row, column,false).x;

y = table.getCellRect(row, column,false).y;

setOpaque(false);

Graphics2D g2d = (Graphics2D)g;

g2d.fillRect(x,y,new Double(rectWidth).intValue(),new Double(rectHeight).intValue());

super.paintComponent(g);

}

}

and the following code produces a runnable example:

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;

publicclass PercentageTestTableextends JFrame{

public PercentageTestTable()

{

Object[] columnNames =new Object[]{"A","B"};

Object[][] tableData =new Object[][]{{0.25,0.5},{0.75,1.0}};

DefaultTableModel testModel =new DefaultTableModel(tableData,columnNames);

JTable test =new JTable(testModel);

test.setDefaultRenderer(Object.class,new PercentageRepresentationRenderer());

JScrollPane scroll =new JScrollPane();

scroll.getViewport().add(test);

add(scroll);

}

publicstaticvoid main(String[] args)

{

PercentageTestTable testTable =new PercentageTestTable();

testTable.pack();

testTable.setVisible(true);

testTable.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

If anyone could help or point me in the right direction, I'd appreciate it.

Ruanae

[5609 byte] By [Ruanaea] at [2007-11-27 3:40:53]
# 1

This is an example I published some while ago -

import java.awt.*;

import javax.swing.*;

import javax.swing.table.*;

public class Fred120 extends JPanel

{

static final Object[][] tableData =

{

{1, new Double(10.0)},

{2, new Double(20.0)},

{3, new Double(50.0)},

{4, new Double(10.0)},

{5, new Double(95.0)},

{6, new Double(60.0)},

};

static final Object[] headers =

{

"One",

"Two",

};

public Fred120() throws Exception

{

super(new BorderLayout());

final DefaultTableModel model = new DefaultTableModel(tableData, headers);

final JTable table = new JTable(model);

table.getColumnModel().getColumn(1).setCellRenderer( new LocalCellRenderer(120.0));

add(table);

add(table.getTableHeader(), BorderLayout.NORTH);

}

public class LocalCellRenderer extends DefaultTableCellRenderer

{

private double v = 0.0;

private double maxV;

private final JPanel renderer = new JPanel(new GridLayout(1,0))

{

public void paintComponent(Graphics g)

{

super.paintComponent(g);

g.setColor(Color.CYAN);

int w = (int)(getWidth() * v / maxV + 0.5);

int h = getHeight();

g.fillRect(0, 0, w, h);

g.drawRect(0, 0, w, h);

}

};

private LocalCellRenderer(double maxV)

{

this.maxV = maxV;

renderer.add(this);

renderer.setOpaque(true);

renderer.setBackground(Color.YELLOW);

renderer.setBorder(null);

setOpaque(false);

setHorizontalAlignment(JLabel.CENTER);

}

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

{

final JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);

if (value instanceof Double)

{

v = ((Double)value).doubleValue();

}

return renderer;

}

}

public static void main(String[] args) throws Exception

{

final JFrame frame = new JFrame("Fred120");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(new Fred120());

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

sabre150a at 2007-7-12 8:44:22 > top of Java-index,Desktop,Core GUI APIs...
# 2
After a slight adjustment of the colours, that works exactly as I needed. Thank you very much :-)
Ruanaea at 2007-7-12 8:44:22 > top of Java-index,Desktop,Core GUI APIs...