We help IT Professionals succeed at work.

setCellRenderer for specific cell in JTable

dshrenik
dshrenik asked
on
I want to set a CellRenderer for a specific cell instead of the whole column.

For example, I want to set the CellRenderer in the 3rd column, 2nd row.

This example does something similar, but not exactly what I want:http://www.javakb.com/Uwe/Forum.aspx/java-gui/6724/setCellRenderer-for-specific-cell-in-JTable

If possible, please provide some sample code. Thanks!
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
what are you trying to achieve - you can do different rendering based on the row after yiouy set renderer for a column - this will be effectrively
different cell renderer for each cell

Explain speifically what you want to get to.

Author

Commented:
I am trying to set multi colored borders of varying thickness. These borders are different for different cells based on their positions in the jtable. 2 cells in the same column can have different kinds of borders.
Awarded 2011
Awarded 2011

Commented:
Look at that multi MutiColorBorder class I posted several times - make a constructor with parameters in that class - including
colors in paramaters for all four sides adn numbers of draw liines for each sides - and make these drawLine commands in the loops,
and create different instance of this MulticolorBorde feeding different paramters - and set different instances of this MultiColorBorder
based on column and row - and with that you should be able to achieve any kinds of borders specific to each cell

Author

Commented:
That code can get very complicated for what I need to achieve. I have found code for a custom renderer but I want to know how I can do setRenderer for a particular cell. Thanks!
Awarded 2011
Awarded 2011

Commented:
No, that code is really simple and you can create specific type of border with maximum flexibility - just have 8 parameters - 4 thicknesesses
and 4 colors - and you can specify any type of borders you want and can create them sepcifically based on the coordiantes of your cell or on the contents just right before you set
the border

You don't need to setRenderer for a particular cell - for different borders or for any other differences - in fact you don't even need to set dfferent renderes
for particular columns, because the getTableCellRendererComponent method has rows and columns as parameters (and in addition has Object value as a parameter)
Based on rows and columns (if your variablity depends just on position of the cell)
or based on the contents (value) you can pprovide any difference you want in the way you want to render particular cell (returning even different kind of component)
 and in particular  you can set different
border  for each particular cell - just check the row, column, and if necessary the value.


Author

Commented:
Could you post the code again. Just to make sure I have got it right.  Thanks!
Awarded 2011
Awarded 2011

Commented:
yes, I'm now trying to do it
Awarded 2011
Awarded 2011
Commented:
import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator;

import java.awt.*;
import java.security.PrivateKey;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;

/**
*
*
* @author Bart Cremers
* @date Feb 6, 2006
*/
public class TableBorders extends JFrame {

   private JTable table;

   protected void frameInit() {
       super.frameInit();

       table = new JTable(5, 4);
       table.setShowVerticalLines(false);
       table.setGridColor(Color.WHITE);
       table.setIntercellSpacing(new Dimension(0, 0));
       table.setDefaultRenderer(Object.class, new
BorderColorRenderer());
       add(table);
   }

   public static void main(String[] args) {
       JFrame f = new TableBorders();
       f.pack();
       f.setLocationRelativeTo(null);
       f.setDefaultCloseOperation(EXIT_ON_CLOSE);
       f.setVisible(true);
   }

   private static class BorderColorRenderer extends
DefaultTableCellRenderer {

       public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
                                                      int row, int
column) {
          MultiColorBorder border = null;
            MultiColorBorder border1 = null;

                   border = new MultiColorBorder();
                   border1 = new MultiColorBorder(new Color[] {Color.RED,  Color.BLUE,  Color.GREEN,  Color.YELLOW}, new int[] {2,4,6,8});
              border.setOpaque(false);

           JComponent comp = (JComponent)
super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
row, column);
          comp.setBorder(border);
           if(row == 2 && column == 2)comp.setBorder(border1);
           return comp;
       }
   }
}

class MultiColorBorder implements Border {

int [] thicknesses;
Color [] colors;

public MultiColorBorder() {
thicknesses = new int[]{1,1,1,1};
colors = new Color[]{Color.BLUE, Color.BLUE, Color.BLUE, Color.BLUE};


}


public MultiColorBorder(Color[] colors, int [] thicknesses){
    this();
  for(int j=0; j<colors.length; j++)this.colors[j] = colors[j];
     for(int j=0; j<thicknesses.length; j++)this.thicknesses[j] = thicknesses[j];


}

    boolean opaque;
     private Color color;

    public void setOpaque(boolean b){
        opaque = b;

    }

        private final Insets insets    = new Insets(1, 1, 1, 1);


