import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicButtonUI;

/**
 * Instructions (Windows-only):
 * 1. Open app
 * 2. Open system Settings app
 * 3. Click "Accessibility"
 * 4. Click "Contrast Themes"
 * 5. Change the theme (for ex: click "Desert" or "Dusk")
 * 6. Click "Apply"
 *
 * Observe in java app the background panel changed color as expected.
 * An exception appears in the console when we asked the button to update its UI.
 *
 * Repeat steps 5 & 6.
 *
 * Observe the background panel no longer changes color.
 *
 * The expected behavior is for the button's exception to not affect future
 * attempts to change the panel's background.
 *
 * (There may (?) be a way to reproduce this on other platforms, but I don't
 * know what system properties to use to trigger this kind of refresh.)
 */
public class PanelUpdateBug extends JFrame {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

                    PanelUpdateBug b = new PanelUpdateBug();
                    b.pack();
                    b.setVisible(true);
                } catch(Throwable t) {
                    t.printStackTrace();
                }
            }
        });
    }

    public PanelUpdateBug() {
        JButton button = new JButton("button");
        button.setUI(new BasicButtonUI() {
            public void uninstallUI(JComponent c) {
                super.uninstallUI(c);
                throw new RuntimeException();
            }
        });

        JPanel p = new JPanel();
        p.setBorder(new EmptyBorder(20,20,20,20));
        p.add(button);

        getContentPane().add(p);
    }
}
