JTable- 在应用默认的 Nimbus 样式时覆盖 prepareRenderer?

JTable- Overriding prepareRenderer while applying the default Nimbus styling?

我正在使用 Nimbus。为了通过重写 JTable 上的 prepareRenderer 使事情变得简单,我通常做的第一件事是从 super 方法中获取组件,这样我就可以只调整我想要的属性,同时保持其他一切不变。

问题是,一旦我更改了该组件上的样式 属性,例如背景颜色,它会在之后使用该颜色进行每次渲染,因为它显然正在重用该 JLabel。所以每一个渲染的新单元格都会变成红色,而不仅仅是我想要的那个。

我一直使用 HTML 来避免这种情况,但我需要操纵 JLabel 的 foreground/background 属性。有没有人有一段代码可以设置渲染器的所有默认属性,我可以在下面的 applyDefaults 方法占位符中使用它?

我正在使用 Nimbus,所以我不知道这是否会使事情变得更难。但我希望 foreground/background 的 selected/unselected 和 odd/even 行呈现为默认值,然后一切都被操纵。

@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;
    }

}

it does every single render after that with that color because it apparently is reusing that JLabel.

正确,这就是为什么您需要 else 语句将背景重置回 table 默认值的原因。

查看 Table Row Rendering 示例,了解如何执行此操作,这样您也可以考虑行的选择颜色。

Does anybody have a block of code that sets all the default properties for a renderer

基本代码为:

c.setBackground( getBackground() );
c.setForeground( getForeground() );

您可以对您手动绑定覆盖的任何 属性 执行此操作。

下面是一个覆盖方法 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;
    }
}