KaNaDa
KaNaDa

Reputation: 141

UIManager, how to get values of different LookAndFeels?

I need to get the default background color of a TableHeader but of the Windows LookAndFeel. I have already tried:

try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    System.out.println(UIManager.getColor("TableHeader.background"));
catch (Exception e) {
    e.printStackTrace();
}

But it just returns the default color (so the Metal theme one). How do I get the background color of a component from a specific LookAndFeel?

PS.

In this case

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

is the same as

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

EDIT:

Ok I found out, why it returns a "wrong" value for TabHeaader.background. That's because the color I want is not the TabHeader.background or TabHeader.foreground. But do one know how one get the color of the "background"?

EDIT 2:

I found out, that header.setBackground(COLOR); works if you are on default theme. But when I set LookAndFeel on Windows look and feel header.setBackground(COLOR) changes the border color.

Upvotes: 3

Views: 2063

Answers (2)

Marco R.
Marco R.

Reputation: 2720

The Windows look and feel is defined in the LookAndFeel class com.sun.java.swing.plaf.windows.WindowsLookAndFeel. You can use it by invoking the UIManager.setLookAndFeel with fully qualified class name as an argument:

    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        System.out.println(UIManager.getColor("TableHeader.background"));
    } catch (Exception ex) {
        // HANDLE EXCEPTION
    }

This is the list of the available look and feels in the Swing framework: https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#available

Hope this helps.

Upvotes: 1

camickr
camickr

Reputation: 324088

Your code works fine for me. I tried:

try
{
    System.out.println(UIManager.getColor("TableHeader.background"));
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    System.out.println(UIManager.getColor("TableHeader.background"));
}
catch (Exception e) { e.printStackTrace(); }

and got the following output:

javax.swing.plaf.ColorUIResource[r=238,g=238,b=238]
javax.swing.plaf.ColorUIResource[r=240,g=240,b=240]

So there is only a slight difference.

You can also verify the results by checking out UIManager Defaults which displays all the properties for each LAF.

Upvotes: 0

Related Questions