Problem with JMenu, JPopupMenu [HELP]
I am having a problem with JPopupMenu.
I create a simple JTree. when the right to display a JPopupMenu of JTree.
When the JPopupMenu is show up again I want to click on a JTree node then takes two clicks of the mouse select the node. If you remove this line "UIManager.setLookAndFeel (UIManager.getSystemLookA ndFeelClassName ());" then click only once.
If you want to use "UIManager.setLookAndFeel (UIManager.getSystemLookA ndFeelClassName ());"
How they must.
Code:
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.tree.*;
public class TreePopup {
JTree tree = new JTree();
JPopupMenu popup;
public TreePopup() {
popup = new JPopupMenu();
popup.setInvoker(tree);
PopupHandler handler = new PopupHandler(tree, popup);
popup.add(getMenuItem("Add child", handler, 'I'));
popup.add(getMenuItem("Add sibling", handler, 'M'));
popup.addSeparator();
popup.add(new JMenuItem("New"));
popup.add(new JMenuItem("Open"));
popup.add(new JMenuItem("Copy"));
popup.add(new JMenuItem("Cut"));
}
private JMenuItem getMenuItem(String s, ActionListener al, char c) {
JMenuItem menuItem = new JMenuItem(s);
menuItem.setAccelerator(KeyStroke.getKeyStroke(c, CTRL_DOWN_MASK));
menuItem.setActionCommand(s.toUpperCase());
menuItem.addActionListener(al);
return menuItem;
}
private JScrollPane getTreeComponent() {
tree.add(popup);
expand(new TreePath(tree.getModel().getRoot()));
return new JScrollPane(tree);
}
@SuppressWarnings("unchecked")
private void expand(TreePath path) {
TreeNode node = (TreeNode) path.getLastPathComponent();
if (node.getChildCount() >= 0) {
java.util.Enumeration e = node.children();
while (e.hasMoreElements()) {
TreeNode n = (TreeNode) e.nextElement();
expand(path.pathByAddingChild(n));
}
}
tree.expandPath(path);
}
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
TreePopup test = new TreePopup();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(test.getTreeComponent());
f.setSize(400, 400);
f.setLocation(200, 200);
f.setVisible(true);
}
}
class PopupHandler implements ActionListener {
JTree tree;
JPopupMenu popup;
Point loc;
public PopupHandler(JTree tree, JPopupMenu popup) {
this.tree = tree;
this.popup = popup;
tree.addMouseListener(ma);
}
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
TreePath path = tree.getPathForLocation(loc.x, loc.y);
// System.out.println("path = " + path);
// System.out.printf("loc = [%d, %d]%n", loc.x, loc.y);
if (ac.equals("ADD CHILD"))
addChild(path);
if (ac.equals("ADD SIBLING"))
addSibling(path);
}
private void addChild(TreePath path) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) path
.getLastPathComponent();
int count = parent.getChildCount();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("child "
+ count);
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.insertNodeInto(child, parent, count);
}
private void addSibling(TreePath path) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path
.getLastPathComponent();
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node
.getParent();
int count = parent.getChildCount();
DefaultMutableTreeNode child = new DefaultMutableTreeNode("child "
+ count);
DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
model.insertNodeInto(child, parent, count);
}
private MouseListener ma = new MouseAdapter() {
private void checkForPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
loc = e.getPoint();
popup.show(tree, loc.x, loc.y);
}
}
public void mousePressed(MouseEvent e) {
checkForPopup(e);
}
public void mouseReleased(MouseEvent e) {
checkForPopup(e);
}
public void mouseClicked(MouseEvent e) {
checkForPopup(e);
}
};
PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
}
};
}
Re: Problem with JMenu, JPopupMenu [HELP]
You have omitted one vital detail: since the problem arises only with the SystemLookAndFeel, you need to tell us what system(s) you have tested this on (Windows/Windows classic/Linux/Mac).
Add a line after setLookAndFeel Code:
System.out.println(UIManager.getLookAndFeel());
Report here what that prints on your system.
db
Re: Problem with JMenu, JPopupMenu [HELP]
result line code Code:
System.out.println(UIManager.getLookAndFeel());
[The Microsoft Windows Look and Feel - com.sun.java.swing.plaf.windows.WindowsLookAndFeel]
Detail
Code:
public static void main(String[] args) throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
System.out.println(UIManager.getLookAndFeel());
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
System.out.println(UIManager.getLookAndFeel());
//TreePopup test = new TreePopup();
//JFrame f = new JFrame();
//f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//f.add(test.getTreeComponent());
//f.setSize(400, 400);
//f.setLocation(200, 200);
//f.setVisible(true);
}
RESUTL
[The Java(tm) Look and Feel - javax.swing.plaf.metal.MetalLookAndFeel]
[The Microsoft Windows Look and Feel - com.sun.java.swing.plaf.windows.WindowsLookAndFeel]
thank