我正在使用Nimbus。为了简化在JTable上覆盖prepareRenderer的事情,我通常做的第一件事就是从super方法中获取Component,所以我只能调整我想要的属性,同时保留其他所有属性。
问题是,一旦我更改了该组件上的样式属性(如背景颜色),它就会使用该颜色执行每个渲染,因为它显然正在重用该JLabel。所以呈现的每个新单元都变成红色,而不仅仅是我想要的单元。
我总是使用HTML来避免这种情况,但我需要操纵JLabel的前景/背景属性。有没有人有一个代码块来设置渲染器的所有默认属性,我可以在下面的 applyDefaults 方法占位符中使用它?
我正在使用Nimbus所以我不知道这是否会让事情变得更难。但是我希望将选定/未选定和奇数/偶数行的前景/背景渲染为其默认值,然后在此之后对所有内容进行操作。
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
final Component c =super.prepareRenderer(renderer, row, col);
applyDefaults(c, row, col); //what do I need to restore default styles here?
boolean highlightRed condtion == //some boolean condition
boolean isSelected = //calculate whether row is selected
if (c instanceof JLabel) {
if (isSelected == false && highlightRed) {
((JLabel) c).setBackground(Color.RED);
}
}
更新:看了camickr的建议,我为Nimbus JTables构建了一个方便的默认界面。 restoreDefaultRenderer将所有属性重置为默认的Nimbus颜色和对齐方式。
public interface JTableCustomRender {
static final Color foreGround = Color.black;
static final Color oddBackGround = new Color(255,255,255);
static final Color evenBackGround = new Color(242,242,242);
static final Color selectedForeGround = new Color(255,255,255);
static final Color selectedBackGround = new Color(57,105,138);
//static final ImmutableList<Class<?>> leftAlignTypes = ImmutableList.of(String.class, Date.class, DateTime.class);
static final ImmutableList<Class<?>> rightAlignTypes = ImmutableList.of(Integer.class, Double.class, Float.class, BigDecimal.class);
public Component prepareRenderer(TableCellRenderer renderer, int viewRow, int viewCol);
public default Component restoreDefaultRenderer(JTable table, Component c, int viewRow, int viewCol) {
if (c instanceof JLabel) {
final boolean rowSelected = Arrays.stream(table.getSelectedRows()).filter(i -> i == viewRow).findAny().isPresent();
JLabel label = (JLabel) c;
if (rowSelected) {
label.setForeground(selectedForeGround);
label.setBackground(selectedBackGround);
}
else {
if (viewRow % 2 == 0) {
label.setBackground(evenBackGround);
}
else {
label.setBackground(oddBackGround);
}
label.setForeground(foreGround);
}
Object value = table.getValueAt(viewRow, viewCol);
if(rightAlignTypes.contains(value.getClass())) {
label.setHorizontalAlignment(SwingConstants.RIGHT);
}
else {
label.setHorizontalAlignment(SwingConstants.LEFT);
}
}
return c;
}
}
答案 0 :(得分:2)
它之后使用该颜色进行每一次渲染,因为它显然正在重用JLabel。
正确,这就是为什么你需要一个else语句将背景重置回表默认值。
查看Table Row Rendering有关如何执行此操作的示例,以便您也考虑行的选择颜色。
是否有任何人设置了一个代码块来设置渲染器的所有默认属性
基本代码是:
c.setBackground( getBackground() );
c.setForeground( getForeground() );
您可以为手动绑定的任何属性执行此操作。
答案 1 :(得分:0)
这是一个覆盖方法prepareRenderer()的示例代码,希望它可以帮助某人:
public class MyJTable extends JTable {
private final Color lineSelectedBackgroundColor = new Color(153,204,255);
private final Color cellSelectedBackgroundColor = new Color(51,153,255);
private final Color cellSelectedForeGroundColor = Color.BLACK;
private final Color oddRowBackgroundColor = new Color(224,224,224);
private final Color evenRowBackgroundColor = Color.WHITE;
private final int lowThreshold = 33;
private final int highThreshold = 66;
private final boolean debugEnabled = true;
private void log(String msg){ if(debugEnabled){ System.out.println(msg); } }
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
JComponent component = (JComponent) super.prepareRenderer(renderer, row, column);
int selectedRow = super.getSelectedRow();
int selectedColumn = super.getSelectedColumn();
if(row==selectedRow && column==selectedColumn){
component.setForeground(cellSelectedForeGroundColor);
component.setBackground(cellSelectedBackgroundColor);
}else if (row==selectedRow){
component.setBackground(lineSelectedBackgroundColor);
}else{
if(row % 2 == 0) {
component.setBackground(evenRowBackgroundColor);
}else{
component.setBackground(oddRowBackgroundColor);
}
}
if(column==AppData.hyundaiTableStockIndex) {
String stockStr = getValueAt(row, AppData.hyundaiTableStockIndex).toString();
try{
int stockValue = Integer.valueOf(stockStr);
if (stockValue < lowThreshold) {
component.setBackground(Color.RED);
} else if (stockValue > lowThreshold && stockValue < highThreshold) {
component.setBackground(Color.YELLOW);
} else if (stockValue > highThreshold) {
component.setBackground(Color.GREEN);
}
}catch (Exception ex){
log("parsing failed.");
}
}
return component;
}
}