How is it possible to give JTable cell border(Left

2019-04-12 03:07发布

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?

3条回答
够拽才男人
2楼-- · 2019-04-12 03:24

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);
    }

}
查看更多
家丑人穷心不美
3楼-- · 2019-04-12 03:25

Thanks for all your code. I really appreciate people who really help us by contributing their time and effort. Well all these solutions will solve the problem. I got a very good resolution on this with the below code:-

 package com.swing.data;

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

public class TableExample{   
    public static void main(String[] args) {   
        final JFrame f = new JFrame("TableExample");   
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        f.getContentPane().add(createTable());   
        f.pack();   
        SwingUtilities.invokeLater(new Runnable(){   
            public void run() {   
                f.setLocationRelativeTo(null);   
                f.setVisible(true);   
            }   
        });   
    }   

    static JComponent createTable() {   
        final JTable table = new JTable(9,9) {   
            private static final long serialVersionUID = 0;   

            Color B = Color.RED;   
            Color C = this.getGridColor();   
            final Border[][] borders = {   
                {new ZoneBorder(C,B,B,B), new ZoneBorder(B,C,C,C), new ZoneBorder(B,B,C,C)},   
                {new ZoneBorder(C,C,C,B), new ZoneBorder(C,C,C,C), new ZoneBorder(C,B,C,C)},   
                {new ZoneBorder(C,C,B,B), new ZoneBorder(C,C,B,C), new ZoneBorder(C,B,B,C)}   
            };   

            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {   
                Component result = super.prepareRenderer(renderer, row, column);   
                if (result instanceof JComponent) {   
                    if(row == 0 && column == 0)
                    ((JComponent) result).setBorder(borders[0][0]);   
                }   
                return result;   
            }   
        };   
        table.setRowHeight(28);   
        //table.setGridColor(Color.BLACK);   
        TableColumnModel tcm = table.getColumnModel();   
        for(int c = 0; c<table.getColumnCount(); ++c) {   
            TableColumn tc = tcm.getColumn(c);   
            tc.setPreferredWidth(28);   
        }   
        JPanel inner = new JPanel(new GridLayout());   
        //inner.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));   
        inner.add(table);   
        return inner;   
    }   
}  

    package com.swing.data;

import java.awt.*;   
import javax.swing.*;   
import javax.swing.border.*;   

public class ZoneBorder implements Border {   
    private static final int WIDTH = 1;   
    private Color colorN, colorE, colorS, colorW;   

    public ZoneBorder(Color colorN, Color colorE, Color colorS, Color colorW) {   
        this.colorN=colorN;   
        this.colorE=colorE;   
        this.colorS=colorS;   
        this.colorW=colorW;   
    }   

    public boolean isBorderOpaque() {   
        return false;   
    }   

    public Insets getBorderInsets(Component c) {   
        return new Insets(WIDTH,WIDTH,WIDTH,WIDTH);   
    }   

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {   
        Color old = g.getColor();   
        if (colorN != null) {   
            g.setColor(colorN);   
            g.fillRect(x, y, width, WIDTH);   
        }   
        if (colorE != null) {   
            g.setColor(colorE);   
            g.fillRect(x+width-WIDTH, y, WIDTH, height);   
        }   
        if (colorS != null) {   
            g.setColor(colorS);   
            g.fillRect(x, y+height-WIDTH, width, WIDTH);   
        }   
        if (colorW != null) {   
            g.setColor(colorW);   
            g.fillRect(x, y, WIDTH, height);   
        }   
        g.setColor(old);   
    }   
}  

Thanks!

查看更多
Viruses.
4楼-- · 2019-04-12 03:32

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);
查看更多
登录 后发表回答