我需要有关JTree和JPopupMenu的nimbus行为的帮助。我正在为JTree设置一个右键菜单。如果在用另一个节点打开右键菜单后左键单击一个节点,则单击节点将被选中。但是在灵气的外观和感觉中,选择另一个节点需要第二次点击。我的代码在下面,您可以使用默认外观尝试它,并评论nimbus部分。
public class JTreeDemo {
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception ex) {
}
}
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
rootNode.add(new DefaultMutableTreeNode("Child1"));
rootNode.add(new DefaultMutableTreeNode("Child2"));
rootNode.add(new DefaultMutableTreeNode("Child3"));
DefaultTreeModel model = new DefaultTreeModel(rootNode);
JTree tree = new JTree(model);
tree.addMouseListener(new TreeMouseListener());
JFrame jf = new JFrame();
jf.getContentPane().add(new JScrollPane(tree));
jf.setSize(new Dimension(300, 300));
jf.setVisible(true);
}
}
class TreeMouseListener extends MouseAdapter {
@Override
public void mouseReleased(MouseEvent e) {
if(SwingUtilities.isRightMouseButton(e)) {
JTree tree = (JTree) e.getSource();
TreePath jClickedPath = tree.getPathForLocation(e.getX(), e.getY());
tree.setSelectionPath(jClickedPath);
JPopupMenu menu = new JPopupMenu();
menu.add(new JMenuItem("menu1"));
menu.show(tree, e.getX(), e.getY());
}
}
}
答案 0 :(得分:2)
如果您打印出按下,释放和点击的鼠标事件,您将看到使用默认的L& F
// right click
tree: pressed
tree: released
tree: clicked
// click on node
tree: pressed
tree: released
tree: clicked
而对于Nimbus L& F,你得到了
// right click
tree: pressed
tree: released
tree: clicked
// first click on node, the pressed event is not passed to the listener
tree: released
tree: clicked
// second click on node
tree: pressed
tree: released
tree: clicked
这是Nimbus弹出窗口在关闭时使用事件所需的行为。 (参见错误报告中的解释#JDK-6770445)
您可以在设置L& F。
后更改此行为UIManager.setLookAndFeel(info.getClassName());
UIManager.put("PopupMenu.consumeEventOnClose", false);
修改代码段,仅更改特定JTree
// instruct the JTree not to close the popup
tree.putClientProperty("doNotCancelPopup",
new JComboBox().getClientProperty("doNotCancelPopup"));
// create the popup menu not inside the listener
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("menu1"));
// add the listener to the JTree
MouseListener popupListener = new PopupListener(popup);
tree.addMouseListener(popupListener);
以编程方式显示和隐藏弹出窗口
static class PopupListener extends MouseAdapter {
JPopupMenu popup;
PopupListener(JPopupMenu popupMenu) {
popup = popupMenu;
}
@Override
public void mousePressed(MouseEvent e) {
togglePopup(e);
}
@Override
public void mouseReleased(MouseEvent e) {
togglePopup(e);
}
private void togglePopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
} else if (popup.isVisible()) {
popup.setVisible(false);
}
}
}
答案 1 :(得分:0)
有两种解决方案可以在不更改nimbus的情况下尝试:
1。使用Robot类模拟第二次点击。
将其添加到MouseListener。
if(SwingUtilities.isLeftMouseButton(e) && e.getSource() instanceof JTree) {
Robot bot = null;
try {
bot = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
}
2。添加TreeSelectionLisitener,与第一种方法使用相同 MouseListener使用TreeSelectionListener选择正确的节点。