Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

java - Nimbus and alternate row colors

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 we want a JTable that alternate Red and Pink rows (and I don't care which color is the first one).

Without redefining custom cellRenderers that perform their own "modulo 2" thing, and without overriding any method from JTable, I want to list the mandatory steps between starting one's application and getting a JTable with custom alternate row colors using Nimbus properties only.

Here are the steps I expected to follow:

  1. Install the Nimbus PLAF
  2. Customize the "Table.background" nimbus property
  3. Customize the "Table.alternateRowColor" nimbus property
  4. Create a JTable with simple data/header
  5. Wrap the jTable in a JScrollPane and add it to the JFrame
  6. Show the JFrame

Here the source code:


public class JTableAlternateRowColors implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JTableAlternateRowColors());
    }

    @Override
    public void run() {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

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

        final JFrame jFrame = new JFrame("Nimbus alternate row coloring");
        jFrame.getContentPane().add(new JScrollPane(new JTable(new String[][] {
                {"one","two","three"},
                {"one","two","three"},
                {"one","two","three"}
        }, new String[]{"col1", "col2", "col3"}
        )));
        jFrame.setSize(400, 300);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setVisible(true);
    }
}

This is JDK6 code. Can somebody tell me goes wrong here?


As per @kleopatra's comment and the contribution of the whole community here's a/the way to get alternate row coloring using only Nimbus properties

public class JTableAlternateRowColors implements Runnable {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new JTableAlternateRowColors());
}

@Override
public void run() {
    try {
        UIManager.setLookAndFeel(new NimbusLookAndFeel());
    } catch (UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }

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

    final JFrame jFrame = new JFrame("Nimbus alternate row coloring");
    final JTable jTable = new JTable(new String[][]{
            {"one", "two", "three"},
            {"one", "two", "three"},
            {"one", "two", "three"}
    }, new String[]{"col1", "col2", "col3"});
    jTable.setFillsViewportHeight(true);
    jFrame.getContentPane().add(new JScrollPane(jTable));
    jFrame.setSize(400, 300);
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    jFrame.setVisible(true);
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...