Search code examples
javaswingflatlaf

Get value for a FlatLaf theme


I have a little problem getting certain values ​​contained in the themes of flatlaf, I'll give you an example, the value of "@accentBase2Color" return null and i don't understand why because the theme contain this key (sry for my bad english).

public static void main(String[] args) throws UnsupportedLookAndFeelException {
    UIManager.setLookAndFeel(flatLaf);
    
    Color obj = UIManager.getColor("@accentBaseColor");
    
    System.out.println(obj);
}

Solution

  • @accentBase2Color exist only in the theme file so it is unreachable from the UIManager. You can use the following code to reach every UIManager key/color attribut, I think the color you want to find is stored in "Object.pink" or "Object.purple" for exemple.

    private static void DrawGizmos(String search) {
    
        UIDefaults defaults = UIManager.getDefaults();
        System.out.println(defaults.size() + " properties defined !");
        Map<String, Color> mapString = new HashMap<>();
        for (Enumeration<Object> e = defaults.keys(); e.hasMoreElements();) {
            Object key = e.nextElement();
    
            if (key.toString().toUpperCase().contains(search.toUpperCase())) {
                if (defaults.get(key) instanceof Color) {
                    mapString.put(key.toString(), (Color) defaults.get(key));
                }
            }
        }
    
        JPanel jPanel = new JPanel();
    
        for (Map.Entry<String, Color> entry:mapString.entrySet()){
            JButton btn = new JButton(entry.getKey() + " |-| " + entry.getValue().toString());
            btn.setBackground(entry.getValue());
            jPanel.add(btn);
        }
    
        JFrame p = new JFrame("UIManager");
        jPanel.setLayout(new VerticalLayout());
        p.add(new JScrollPane(jPanel));
        p.setVisible(true);
        p.setSize(400, 500);
    }
    
    
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(new FlatCarbonIJTheme());
        } catch (UnsupportedLookAndFeelException e) {
            throw new RuntimeException(e);
        }
    
        DrawGizmos("Objects");
    }