Solved

Right click in JTree or JTable

Posted on 2004-10-14
9
1,895 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

751 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