Why does JMenu not render correctly when using System L&F?

205 Views Asked by At

Bug Report

This has been confirmed as a bug. You can track it here:

https://bugs.openjdk.org/browse/JDK-8258934

Background

  • There's no other code running besides what I've posted in the MCVE; there are no other JVMs active when running this program (though I don't believe that should matter).

  • I do not have any programs installed which customizes the UI of my system.

Target Environment

Java 1.8.0_201
Windows 10 Home Edition

Tested JDKs

  • JDK 8 to JDK 15

Tested by third party

  • George Z. - Windows 7 - Can reproduce bug
  • David Kroukamp - Windows 10, JDK 10 - Can reproduce bug
  • camickr - Windows 10, JDK 11 - Can reproduce bug
  • Canvas (Discord) - Windows 10 - Can reproduce bug
  • Pawnee (Discord) - macOS Catalina 10.15.7, JDK 15 - Unable to reproduce bug

MCVE

import javax.swing.*;

public class Demo {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(Demo::launchUI);
    }

    private static void launchUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        JFrame frame = new JFrame();
        JMenuItem report = new JMenuItem("Report");
        JMenu newMenu = new JMenu("New...");
        JMenu fileMenu = new JMenu("File");
        JMenuBar bar = new JMenuBar();

        newMenu.add(report);
        fileMenu.add(newMenu);
        bar.add(fileMenu);
        frame.setJMenuBar(bar);
        
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Reproduction Steps

  1. Run the MCVE
  2. Click through the menus
  3. Resize the window
  4. Click through the menus again

MCVE Results

Windows 10 machine targeting JDK 11 (though similar results on all tested major Java distributions):

enter image description here

  • Menus give different render results before & after resizing the window
  • There's overlap of the highlighting - the highlight of "New..." bleeds into the "Report" menu item.

  • Sometimes the arrow of "New..." will get overlapped by "Report" as shown below.

Windows 7 - image by George Z

enter image description here

Windows 10, JDK - image by Canvas (Discord)

enter image description here


New Findings

The highlight bug does not appear if part of the "Report" menu item is outside of the windows bounds.

enter image description here

This was achieved by updating the popout offset for menus:

// apply this before creating any components
UIManager.put("Menu.menuPopupOffsetX", 20);
UIManager.put("Menu.menuPopupOffsetY", 20);

Works on...

  • macOS Catalina 10.15.7 - image by Pawnee (Discord)

enter image description here


Question

Does JMenu work properly with System L&F, and if so, what am I missing to get proper results? (No extra spacing, no highlights bleeding into other components)

0

There are 0 best solutions below