Reputation: 1432
I am trying to change the background of a JTable
in a swing based GUI. I have added the table to a JScrollPane
. However, the area of the table where there are no cells does not change color. I tried changing the background and foreground color of the scroll pane. However, that does not help either. What component of JTable do I need to edit to change the white background. Below is the part of my code.
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);
}
Code for 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);
Upvotes: 2
Views: 2224
Reputation: 67505
Try this line, that work for me :
mdScrolPane.getViewport().setBackground(UiColors.BACKGROUND_COLOR_DARK);
And try to replace the following code before JscrollPanel`` declaration :
Replace the following code position :
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);
To be Befor :
JScrollPane mdScrolPane = new JScrollPane();
Upvotes: 3