5

我正在尝试实现一个 JTree 来表示一个数据库!Root 是数据库,它可以有几个关系。每个关系都可以具有属性和功能依赖关系。每个节点(数据库、关系、属性和 fd)都有不同的右键菜单。第一步是以标准方式实现弹出菜单(首先左键单击节点,然后右键单击以显示弹出菜单)。

现在我想将其更改为文件浏览器的标准行为。右键单击选择节点并显示正确的弹出菜单。

目前我可以右键单击并显示一个弹出菜单,但菜单不正确。它是上一个选定节点的菜单。

这是树的示例图片:

在此处输入图像描述

这是我的课:

public class ShowPopupMouseListener extends MouseAdapter {
  // Refernece: http://goo.gl/plojB
  private JTree tree;
  private JPopupMenu dbPopUpMenu;
  private JPopupMenu relPopUpMenu;
  private JPopupMenu attrPopUpMenu;
  private JPopupMenu fdPopUpMenu;
  private AttrPopupFactory attrPopupFactory;

  public ShowPopupMouseListener(JTree jTree) {
    this.tree = jTree;
    DbPopupFactory dbPopupFactory = new DbPopupFactory(tree);
    dbPopUpMenu = dbPopupFactory.getDbPopupMenu();

    RelPopupFactory relPopupFactory = new RelPopupFactory(tree);
    relPopUpMenu = relPopupFactory.getRelPopupMenu();

    attrPopupFactory = new AttrPopupFactory(tree);
    attrPopUpMenu = attrPopupFactory.getAttrPopupMenu();

    FdPopupFactory fdPopupFactory = new FdPopupFactory(tree);
    fdPopUpMenu = fdPopupFactory.getFdPopupMenu();
  }

  public void mousePressed(MouseEvent e) {
    showMenuIfPopupTrigger(e);
  }

  public void mouseClicked(MouseEvent e) {
    showMenuIfPopupTrigger(e);
  }

  public void mouseReleased(MouseEvent e) {
    showMenuIfPopupTrigger(e);
  }

  private void showMenuIfPopupTrigger(final MouseEvent e) {

    if (e.isPopupTrigger()) {
      setSelectedItemsOnPopupTrigger(e);

      if (tree.getLastSelectedPathComponent() instanceof DatabaseNode) {
        addRightClickPopUpMenu(tree, dbPopUpMenu);
      } else if (tree.getLastSelectedPathComponent() instanceof RelationNode) {
        addRightClickPopUpMenu(tree, relPopUpMenu);
      } else if (tree.getLastSelectedPathComponent() instanceof AttributeNode) {
        attrPopupFactory.updateKeyCheckboxes();
        addRightClickPopUpMenu(tree, attrPopUpMenu);
      } else if (tree.getLastSelectedPathComponent() instanceof FunctionalDependencyNode) {
        addRightClickPopUpMenu(tree, fdPopUpMenu);
      }
    }
  }

  private void addRightClickPopUpMenu(Component component,
      final JPopupMenu popUpMenu) {
    component.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        if (e.isPopupTrigger()) {
          showPopUpMenu(e);
        }
      }

      public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger()) {
          showPopUpMenu(e);
        }
      }

      private void showPopUpMenu(MouseEvent e) {
        popUpMenu.show(e.getComponent(), e.getX(), e.getY());
      }
    });
  }

  private void setSelectedItemsOnPopupTrigger(MouseEvent e) {
    TreePath p = tree.getPathForLocation(e.getX(), e.getY());
    if (!tree.getSelectionModel().isPathSelected(p)) {
      tree.getSelectionModel().setSelectionPath(p);
    }
  }

}

在我的树中,我通过以下方式对其进行初始化:

UIManager.put("PopupMenu.consumeEventOnClose", Boolean.FALSE);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
tree.addMouseListener(new ShowPopupMouseListener(tree));

有什么建议为什么这不起作用?

4

1 回答 1

9

你应该尽量保持简单,以下是你真正需要的:

 class RightMouseListener implements MouseListener {

    @Override
    public void mouseClicked(MouseEvent e) {

        if (SwingUtilities.isRightMouseButton(e)) {

            int row = tree.getClosestRowForLocation(e.getX(), e.getY());
            tree.setSelectionRow(row);
            popupMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }

    ...

    //other overrides

    ...
 };

这是实现您要求的功能所需的最低限度,您显然可以根据需要添加更多自定义功能。

于 2012-06-02T03:22:15.930 回答