-
Bug
-
Resolution: Unresolved
-
P3
-
7
-
generic
-
generic
Please run the test case provided below
Note: package name is important to make it work
Click to the menu and click any MenuItem
in console you'll see "Modifiers - 0"
Now make the frame big enough to completely contain popup menu
(we need to make it lightweight)
Click to the menu and click any MenuItem
in console you'll see "Modifiers - 16"
This is the problem,
the result has to be the same for both cases
See evalutaion for more details
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalRootPaneUI;
public class Sample extends JFrame {
public static class CustomRootPaneUI extends MetalRootPaneUI {
public static ComponentUI createUI(JComponent c) {
return new CustomRootPaneUI();
}
HierarchyListener hl;
@Override
protected void installListeners(JRootPane root) {
super.installListeners(root);
this.hl = new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
System.out.println("Root pane hierarchy changed");
}
};
// comment the following line to have correct modifiers
// on the menu action listeners
root.addHierarchyListener(this.hl);
}
@Override
protected void uninstallListeners(JRootPane root) {
root.removeHierarchyListener(this.hl);
this.hl = null;
super.uninstallListeners(root);
}
}
public static class CustomLookAndFeel extends MetalLookAndFeel {
@Override
protected void initClassDefaults(UIDefaults table) {
super.initClassDefaults(table);
table.put("RootPaneUI", "fromKirill.Sample$CustomRootPaneUI");
}
@Override
public String getID() {
return "Custom";
}
}
public Sample() {
super("Sample");
JMenuBar jmb = new JMenuBar();
JMenu menu = new JMenu("Menu");
for (int i = 0; i < 5; i++) {
JMenuItem jmi = new JMenuItem("Shift-click me " + i) {
};
jmi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Modifiers - " + e.getModifiers());
}
});
menu.add(jmi);
}
jmb.add(menu);
this.setJMenuBar(jmb);
this.setSize(100, 100);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(new CustomLookAndFeel());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Sample().setVisible(true);
}
});
}
}
Note: package name is important to make it work
Click to the menu and click any MenuItem
in console you'll see "Modifiers - 0"
Now make the frame big enough to completely contain popup menu
(we need to make it lightweight)
Click to the menu and click any MenuItem
in console you'll see "Modifiers - 16"
This is the problem,
the result has to be the same for both cases
See evalutaion for more details
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.MetalRootPaneUI;
public class Sample extends JFrame {
public static class CustomRootPaneUI extends MetalRootPaneUI {
public static ComponentUI createUI(JComponent c) {
return new CustomRootPaneUI();
}
HierarchyListener hl;
@Override
protected void installListeners(JRootPane root) {
super.installListeners(root);
this.hl = new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
System.out.println("Root pane hierarchy changed");
}
};
// comment the following line to have correct modifiers
// on the menu action listeners
root.addHierarchyListener(this.hl);
}
@Override
protected void uninstallListeners(JRootPane root) {
root.removeHierarchyListener(this.hl);
this.hl = null;
super.uninstallListeners(root);
}
}
public static class CustomLookAndFeel extends MetalLookAndFeel {
@Override
protected void initClassDefaults(UIDefaults table) {
super.initClassDefaults(table);
table.put("RootPaneUI", "fromKirill.Sample$CustomRootPaneUI");
}
@Override
public String getID() {
return "Custom";
}
}
public Sample() {
super("Sample");
JMenuBar jmb = new JMenuBar();
JMenu menu = new JMenu("Menu");
for (int i = 0; i < 5; i++) {
JMenuItem jmi = new JMenuItem("Shift-click me " + i) {
};
jmi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Modifiers - " + e.getModifiers());
}
});
menu.add(jmi);
}
jmb.add(menu);
this.setJMenuBar(jmb);
this.setSize(100, 100);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(new CustomLookAndFeel());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Sample().setVisible(true);
}
});
}
}
- relates to
-
JDK-6637862 Modifiers in ActionEvent wrong when ContainerListener is present
- Open