JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.42k stars 272 forks source link

Window icon not applied to OptionPane #886

Closed TheCardinalSystem closed 1 month ago

TheCardinalSystem commented 2 months ago

I want to display a custom window icon on some of my option panes, so I created a custom method which takes an Image parameter and shows a JOptionPane:

public static int showOptionDialog(Component parentComponent, Object message, String title, int optionType,
        int messageType, Icon icon, Image windowIcon, Object[] options, Object initialValue)
        throws HeadlessException {
    JOptionPane pane = new JOptionPane(message, messageType, optionType, icon, options, initialValue);
    pane.setInitialValue(initialValue);

    JDialog dialog = pane.createDialog(parentComponent, title);
    pane.selectInitialValue();

    dialog.setIconImage(windowIcon);
    dialog.show();
    dialog.dispose();

    Object selectedValue = pane.getValue();

    if (selectedValue == null)
        return JOptionPane.CLOSED_OPTION;
    if (options == null) {
        if (selectedValue instanceof Integer)
            return ((Integer) selectedValue).intValue();
        return JOptionPane.CLOSED_OPTION;
    }
    for (int counter = 0, maxCounter = options.length; counter < maxCounter; counter++) {
        if (options[counter].equals(selectedValue))
            return counter;
    }
    return JOptionPane.CLOSED_OPTION;
}

This works when using a built-in LAF, but when I switch to FlatLaf the icon vanishes. I discovered the TitlePane.icon key from this issue, but this affects all option panes. I only want to set icons on specific option panes, and these icons will differ depending on the information displayed.

Can FlatLaf be modified to acknowledge icons set with setIconImages?

DevCharly commented 1 month ago

You can show icon for single (JOptionPane) dialog with:

dialog.getRootPane().putClientProperty( FlatClientProperties.TITLE_BAR_SHOW_ICON, true );

Or show icon for all JOptionPanes with:

UIManager.put( "OptionPane.showIcon", true );