Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Right click in JTree or JTable

Posted on 2004-10-14
9
Medium Priority
?
1,907 Views
Last Modified: 2009-07-29
Hi,

I have a windows explorer type split-pane. directory tree on the left and file/info table on right.
When the user right clicks it brings up a pop-up menu; but like in Eclipse (for example) i would like to select the entry they are over when they right-click before bringing up the pop-up menu.

I realise that this will probably work like this for JTable:

    public void mouseClicked(MouseEvent evt)
    {
      if (evt is right-click)
      {
        Point origin = evt.getPoint();
        int row = table.rowAtPoint(origin);
        if (row != -1)
        {
          select it!
        }
      }
    }

but how does it work for JTree.  Also - the popup-trigger is used for bringing up the popup menu - should this have the same effect I described above (example the apple mac keyboard trigger - should this also make the row the mouse is over be selected before performing the popup.
Last of all - will this right-click functionality described above happen before the pop-up menu is brought up? does it have to?

Cheers,
Cathal.
0
Comment
Question by:cathalmchale
9 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 12305701
use:

TreePath path = tree.getPathForLocation(x, y)  
0
 
LVL 37

Accepted Solution

by:
zzynx earned 100 total points
ID: 12305705
   private void theTreeMousePressed(java.awt.event.MouseEvent evt) {
        if (SwingUtilities.isRightMouseButton(evt)) {
            // When right-click on an item => select it (if not yet):
            TreePath rcPath = theTree.getPathForLocation(evt.getX(), evt.getY());
            if (rcPath==null) return; // no right-click on an item
           
            if ( (getSelection()==null) || // nothing selected yet or...
                 !getSelection().getPath().equals(rcPath) ) // selected <> the right-clicked item
            {
                theTree.setSelectionPath( rcPath );
            }

            // Do your stuff
   }
0
 
LVL 92

Expert Comment

by:objects
ID: 12305709
> will this right-click functionality described above happen before the pop-up menu is brought up? does it have to?

you are responsible for popping up the menu, so its really up to you :)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 37

Expert Comment

by:zzynx
ID: 12305712
>> // Do your stuff  == pop up the menu  (after the selection is made)
0
 
LVL 92

Expert Comment

by:objects
ID: 12305724
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12305827
//** call the following method after initiallzing ur JTree object.

  //** this method adds popup listener and popup menu items to this tree
   private void addPopupMenu()
   {
     //*** Add listener to components that can bring up popup menus.
      MouseListener popupListener = new PopupListener();
     addMouseListener(popupListener);
     thisTree.addMouseListener(popupListener);
     //tree.setMouseClickListener(popupListener);
     //this.add(JTreeScroller, new PaneConstraints("JTreeScroller", "", PaneConstraints.BOTTOM, 0.93333334f));
     //this.updateUI();

     jPopupMenuFind.addActionListener(new java.awt.event.ActionListener()
     {
       public void actionPerformed(ActionEvent e)
       {
         jPopupMenuFind_actionPerformed(e);
       }
     });
     popupAtTree.add(jPopupMenuFind);

     jPopupMenuAddToXMLFilter.addActionListener(new java.awt.event.ActionListener()
     {
       public void actionPerformed(ActionEvent e)
       {
         jPopupMenuAddToXMLFilter_actionPerformed(e);
       }
     });
     popupAtTree.add(jPopupMenuAddToXMLFilter);

     jPopupMenuAddToHTMLFilter.addActionListener(new java.awt.event.ActionListener()
     {
       public void actionPerformed(ActionEvent e)
       {
         jPopupMenuAddToHTMLFilter_actionPerformed(e);
       }
     });
     popupAtTree.add(jPopupMenuAddToHTMLFilter);
     //***************************************************************************

   } //** end of method private void addPopupMenu()

  class PopupListener extends MouseAdapter
  {
    public void mousePressed(MouseEvent e)
    {
      System.out.println("hellooooooo");
      maybeShowPopup(e);
    }

    public void mouseReleased(MouseEvent e)
    {
      System.out.println("worldddddddd");
      maybeShowPopup(e);
    }

    private void maybeShowPopup(MouseEvent e)
    {
      if(e.isPopupTrigger())
      {
        if(((Node)((DefaultMutableTreeNode)
                   thisXMLTree.getSelectionPath().getLastPathComponent()).getUserObject()).getNodeType() == Node.ELEMENT_NODE)
        {
          jPopupMenuAddToXMLFilter.setEnabled(true);
          jPopupMenuAddToHTMLFilter.setEnabled(true);
        }
        else
        {
          jPopupMenuAddToXMLFilter.setEnabled(false);
          jPopupMenuAddToHTMLFilter.setEnabled(false);
        }

        popupAtTree.show(e.getComponent(), e.getX(), e.getY());
      }
    }
  }

  void jPopupMenuFind_actionPerformed(ActionEvent e)
  {
    System.out.println("Find is clicked");
  }

//Naeem Shehzad Ghuman
0
 

Author Comment

by:cathalmchale
ID: 12305973
thanks guys
0
 
LVL 92

Expert Comment

by:objects
ID: 12305999
no worries :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12306077
Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question