4

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

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

与此相关的任何建议会非常有帮助吗?

4

3 回答 3

7

@andrewthompson 提供的链接应该可以回答您的问题的第一部分,即打印表格的边框(也就是为什么 JTable 标题没有出现在图像中?

要在表格中获得不同颜色的内边框(我相信这是您的第二个问题),您必须将MatteBorderandCompoundBorder与 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);
    }

}
于 2012-10-11T19:21:41.383 回答
6

在打印之前将 MatteBorder 添加到表格中。

Color color = UIManager.getColor("Table.gridColor");
MatteBorder border = new MatteBorder(1, 1, 0, 0, color);
table.setBorder(border);
于 2012-10-11T18:55:44.687 回答
0

感谢您的所有代码。我非常感谢那些通过贡献时间和精力真正帮助我们的人。那么所有这些解决方案都将解决问题。我用下面的代码得到了一个很好的解决方案: -

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

谢谢!

于 2012-10-12T10:34:54.700 回答