JTable with mycellrenderer stucked

Hi! I've written "TableColorRenderer extends DefaultTableCellRenderer" class to add a border to some cells

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

Component ret = super.getTableCellRendererComponent(tblDataTable,value,isSelected,hasFocus,row,column);

if(colored[row]){

LinesBorder border =new LinesBorder(borderColor);

border.setThickness(new Insets(0, 0, thickness, 0));

setBorder(border);

tblDataTable.setRowHeight(row, thickness + 16);

}

return ret;

}

publicclass LinesBorderextends AbstractBorderimplements SwingConstants{

public LinesBorder(Color color){

this(color, 1);

}

public LinesBorder(Color color,int thickness){

setColor(color);

setThickness(thickness);

}

public LinesBorder(Color color, Insets insets){

setColor(color);

setThickness(insets);

}

publicvoid paintBorder(Component c, Graphics g,int x,int y,int width,int height){

Color oldColor = g.getColor();

g.setColor(northColor);

for (int i = 0; i < northThickness; i++){

g.drawLine(x, y+i, x+width-1, y+i);

}

g.setColor(southColor);

for (int i = 0; i < southThickness; i++){

g.drawLine(x, y+height-i-1, x+width-1, y+height-i-1);

}

g.setColor(eastColor);

for (int i = 0; i < westThickness; i++){

g.drawLine(x+i, y, x+i, y+height-1);

}

g.setColor(westColor);

for (int i = 0; i < eastThickness; i++){

g.drawLine(x+width-i-1, y, x+width-i-1, y+height-1);

}

g.setColor(oldColor);

}

public Insets getBorderInsets(Component c){

returnnew Insets(northThickness, westThickness, southThickness, eastThickness);

}

public Insets getBorderInsets(Component c, Insets insets){

returnnew Insets(northThickness, westThickness, southThickness, eastThickness);

}

publicboolean isBorderOpaque(){returntrue;}

publicvoid setColor(Color c){

northColor = c;

southColor = c;

eastColor = c;

westColor = c;

}

publicvoid setColor(Color c,int direction){

switch (direction){

case NORTH: northColor = c;break;

case SOUTH: southColor = c;break;

case EAST: eastColor = c;break;

case WEST: westColor = c;break;

default:

}

}

publicvoid setThickness(int n){

northThickness = n;

southThickness = n;

eastThickness = n;

westThickness = n;

}

publicvoid setThickness(Insets insets){

northThickness = insets.top;

southThickness = insets.bottom;

eastThickness = insets.right;

westThickness = insets.left;

}

publicvoid setThickness(int n,int direction){

switch (direction){

case NORTH: northThickness = n;break;

case SOUTH: southThickness = n;break;

case EAST: eastThickness = n;break;

case WEST: westThickness = n;break;

default:

}

}

publicvoid append(LinesBorder b,boolean isReplace){

if (isReplace){

northThickness = b.northThickness;

southThickness = b.southThickness;

eastThickness = b.eastThickness;

westThickness = b.westThickness;

}else{

northThickness = Math.max(northThickness ,b.northThickness);

southThickness = Math.max(southThickness ,b.southThickness);

eastThickness = Math.max(eastThickness ,b.eastThickness);

westThickness = Math.max(westThickness ,b.westThickness);

}

}

publicvoid append(Insets insets,boolean isReplace){

if (isReplace){

northThickness = insets.top;

southThickness = insets.bottom;

eastThickness = insets.right;

westThickness = insets.left;

}else{

northThickness = Math.max(northThickness ,insets.top);

southThickness = Math.max(southThickness ,insets.bottom);

eastThickness = Math.max(eastThickness ,insets.right);

westThickness = Math.max(westThickness ,insets.left);

}

}

protectedint northThickness;

protectedint southThickness;

protectedint eastThickness;

protectedint westThickness;

protected Color northColor;

protected Color southColor;

protected Color eastColor;

protected Color westColor;

}

and it seems to work great but when the JTable shows the cells with border, if i try to open a JDialog, the app becomes stucked (if i close the JDialog the app come back working)! If JTable shows the cells whitout border and i try to open same JDialog, the app works correctly. Maybe TableColorRenderer isn't the solution to paint borders in a cell? Also I'm finding a solution to overwrite the JTable's grid to avoid adding border by TableColorRenderer. Help me plese!

[9588 byte] By [redclouda] at [2007-10-3 8:45:56]
# 1

Not sure what's causing your exact problem (try to recreate it in a single executable class), but a couple of comments -

- try to avoid creating too many objects in painting code, you can reuse all the borders and insets you're creating

- you realise you can implement the same border style without writing all that code? Just use MatteBorder and CompoundBorder :o)

itchyscratchya at 2007-7-15 3:54:53 > top of Java-index,Desktop,Core GUI APIs...
# 2

Ok, i've substitued LineBoreder with BorderFactory but the problem still remain. The problem is that when the row with added border is shown by JTable, the metod getTableCellRendererComponent is called in a loop while JTable shows no rows with added border! I think this loop is generated by swing's engine... how i can solve?

redclouda at 2007-7-15 3:54:54 > top of Java-index,Desktop,Core GUI APIs...
# 3

> I think this loop is generated by swing's engine

I think the loop is generated by your code.

> tblDataTable.setRowHeight(row, thickness + 16);

Whenever you change the row height the table needs to repaint the row, to the renderer get called over and over. Don't set the height of the row if its already equal to the height you want it to be.

camickra at 2007-7-15 3:54:54 > top of Java-index,Desktop,Core GUI APIs...
# 4

> > I think this loop is generated by swing's engine

>

> I think the loop is generated by your code.

Yes but it's the swing's engine that make it :D

> Whenever you change the row height the table needs to

> repaint the row, to the renderer get called over and

> over. Don't set the height of the row if its already

> equal to the height you want it to be.

ok, that's the problem! I solved with this code

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

if(column == table.getColumnCount() - 1)

if(table.getRowHeight(row) != 16 + thickness)

table.setRowHeight(row, 16 + thickness);

super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

Border border = BorderFactory.createMatteBorder(0, 0, thickness, 0, borderColor);

super.setBorder(border);

return this;

}

Thank you all!

redclouda at 2007-7-15 3:54:54 > top of Java-index,Desktop,Core GUI APIs...