We help IT Professionals succeed at work.

JTable - Cell Renderer

dshrenik
dshrenik asked
on
I am trying to set the border of each cell in a JTable in its TableCellRenderer.

However, the border appears outside the grid line. When I turn off the grid lines, I see white spaces  in the place of the gridlines.

I don not want to set the color of the gridline to the background color since I am using multi-colored borders for my cells.

Please let me know how I can fix it. Thanks!
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
Please, post your code

Author

Commented:
It's the case for any table, I think.
Awarded 2011
Awarded 2011

Commented:
what is the case for any table?

and what is it that you are trying to achieve?

Author

Commented:
The problem that I spoke about holds good for any table, I think.

I am trying to create a custom JTable with multicolored borders.

I have attached an image of how it looks like. I have turned off the gridlines and have the following code in my cell renderer:
setBorder(BorderFactory.createLineBorder(Color.red));
Table-Gridline.png
Awarded 2011
Awarded 2011

Commented:
well, I don't see much problem with that either - effectively looks like red borders

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, 1);
       table.setShowVerticalLines(false);
       table.setGridColor(Color.WHITE);
       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) {
           Border border = null;

                   border = new MultiColorBorder();

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

class MultiColorBorder implements Border {

    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(Color.RED);
            g.drawLine(x, y, x + width, y);

             g.setColor(Color.RED);
            g.drawLine(x + width, y, x + width, y + height);

                     g.setColor(Color.RED);
            g.drawLine(x+width, y+height, x, y + height);


             g.setColor(Color.RED);
            g.drawLine(x, y+ height, x, y);

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

Open in new window






borders3.PNG
Awarded 2011
Awarded 2011

Commented:


>well, I don't see much problem with that either - effectively looks like red borders

I meant that about your picture

In my code and picture produced - at the bottom of above posting - I don't see anything white at all

Author

Commented:
I had to do this:

table.setIntercellSpacing(new Dimension(0, 0));
Awarded 2011
Awarded 2011
Commented:

I think with this class you can create any borders you like.

Maybe you see something in between them - but I don't - I think green and red are next to each
 other in the picture below; maybe ther is one row, maybe they just cannot be closer - I don't
see any practical difference

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, 1);
       table.setShowVerticalLines(false);
       table.setGridColor(Color.WHITE);
       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;

                   border = new MultiColorBorder();
              border.setOpaque(false);

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

class MultiColorBorder implements Border {

    boolean opaque;
     private Color color;

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

    }

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


        public boolean isBorderOpaque() {
            return opaque;

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

               g.setColor(Color.GREEN);
            g.drawLine(x, y, x + width, y);
              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(Color.BLUE);
            g.drawLine(x + width, y, x + width, y + height);

                     g.setColor(Color.RED);

             g.drawLine(x+width, y+height, x, y + height);
            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(Color.YELLOW);
            g.drawLine(x, y+ height, x, y);

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

Open in new window



borders4.PNG