JTable 表头背景颜色

Posted

技术标签:

【中文标题】JTable 表头背景颜色【英文标题】:JTable header background color 【发布时间】:2022-01-24 03:11:39 【问题描述】:

我正在尝试在 JTable 标题上添加背景颜色,但它似乎不会改变标题的颜色这是我的 JTable 代码.. 我做错了什么?

Color headerColor = new Color(25, 78, 132);
        itemTable = new JTable()

        public boolean isCellEditable(int row, int column)                 
            return false;               
        ;
        Color rowColor1 = new Color(99, 154, 206);
        Color rowColor2 = new Color(148, 186, 231);
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) 
            Component stamp = super.prepareRenderer(renderer, row, column);
            if (row % 2 == 0)
                stamp.setBackground(rowColor1);
            else
                stamp.setBackground(rowColor2);
            return stamp;
        
        ;
        JTableHeader Header =  itemTable.getTableHeader();
        Header.setBackground(headerColor);
        Header.setReorderingAllowed(false);
        itemTable.setFont(fpanelItemTable);
        itemTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
        //itemTable.setPreferredScrollableViewportSize(new Dimension(200,200));
        //itemTable.setFillsViewportHeight(true);
        itemTable.setAutoCreateRowSorter(true);

【问题讨论】:

注意 prepareRenderer 不适合这项工作 我应该把它放在哪里?我希望我的标题和行有颜色 【参考方案1】:

我应该把它放在哪里?我希望我的标题和行有颜色

例如,三种可能的方式之一

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

public class SelectedTableHeader 

    private JFrame frame = new JFrame("Table Demo");
    private JTableHeader header;
    private Object selectedColumn = null;
    private String[] columnNames = "String", "Integer", "Float", "Double", "Locale & Double", "Boolean";
    private Object[][] data = 
        "aaa", new Integer(12), new Float(12.15), new Double(100.05), new Double(12.05), true,
        "bbb", new Integer(5), new Float(7.154), new Double(6.1555), new Double(417.55), false,
        "CCC", new Integer(92), new Float(0.1135), new Double(3.1455), new Double(11.05), true,
        "ddd", new Integer(12), new Float(31.15), new Double(10.05), new Double(23.05), true,
        "eee", new Integer(5), new Float(5.154), new Double(16.1555), new Double(17.55), false,
        "fff", new Integer(92), new Float(4.1135), new Double(31.1455), new Double(3.05), true;
    private TableModel model = new DefaultTableModel(data, columnNames) 
        private static final long serialVersionUID = 1L;

        @Override
        public Class<?> getColumnClass(int column) 
            return getValueAt(0, column).getClass();
        
    ;
    private JTable table = new JTable(model) 

            private static final long serialVersionUID = 1L;

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) 
                Component c = super.prepareRenderer(renderer, row, column);
                int firstRow = 0;
                int secondRow = 0;
                int lastRow = table.getRowCount() - 1;
                if (row == lastRow) 
                    ((JComponent) c).setBackground(Color.GREEN);
                 else if (row == firstRow) 
                    ((JComponent) c).setBackground(Color.YELLOW);
                 else 
                    ((JComponent) c).setBackground(table.getBackground());
                
                return c;
            
        ;

    public SelectedTableHeader() 
        header = table.getTableHeader();
        header.addMouseListener(new MouseAdapter() 
            @Override
            public void mouseClicked(MouseEvent e) 
                JTableHeader h = (JTableHeader) e.getSource();
                int i = h.columnAtPoint(e.getPoint());
                Object o = h.getColumnModel().getColumn(i).getHeaderValue();
                if (i < 0) 
                    selectedColumn = null;
                    return;
                
                selectedColumn = o;
                h.requestFocusInWindow();
            
        );
        final TableCellRenderer hr = table.getTableHeader().getDefaultRenderer();
        header.setDefaultRenderer(new TableCellRenderer() 
            private JLabel lbl;
            @Override
            public Component getTableCellRendererComponent(
                    JTable table, Object value, boolean isSelected, boolean hasFocus, 
                    int row, int column) 
                if (selectedColumn == value) 
                    lbl = (JLabel) hr.getTableCellRendererComponent(table, value, 
                            true, true, row, column);
                    lbl.setBorder(BorderFactory.createCompoundBorder(
                            BorderFactory.createEmptyBorder(1, 1, 1, 1), 
                            BorderFactory.createLineBorder(Color.red, 1)));
                    lbl.setHorizontalAlignment(SwingConstants.LEFT);
                    lbl.setBackground(lbl.getBackground());
                 else 
                    lbl = (JLabel) hr.getTableCellRendererComponent(table, value, 
                            false, false, row, column);
                    lbl.setBorder(BorderFactory.createCompoundBorder(
                            lbl.getBorder(), 
                            BorderFactory.createEmptyBorder(1, 1, 1, 1)));
                    lbl.setHorizontalAlignment(SwingConstants.CENTER);
                    lbl.setBackground(Color.ORANGE);
                
                if (column == 0) 
                    lbl.setForeground(Color.red);
                 else 
                    lbl.setForeground(header.getForeground());
                
                /*return (value == selectedColumn) ? hr.getTableCellRendererComponent(
                 table, value, true, true, row, column) : hr.getTableCellRendererComponent(
                 table, value, false, false, row, column);*/
                return lbl;
            
        );
        table.setRowHeight(20);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scroll = new JScrollPane(table);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scroll);
        frame.pack();
        frame.setLocation(150, 150);
        frame.setVisible(true);
    

    public static void main(String[] args) 
        EventQueue.invokeLater(new Runnable() 
            @Override
            public void run() 
                SelectedTableHeader sTH = new SelectedTableHeader();
            
        );
    

【讨论】:

谢谢你。所以我应该将单元格和标题渲染器分开,对吗?我还有一个问题,为什么当我在标题上涂上颜色时,双头箭头不可见?我怎样才能让它再次可见? cell and header renderer am I right? ---> 是的,why is the double head arrow is not visible when there I put color on the header? ---> 请问是什么???不知道没有细节......,你的意思是排序图标还是??? 是的。当标题没有颜色时,排序图标或双头箭头可见,然后当标题有颜色时,图标不会出现..为什么?

以上是关于JTable 表头背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

更改 JTable 中行的背景颜色

用搜索词过滤JTable而不丢失背景颜色

JTable设置表格背景颜色——隔行不同

根据列值更改 JTable 行的背景颜色

背景排序JTable后,特定值的颜色不会改变

JTable 单元格颜色