gpt4 book ai didi

java - JTable 渲染器不改变背景

转载 作者:行者123 更新时间:2023-12-01 10:51:51 25 4
gpt4 key购买 nike

我正在使用 JTable 的以下子类,并且无法更改任何背景颜色单元格:

@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) 设置渲染器,但也没有任何改变。

这是所使用的表模型的一部分:

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 );

关于java - JTable 渲染器不改变背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com