怎样才能给出不同颜色的JTable单元边框(左,右,上,下)?

aja*_*oti 4 java pdf swing border jtable

我们需要在pdf上绘制JTable.对于这个要求,我采用了JTable并直接打印到pdf上.但是我没有获得JTable的Left和Top边框.另外,我需要为表格切割单元格边框.无论如何,我可以为JTable中的单元格赋予不同的颜色边框吗?例如: -

Left Border = Grid Color
Top Border = Grid Color
Right Border = Black Color
Bottom Border =  Grid Color
Run Code Online (Sandbox Code Playgroud)

任何与此相关的建议都会非常有用吗?

Nic*_*ppe 6

@andrewthompson提供的链接应该能够让你回答问题的第一部分,即打开Table的边框(也就是为什么JTable标题没有出现在图像中?)

为了在表格中获得不同颜色的内部边界(我相信这是你的第二个问题),你必须结合使用MatteBorderCompoundBorder结合使用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);
    }

}
Run Code Online (Sandbox Code Playgroud)


Mad*_*mer 5

在打印之前向表中添加MatteBorder.

Color color = UIManager.getColor("Table.gridColor");
MatteBorder border = new MatteBorder(1, 1, 0, 0, color);
table.setBorder(border);
Run Code Online (Sandbox Code Playgroud)