Solved

Right click in JTree or JTable

Posted on 2004-10-14
9
1,888 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

746 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

12 Experts available now in Live!

Get 1:1 Help Now