Nimbus and alternate row colors

前端 未结 2 871
忘了有多久
忘了有多久 2020-12-17 03:30

I don\'t understand how alternate row coloring works in Nimbus. It seems just crazy!!! I would like to clear things up here.

For the demonstration, let\'s say that <

2条回答
  •  孤城傲影
    2020-12-17 03:55

    Looks like the interference of several bugs ...

    For changing both default table background and default striping, the expected (not only yours, mine as well) configuration of the UIManager (same for all LAFs which respect the alternateRow property) would be:

    UIManager.put("Table.background", Color.RED);
    UIManager.put("Table.alternateRowColor", Color.PINK);
    

    Doesn't work, neither for Metal nor for Nimbus

    • in Metal: no striping, table is all red
    • in Nimbus: striping white/pink, that is the table background is ignored

    Underlying reason for the first can be found in DefaultTableCellRenderer:

    Color background = unselectedBackground != null
                            ? unselectedBackground
                            : table.getBackground();
    if (background == null || background instanceof javax.swing.plaf.UIResource) {
        Color alternateColor = DefaultLookup.getColor(this, ui, "Table.alternateRowColor");
        if (alternateColor != null && row % 2 != 0) {
            background = alternateColor;
        }
    }
    

    It's logic is crooked: the alternate color is only taken if the table's background is a colorUIResource, a rather weak distinction. Anyway, it leads us to next try:

    UIManager.put("Table.background", new ColorUIResource(Color.RED));
    UIManager.put("Table.alternateRowColor", Color.PINK);
    

    This looks fine (except the typical issue with a checkbox renderer, but that's yet another bug story ;-) for metal, still no luck for Nimbus.

    Next step is look up Nimbus defaults which might be related, and apply (after! setting the LAF):

    UIManager.getLookAndFeelDefaults().put("Table:\"Table.cellRenderer\".background", 
        new ColorUIResource(Color.RED));
    

    Edit (as it was asked in the comments)

    JXTable tries to side-step the problem entirely - its means for striping is a Highlighter retrieved from the HighlighterFactory. Needs to go dirty with Nimbus by removing the alternateRowColor property from the lookAndFeelDefaults and add it with a new key "UIColorHighlighter.stripingBackground"

提交回复
热议问题