Solved

Right click in JTree or JTable

Posted on 2004-10-14
9
1,889 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 25 total points
ID: 12305701
use:

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

Accepted Solution

by:
zzynx earned 25 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
 
LVL 37

Expert Comment

by:zzynx
ID: 12305712
>> // Do your stuff  == pop up the menu  (after the selection is made)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 80
firstswap challenge 20 68
what is a "java.lang.System Property"   ? 20 57
Groovy problem when using SOAPUI : DispatchException occurred 7 27
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now