【问题标题】:Change JTable Background Color更改 JTable 背景颜色
【发布时间】:2015-10-24 15:59:38
【问题描述】:

我正在尝试在基于摇摆的 GUI 中更改 JTable 的背景。我已将表格添加到JScrollPane。但是,没有单元格的表格区域不会改变颜色。我尝试更改滚动窗格的背景和前景色。但是,这也无济于事。我需要编辑 JTable 的哪个组件来更改白色背景。以下是我的部分代码。

public class UiColors {
    public static Color BACKGROUND_COLOR_DARK = new Color(30,30,30);
    public static Color BACKGROUND_COLOR_LIGHT = new Color(70,70,70);
    public static Color GOLDEN_TEXT = new Color(255, 215, 0);
}

JTable 代码

JScrollPane mdScrolPane = new JScrollPane();
mdScrolPane.setBackground(UiColors.BACKGROUND_COLOR_DARK);
mdScrolPane.setOpaque(false);
mdScrolPane.setForeground(UiColors.BACKGROUND_COLOR_DARK);
contentPane.add(mdScrolPane, "cell 1 0 1 5,grow");

mdTableModel = new ReadOnlyTableModel();

for (String col : columnNames) {
    mdTableModel.addColumn(col);
}

marketDataTable = new JTable(mdTableModel);
marketDataTable.setFillsViewportHeight(true);
marketDataTable.setToolTipText("Quotes");
marketDataTable.setBorder(null);
marketDataTable.setForeground(new Color(255, 215, 0));
marketDataTable.setBackground(UiColors.BACKGROUND_COLOR_DARK);
marketDataTable.setOpaque(false);
mdScrolPane.setColumnHeaderView(marketDataTable);
mdScrolPane.setViewportView(marketDataTable);

【问题讨论】:

    标签: java swing background jtable


    【解决方案1】:

    试试这条线,对我有用:

    mdScrolPane.getViewport().setBackground(UiColors.BACKGROUND_COLOR_DARK);
    

    并尝试在JscrollPanel``声明之前替换以下代码:

    替换以下代码位置:

    mdTableModel = new ReadOnlyTableModel();
    
    for (String col : columnNames) {
        mdTableModel.addColumn(col);
    }
    
    marketDataTable = new JTable(mdTableModel);
    marketDataTable.setFillsViewportHeight(true);
    marketDataTable.setToolTipText("Quotes");
    marketDataTable.setBorder(null);
    marketDataTable.setForeground(new Color(255, 215, 0));
    marketDataTable.setBackground(UiColors.BACKGROUND_COLOR_DARK);
    marketDataTable.setOpaque(false);
    

    待在:

    JScrollPane mdScrolPane = new JScrollPane();
    

    【讨论】:

    • 完美!非常感谢。我知道它只是我缺少的一个组件。
    猜你喜欢
    • 2012-01-02
    • 2011-09-24
    • 2011-04-21
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 2022-01-24
    相关资源
    最近更新 更多