javaswinglook-and-feeljpopupmenu

Different behavior in WindowsLookAndFeel


I want to show a popupmenu, while using Metal L&F it will do these behavior.

Yes it is I needed. But when using WindowsLookAndFeel, it not same as those. when twice pressed it just hide menu and not printed "pressed". why it have difference behavior between two L&F?

 import javax.swing.*;
 import java.awt.event.*;
 
 public class Popup {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(300, 300);
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        JPopupMenu menu = new JPopupMenu();
        menu.add("item");
        f.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                System.out.println("pressed");
            }
        });
        f.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                menu.show(f, e.getX(), e.getY());
            }
        });
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
 }

Solution

  • boolean consumeEvent = UIManager.getBoolean("PopupMenu.consumeEventOnClose");
    // Consume the event so that normal processing stops.
    if(consumeEvent && !(src instanceof MenuElement)) {
        me.consume();
    }
    

    I have found the problems, because of this property is different so end it