Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8032363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:21:49+00:00 2026-06-05T01:21:49+00:00

I am trying to implement a JTree to represent a database! The Root is

  • 0

I am trying to implement a JTree to represent a database!
The Root is the database, which can have several relations. Each relation can have attributes and functional dependencies.
Each node (database, relation, attribute and fd) has a different rightclick menu.
The first step was to implement the popdown menu in the standard way (first leftclick on a node, then rightclick to show popupmenu).

Now I want to change it to the standard behaviour of file-explorers. A rightclick selects the node and shows the correct popupmenu.

Currently I am able to rightclick and show a popupmenu, but the menu is incorrect. It is the menu of the previous selected node.

Here is an examplepicture of the tree:

enter image description here

This here is my class:

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);
    }
  }

}

And in my tree I initialize it the following way:

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

Any suggestions why this is not working?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:21:50+00:00Added an answer on June 5, 2026 at 1:21 am

    You should try keeping things simple, the following is all you really need:

     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
    
        ...
     };
    

    This is the bare minimum you need to achieve the functionality you’re asking for, you can obviously add more custom functionality as needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to implement an Observer/Observable pattern on an EC2 instance. I have been
I have been trying to implement a CheckBox Node Tree, where the parent nodes
Trying to implement a function that will return a list of ints the represent
Trying to implement NSCopying for the first time, and I have a question about
I have this weird kind of error. I am trying implement basic Euclidean algorithm
Trying to implement what I thought was a simple concept. I have a user
Trying to implement a shell, mainly piping. I've written this test case which I
Im trying to implement some code i found on a website which duplicates a
Im trying to implement pagination using multiple searching criteria. Supposed I Have student table.
Trying to implement the following behavior on an iPad. I have a map-centric application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.