【问题标题】:How to remove the borders of column headers in JTable?如何去除 JTable 中列标题的边框?
【发布时间】:2011-12-10 00:11:42
【问题描述】:

我的问题是关于删除 JTable 中列标题的边框。

我的班级扩展了 JTable,因此我设法消除了所有其他边框:

super.setShowHorizontalLines(false);
super.setShowVerticalLines(false);

但这不会删除列标题中的边框。如何做到这一点?

【问题讨论】:

    标签: java swing jtable


    【解决方案1】:

    您必须从TableHeader 中提取JLabel 并设置Border,例如,Borders 仅针对第一个进行更改。 Column

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.table.*;
    
    public class LabelHeaderSample {
    
        public static void main(String args[]) {
            Icon redIcon = new DiamondIcon(Color.red);
            Icon blueIcon = new DiamondIcon(Color.blue);
            Object rows[][] = {{"one", "ichi - \u4E00", new DiamondIcon(Color.red)},
                {"two", "ni - \u4E8C", new DiamondIcon(Color.red)},
                {"three", "san - \u4E09", new DiamondIcon(Color.red)},
                {"four", "shi - \u56DB", new DiamondIcon(Color.red)},
                {"five", "go - \u4E94", new DiamondIcon(Color.red)},
                {"six", "roku - \u516D", new DiamondIcon(Color.red)},
                {"seven", "shichi - \u4E03", new DiamondIcon(Color.red)},
                {"eight", "hachi - \u516B", new DiamondIcon(Color.red)},
                {"nine", "kyu - \u4E5D", new DiamondIcon(Color.red)},
                {"ten", "ju - \u5341", new DiamondIcon(Color.red)}};
            JFrame frame = new JFrame("Label Header");
            String headers[] = {"English", "Japanese", "Icon"};
            JTable table = new JTable(rows, headers);
            JScrollPane scrollPane = new JScrollPane(table);
            Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");
            JLabel blueLabel = new JLabel(headers[0], blueIcon, JLabel.CENTER);
    
            blueLabel.setBorder(null);// < - - - here you set Borders
    
            JLabel redLabel = new JLabel(headers[1], redIcon, JLabel.CENTER);
            redLabel.setBorder(headerBorder);
            TableCellRenderer renderer = new JComponentTableCellRenderer();
            TableColumnModel columnModel = table.getColumnModel();
            TableColumn column0 = columnModel.getColumn(0);
            TableColumn column1 = columnModel.getColumn(1);
            TableColumn column2 = columnModel.getColumn(2);
            column0.setHeaderRenderer(renderer);
            column0.setHeaderValue(blueLabel);
            column1.setHeaderRenderer(renderer);
            column1.setHeaderValue(redLabel);
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
        }
    
        private LabelHeaderSample() {
        }
    }
    
    class DiamondIcon implements Icon {
    
        private Color color;
        private boolean selected;
        private int width;
        private int height;
        private Polygon poly;
        private static final int DEFAULT_WIDTH = 10;
        private static final int DEFAULT_HEIGHT = 10;
    
        public DiamondIcon(Color color) {
            this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);
        }
    
        public DiamondIcon(Color color, boolean selected) {
            this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);
        }
    
        public DiamondIcon(Color color, boolean selected, int width, int height) {
            this.color = color;
            this.selected = selected;
            this.width = width;
            this.height = height;
            initPolygon();
        }
    
        private void initPolygon() {
            poly = new Polygon();
            int halfWidth = width / 2;
            int halfHeight = height / 2;
            poly.addPoint(0, halfHeight);
            poly.addPoint(halfWidth, 0);
            poly.addPoint(width, halfHeight);
            poly.addPoint(halfWidth, height);
        }
    
        public int getIconHeight() {
            return height;
        }
    
        public int getIconWidth() {
            return width;
        }
    
        public void paintIcon(Component c, Graphics g, int x, int y) {
            g.setColor(color);
            g.translate(x, y);
            if (selected) {
                g.fillPolygon(poly);
            } else {
                g.drawPolygon(poly);
            }
            g.translate(-x, -y);
        }
    }
    
    class JComponentTableCellRenderer implements TableCellRenderer {
    
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            return (JComponent) value;
        }
    }
    

    【讨论】:

    • 如果我使用定义标题的 AbstractTableModel 而不是扩展 JTable 的类怎么办?
    • 从 DefaultTableModel 或 AbstractTableModel 获取 TableHeader 的任何区别,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2012-09-07
    • 2017-03-16
    • 1970-01-01
    • 2016-12-19
    相关资源
    最近更新 更多