Vince
Vince

Reputation: 15146

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

Bug Report

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

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

Background

Target Environment

Java 1.8.0_201
Windows 10 Home Edition

Tested JDKs

Tested by third party


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


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...

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)

Upvotes: 5

Views: 221

Answers (0)

Related Questions