Solved

Right click in JTree or JTable

Posted on 2004-10-14
9
1,891 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This video teaches viewers about errors in exception handling.

776 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