JTable 渲染器不改变背景
JTable renderer don't change background
我正在使用 JTable
的以下女儿 class,并且无法更改任何背景色单元格:
@SuppressWarnings("serial")
public abstract class GenericConfigurationTable extends JTable {
private EditableCellRenderer t = new EditableCellRenderer();
private DefaultCellRenderer d = new DefaultCellRenderer();
protected boolean[] editable;
public GenericConfigurationTable(AbstractTableModel m) {
this();
this.setModel(m);
}
public GenericConfigurationTable() {
this.changeSelection(0, 0, false, false);
this.setBackground(ViewsPreferences.P_TABLE_BACKGROUND_COLOR);
this.setShowGrid(false);
this.setPreferredScrollableViewportSize(this.getPreferredSize());
}
protected void setRenderers() {
TableColumnModel u = this.getColumnModel();
for(int i = 0; i < this.getModel().getColumnCount(); i++) {
if(editable[i])
u.getColumn(i).setCellRenderer(t);
else
u.getColumn(i).setCellRenderer(d);
}
}
public class EditableCellRenderer extends JLabel implements TableCellRenderer {
public EditableCellRenderer() {
this.setFont(ViewsPreferences.SU_EDITABLE_CELL_FONT);
this.setForeground(ViewsPreferences.SU_EDITABLE_FOREGROUND_COLOR);
this.setBackground(ViewsPreferences.SU_EDITABLE_BACKGROUND_COLOR);
setBorder(null);
this.setHorizontalAlignment( JLabel.CENTER );
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(value != null)
setText(value.toString());
else
setText("");
return this;
}
}
public class DefaultCellRenderer extends JLabel implements TableCellRenderer {
public DefaultCellRenderer() {
this.setFont(ViewsPreferences.SU_CELL_FONT);
this.setForeground(ViewsPreferences.SU_CELL_FOREGROUND_COLOR);
this.setBackground(ViewsPreferences.SU_CELL_BACKGROUND_COLOR);
setBorder(null);
this.setHorizontalAlignment( JLabel.CENTER );
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(value != null)
setText(value.toString());
else
setText("");
return this;
}
}
}
所以我认为这与我尝试在两个渲染器中渲染任何类型有关,所以我尝试使用 table.setDefaultRenderer(Type.class, myRenderer)
设置渲染器,但也没有任何改变。
这里是使用的 table 模型的一部分:
public class T extends AbstractTableModel {
@SuppressWarnings("rawtypes")
private final Class[] columns_class = { String.class, String.class, Integer.class, Integer.class, String.class, Double.class};
private final String[] columns_names = { "Type", "Champs", "HM", "A", "V", "Fc" };
private final String[] row_labels = {"J1", "J2", "J3", "J4", "J5", "J7", "J8", "J9", "J10", "J11", "J100", " ", " ", "JS", "JC"};
默认情况下 JLabel 是透明的,因此永远不会绘制背景。
要绘制标签的背景,您需要使标签不透明。在渲染器的构造函数中,您需要添加:
this.setOpaque( true );
我正在使用 JTable
的以下女儿 class,并且无法更改任何背景色单元格:
@SuppressWarnings("serial")
public abstract class GenericConfigurationTable extends JTable {
private EditableCellRenderer t = new EditableCellRenderer();
private DefaultCellRenderer d = new DefaultCellRenderer();
protected boolean[] editable;
public GenericConfigurationTable(AbstractTableModel m) {
this();
this.setModel(m);
}
public GenericConfigurationTable() {
this.changeSelection(0, 0, false, false);
this.setBackground(ViewsPreferences.P_TABLE_BACKGROUND_COLOR);
this.setShowGrid(false);
this.setPreferredScrollableViewportSize(this.getPreferredSize());
}
protected void setRenderers() {
TableColumnModel u = this.getColumnModel();
for(int i = 0; i < this.getModel().getColumnCount(); i++) {
if(editable[i])
u.getColumn(i).setCellRenderer(t);
else
u.getColumn(i).setCellRenderer(d);
}
}
public class EditableCellRenderer extends JLabel implements TableCellRenderer {
public EditableCellRenderer() {
this.setFont(ViewsPreferences.SU_EDITABLE_CELL_FONT);
this.setForeground(ViewsPreferences.SU_EDITABLE_FOREGROUND_COLOR);
this.setBackground(ViewsPreferences.SU_EDITABLE_BACKGROUND_COLOR);
setBorder(null);
this.setHorizontalAlignment( JLabel.CENTER );
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(value != null)
setText(value.toString());
else
setText("");
return this;
}
}
public class DefaultCellRenderer extends JLabel implements TableCellRenderer {
public DefaultCellRenderer() {
this.setFont(ViewsPreferences.SU_CELL_FONT);
this.setForeground(ViewsPreferences.SU_CELL_FOREGROUND_COLOR);
this.setBackground(ViewsPreferences.SU_CELL_BACKGROUND_COLOR);
setBorder(null);
this.setHorizontalAlignment( JLabel.CENTER );
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(value != null)
setText(value.toString());
else
setText("");
return this;
}
}
}
所以我认为这与我尝试在两个渲染器中渲染任何类型有关,所以我尝试使用 table.setDefaultRenderer(Type.class, myRenderer)
设置渲染器,但也没有任何改变。
这里是使用的 table 模型的一部分:
public class T extends AbstractTableModel {
@SuppressWarnings("rawtypes")
private final Class[] columns_class = { String.class, String.class, Integer.class, Integer.class, String.class, Double.class};
private final String[] columns_names = { "Type", "Champs", "HM", "A", "V", "Fc" };
private final String[] row_labels = {"J1", "J2", "J3", "J4", "J5", "J7", "J8", "J9", "J10", "J11", "J100", " ", " ", "JS", "JC"};
默认情况下 JLabel 是透明的,因此永远不会绘制背景。
要绘制标签的背景,您需要使标签不透明。在渲染器的构造函数中,您需要添加:
this.setOpaque( true );