setCellRenderer for specific cell in JTable

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!
dshrenikAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
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.
0
dshrenikAuthor 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.
0
for_yanCommented:
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
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

dshrenikAuthor 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!
0
for_yanCommented:
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.


0
dshrenikAuthor Commented:
Could you post the code again. Just to make sure I have got it right.  Thanks!
0
for_yanCommented:
yes, I'm now trying to do it
0
for_yanCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
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
0
dshrenikAuthor Commented:
That's awesome! Thanks!!!
0
for_yanCommented:
You are always welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.