        public boolean isBorderOpaque() {
            return opaque;

        }
        public void paintBorder(Component c, Graphics g, int x, int y,
                int width, int height) {

               g.setColor(colors[0]);
            for(int j=0; j< thicknesses[0]; j++)
            g.drawLine(x, y+j, x + width, y+j);
                        g.setColor(colors[1]);
             for(int j=0; j< thicknesses[1]; j++)
            g.drawLine(x + width-j, y, x + width-j, y + height);

                     g.setColor(colors[2]);
                for(int j=0; j< thicknesses[2]; j++)
                       g.drawLine(x+width, y+height-j, x, y + height-j);
           
             g.setColor(colors[3]);
            for(int j=0; j< thicknesses[3]; j++)
            g.drawLine(x+j, y+ height, x+j, y);

        }
        public Insets getBorderInsets(Component c) {
            return insets;
        }
        void setColor(Color color) {
            this.color = color;
        }
    }

Open in new window




uniqueCell.PNG
Awarded 2011
Awarded 2011

Commented:
This is better in the corners:

import java.awt.*;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;

/**
*
*
* @author Bart Cremers
* @date Feb 6, 2006
*/
public class TableBorders extends JFrame {

   private JTable table;

   protected void frameInit() {
       super.frameInit();

       table = new JTable(5, 4);
       table.setShowVerticalLines(false);
       table.setGridColor(Color.WHITE);
       table.setIntercellSpacing(new Dimension(0, 0));
       table.setDefaultRenderer(Object.class, new
BorderColorRenderer());
       add(table);
   }

   public static void main(String[] args) {
       JFrame f = new TableBorders();
       f.pack();
       f.setLocationRelativeTo(null);
       f.setDefaultCloseOperation(EXIT_ON_CLOSE);
       f.setVisible(true);
   }

   private static class BorderColorRenderer extends
DefaultTableCellRenderer {

       public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
                                                      int row, int
column) {
          MultiColorBorder border = null;
            MultiColorBorder border1 = null;

                   border = new MultiColorBorder();
                   border1 = new MultiColorBorder(new Color[] {Color.RED,  Color.BLUE,  Color.GREEN,  Color.YELLOW}, new int[] {5,5,5,5});
              border.setOpaque(false);

           JComponent comp = (JComponent)
super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
row, column);
          comp.setBorder(border);
           if(row == 2 && column == 2)comp.setBorder(border1);
           return comp;
       }
   }
}

class MultiColorBorder implements Border {

int [] thicknesses;
Color [] colors;

public MultiColorBorder() {
thicknesses = new int[]{1,1,1,1};
colors = new Color[]{Color.BLUE, Color.BLUE, Color.BLUE, Color.BLUE};


}


public MultiColorBorder(Color[] colors, int [] thicknesses){
    this();
  for(int j=0; j<colors.length; j++)this.colors[j] = colors[j];
     for(int j=0; j<thicknesses.length; j++)this.thicknesses[j] = thicknesses[j];


}

    boolean opaque;
     private Color color;

    public void setOpaque(boolean b){
        opaque = b;

    }

        private final Insets insets    = new Insets(1, 1, 1, 1);


        public boolean isBorderOpaque() {
            return opaque;

        }
        public void paintBorder(Component c, Graphics g, int x, int y,
                int width, int height) {

               g.setColor(colors[0]);
            for(int j=0; j< thicknesses[0]; j++)
            g.drawLine(x+j, y+j, x + width-j, y+j);
            /*
              g.drawLine(x, y+1, x + width, y+1);
              g.drawLine(x, y+2, x + width, y+2);
              g.drawLine(x, y+3, x + width, y+3);
             g.drawLine(x, y+4, x + width, y+4);
             */
             g.setColor(colors[1]);
             for(int j=0; j< thicknesses[1]; j++)
            g.drawLine(x + width-j, y+j, x + width-j, y + height-j);

                     g.setColor(colors[2]);
                for(int j=0; j< thicknesses[2]; j++)
                       g.drawLine(x+width-j, y+height-j, x+j, y + height-j);
              /*
            g.drawLine(x+width, y+height-1, x, y + height-1);
              g.drawLine(x+width, y+height-2, x, y + height-2);
              g.drawLine(x+width, y+height-3, x, y + height-3);
              g.drawLine(x+width, y+height-4, x, y + height-4);
            */

             g.setColor(colors[3]);
            for(int j=0; j< thicknesses[3]; j++)
            g.drawLine(x+j, y+ height-j, x+j, y+j);

        }
        public Insets getBorderInsets(Component c) {
            return insets;
        }
        void setColor(Color color) {
            this.color = color;
        }
    }

Open in new window

uniqueCell-with-good-corners.PNG

Author

Commented:
That's awesome! Thanks!!!
Awarded 2011
Awarded 2011

Commented:
You are always welcome.