Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is it possible to give JTable cell border(Left,right,Top,Bottom) of different color?

We have a requirement to draw a JTable on a pdf. For this requirement I had taken JTable and directly print onto the pdf. But I didn't get the Left and Top borders for a JTable. Plus I have a requirement to cutomize a cell border for a table. Is there anyway I can give different color borders to a cell in a JTable? For eg:-

Left Border = Grid Color
Top Border = Grid Color
Right Border = Black Color
Bottom Border =  Grid Color

Any suggestions related to this would be very helpful?

like image 905
ajay partoti Avatar asked Dec 11 '22 21:12

ajay partoti


2 Answers

The link @andrewthompson provided should give you an answer to the first part of your question of getting the Table's borders to print (a.k.a. Why does the JTable header not appear in the image?)

To get different color inner borders in the Table (I believe that was your second question), you'll have to use a combination of the MatteBorder and CompoundBorder in conjunction with a TableCellRenderer.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;


public class JTableColoredBorder extends Box{

    public JTableColoredBorder(){
        super(BoxLayout.Y_AXIS);

        JTable table = new JTable(5,5);
        table.setIntercellSpacing(new Dimension(0,0));//Get rid of cell spacing

        //Set your own renderer.  You'll have to set this for Number and Boolean too if you're using those
        CustomRenderer cr = new CustomRenderer(table.getDefaultRenderer(Object.class), Color.red, Color.orange, Color.pink, Color.magenta);
        table.setDefaultRenderer(Object.class, cr);

        add(table);
    }

    //Custom renderer - do what the natural renderer would do, just add a border
    public static class CustomRenderer implements TableCellRenderer{
        TableCellRenderer render;
        Border b;
        public CustomRenderer(TableCellRenderer r, Color top, Color left,Color bottom, Color right){
            render = r;

            //It looks funky to have a different color on each side - but this is what you asked
            //You can comment out borders if you want too. (example try commenting out top and left borders)
            b = BorderFactory.createCompoundBorder();
            b = BorderFactory.createCompoundBorder(b, BorderFactory.createMatteBorder(2,0,0,0,top));
            b = BorderFactory.createCompoundBorder(b, BorderFactory.createMatteBorder(0,2,0,0,left));
            b = BorderFactory.createCompoundBorder(b, BorderFactory.createMatteBorder(0,0,2,0,bottom));
            b = BorderFactory.createCompoundBorder(b, BorderFactory.createMatteBorder(0,0,0,2,right));
        }

        @Override
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
            JComponent result = (JComponent)render.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            result.setBorder(b);
            return result;
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new JTableColoredBorder());
        frame.validate();
        frame.pack();
        frame.setVisible(true);
    }

}
like image 182
Nick Rippe Avatar answered Dec 29 '22 01:12

Nick Rippe


Add a MatteBorder to the table before you print.

Color color = UIManager.getColor("Table.gridColor");
MatteBorder border = new MatteBorder(1, 1, 0, 0, color);
table.setBorder(border);
like image 43
MadProgrammer Avatar answered Dec 29 '22 00:12

MadProgrammer