I'm not sure if the problem is with my code or with Java 1.7.
In the below code (based largely on the Java Popup Demo), the popup will appear upon a mouse right click. The popup menu item will get highlighted upon mouse roll-over, and clicking on the JmenuItem gets the popup to disappear; however, the actionEvent of the JMenuItem is not fired upon clicking (which should be reported in the JTextArea).
Other nuggets: If I type the mnemonic for the JMenuItem (here "a"), then the actionEvent is fired (the event is reported in the JTextArea).
If I do not attach a custom Popup(Factory), then a mouse click fires the actionEvent as expected.
I'm using OSX 10.7.5
This problem occurs with:
java version "1.7.0_07" Java(TM) SE Runtime Environment (build 1.7.0_07-b10) Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)
This code behaves fine with:
java version "1.6.0_33" Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-11M3720) Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)
Any help/thoughts are greatly appreciated! Self-contained code example is below.
Thanks
Andrew
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PopupMenuDemo implements ActionListener {
JTextArea output;
JScrollPane scrollPane;
String newline = "\n";
public Container createContentPane() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setOpaque(true);
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
contentPane.add(scrollPane, BorderLayout.CENTER);
return contentPane;
}
public void createPopupMenu() {
JMenuItem menuItem;
JPopupMenu popup = new JPopupMenu();
menuItem = new JMenuItem("A popup menu item", 'a');
menuItem.addActionListener(this);
popup.add(menuItem);
MouseListener popupListener = new PopupListener(popup);
output.addMouseListener(popupListener);
}
public void actionPerformed(ActionEvent e) {
JMenuItem source = (JMenuItem)(e.getSource());
String s = "Action event detected."
+ newline
+ " Event source: " + source.getText()
+ " (an instance of " + source.getClass().getName() + ")";
output.append(s + newline);
output.setCaretPosition(output.getDocument().getLength());
}
private void createAndShowGUI() {
JFrame frame = new JFrame("PopupMenuDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PopupFactory.setSharedInstance(new MyPopupFactory());
PopupMenuDemo demo = new PopupMenuDemo();
frame.setContentPane(demo.createContentPane());
demo.createPopupMenu();
frame.setSize(450, 260);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PopupMenuDemo().createAndShowGUI();
}
});
}
class PopupListener extends MouseAdapter {
JPopupMenu popup;
PopupListener(JPopupMenu popupMenu) {
popup = popupMenu;
}
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
class MyPopupFactory extends PopupFactory {
public Popup getPopup(Component owner, Component contents, int x, int y) throws IllegalArgumentException {
return new MyPopup(owner, contents, x, y);
}
}
class MyPopup extends Popup {
private JWindow popupWindow;
MyPopup(Component owner, Component contents, int ownerX, int ownerY) {
popupWindow = new JWindow();
popupWindow.setLocation(ownerX, ownerY);
popupWindow.getContentPane().add(contents, BorderLayout.CENTER);
contents.invalidate();
}
public void show() {
popupWindow.setVisible(true);
popupWindow.pack();
}
public void hide() {
popupWindow.setVisible(false);
popupWindow.removeAll();
popupWindow.dispose();
}
}
}