简体   繁体   English

如何设置JTableHeader的外观和颜色

[英]How to set JTableHeader Look and Feel AND Color

this is my first post so any suggestions for how to make my post better would be awesome! 这是我的第一篇文章,所以关于如何改善我的文章的任何建议都将很棒!

With the help of research from this website, I am able to generate a table that I can change the font of, change the alignment of, and maintain the current look and feel. 借助该网站的研究,我可以生成一个表,可以更改其字体,更改其对齐方式并保持当前的外观。 I cannot however, change the color and am unsure what to do. 但是,我无法更改颜色,不确定该怎么办。 Any suggestions would be greatly appreciated! 任何建议将不胜感激!

After creating a JTable, I call: 创建JTable之后,我调用:

table.getTableHeader().setDefaultRenderer(new HeaderRenderer(table)); table.getTableHeader()。setDefaultRenderer(new HeaderRenderer(table));

private class HeaderRenderer implements TableCellRenderer {

    DefaultTableCellRenderer renderer;

    public HeaderRenderer(JTable table) {
        setOpaque(true);//this is an attempt to change color
        renderer = (DefaultTableCellRenderer)
            table.getTableHeader().getDefaultRenderer();
        renderer.setHorizontalAlignment(JLabel.CENTER);
    }

    @Override
    public Component getTableCellRendererComponent(
        JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int col) {
        Component cell = renderer.getTableCellRendererComponent(
                table, value, isSelected, hasFocus, row, col);

        setBackground(headerColor);//this is an attempt to change color
        //cell.setBackground(headerColor);//this is an attempt to change color
        cell.setFont(headerFont);//this successfully changes font
        return cell;
    }
}

Can anyone point me in a direction or help me create a table header that is able to do be colorable, centered, and has a look and feel of "Nimbus"? 谁能指出我的方向或帮助我创建一个表格标题,该表格标题可以着色,居中并且具有“雨云”外观。

All of the Nimbus properties are stored as keys in the UIManager's defaults table. 所有的Nimbus属性都作为键存储在UIManager的默认表中。 You can retrieve and modify any of these values to customize the look and feel of your application. 您可以检索和修改任何这些值,以自定义应用程序的外观。 this page lists all of the Nimbus defaults 页面列出了所有Nimbus默认设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM