Skip to content

Default JPanel rendered unstyled in JOptionPane #761

Closed
@ArthurKwasny

Description

@ArthurKwasny

The default JPanel from the javax.swing package won't render with its applied style, if it is displayed in a JOptionPane / JDialog (magenta / red panel).
If using an anonymous or named sub class of JPanel, the panel will be rendered without any issues (green / blue panel).

In a JFrame all panels are rendered as expected (top window):
image

It does not matter, how styling is applied, e.g. setting background via JPanel.setBackground(Color) will also not render the correct background color.

Tested with FlatLaf 3.2.5, Adoptium JDK 21 on Windows 10.

Test code:

import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.FlatLightLaf;

public class OptionPaneBug {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            FlatLightLaf.setup();

            // correct rendering:
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            frame.setContentPane(createPanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            // fails to render main / r panel with its applied style:
            JOptionPane.showMessageDialog(frame, createPanel());
        });
    }

    static JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setBackground(Color.MAGENTA);

        // default class
        JPanel r = new JPanel();
        r.putClientProperty(FlatClientProperties.STYLE,
                "background: #ff0000; border: 12,12,12,12;");
        panel.add(r);

        // anonymous sub class
        JPanel g = new JPanel() {};
        g.putClientProperty(FlatClientProperties.STYLE,
                "background: #00ff00; border: 12,12,12,12;");
        panel.add(g);

        // named sub class
        JPanel b = new TestPanel();
        b.putClientProperty(FlatClientProperties.STYLE,
                "background: #0000ff; border: 12,12,12,12;");
        panel.add(b);

        return panel;
    }
}

and

public class TestPanel extends javax.swing.JPanel {}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions