我正在尝试在基于swing的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);
答案 0 :(得分:3)
试试这行,对我有用:
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);
成为Befor:
JScrollPane mdScrolPane = new JScrollPane();