Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JPopupMenu

Posted on 1999-07-22
6
Medium Priority
?
1,674 Views
Last Modified: 2008-02-01
Hi,
I would like to have an example of how to use JPopupMenu in a JTree (within a JScrollPane).
What I have for now is:

JMenuItem1.setText("Try popup");
JMenuItem1.setActionCommand("Try popup");
JPoppMenu1.add(JMenuItem1);
JTree1.add(treePopUpMenu);
JMenuItem1.addActionListener(lSymAction);

void JMenuItem1_actionPerformed(java.awt.event.ActionEvent event)
{
      // code
}

Now when I click with right mouse button on a tree node, I'd like to popup to appear - what else should I have?
BTW on mouseClicked(MouseEvent  event) how can I figure if was it the left mouse button or the right mouse button?

Thanks
0
Comment
Question by:s_lavie
[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
  • 4
  • 2
6 Comments
 
LVL 2

Expert Comment

by:florinsuciu
ID: 1246781
Is an example in "Thinking in Java " by Bruce Eckel:(http://www.codecuts.com/mainpage.asp?MenuID=141&WebPageID=198 )
You must call enableEvents( )
and select for mouse events instead of using an event listener. That is, it’s possible to add a
mouse listener but the MouseEvent that comes through doesn’t return true from
isPopupTrigger( ) – it doesn’t know that it should trigger a popup menu. In addition, when
I tried the listener approach it behaved strangely, possibly from recursive click handling. In
any event, the following example produces the desired popup behavior:
//: Popup.java
// Creating popup menus with Swing
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
public class Popup extends JPanel {
JPopupMenu popup = new JPopupMenu();
JTextField t = new JTextField(10);
public Popup() {
add(t);
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e){
t.setText(
((JMenuItem)e.getSource()).getText());
}
};
JMenuItem m = new JMenuItem("Hither");
m.addActionListener(al);
popup.add(m);
m = new JMenuItem("Yon");
m.addActionListener(al);
popup.add(m);
m = new JMenuItem("Afar");
m.addActionListener(al);
popup.add(m);
popup.addSeparator();
m = new JMenuItem("Stay Here");
m.addActionListener(al);
popup.add(m);
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
protected void processMouseEvent(MouseEvent e){
if (e.isPopupTrigger())
popup.show(
e.getComponent(), e.getX(), e.getY());
super.processMouseEvent(e);
}
public static void main(String args[]) {
Show.inFrame(new Popup(),200,150);
}
} ///:

0
 
LVL 2

Author Comment

by:s_lavie
ID: 1246782
florinsuciu,
It doesn't really feet my question. I asked for an example of how to use JPopupMenu in a JTree (within a JScrollPane).
I don't really understand how your example helps me. Maybe you can be more specific with my needs...
0
 
LVL 2

Author Comment

by:s_lavie
ID: 1246783
Ok, I'll let someone else try...
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 2

Expert Comment

by:florinsuciu
ID: 1246784
JTree1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
if(SwingUtilities.isRightMouseButton(evt))
{
JPoppMenu1.show(......_;
}
}
});

I think you might take a look at the Java tutorial & How to Use Trees in the
sun site..
Just go to java.sun.com & then tutorials... THat is the best place to start
with...

0
 
LVL 2

Accepted Solution

by:
florinsuciu earned 200 total points
ID: 1246785
JTree1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
if(SwingUtilities.isRightMouseButton(evt))
{
JPoppMenu1.show(......_;
}
}
});

I think you might take a look at the Java tutorial & How to Use Trees in the
sun site..
Just go to java.sun.com & then tutorials... THat is the best place to start
with...


0
 
LVL 2

Expert Comment

by:florinsuciu
ID: 1246786
A better way is to use e.isPopupTrigger(), this is supposed to return
whether or not the mouse event is the popup trigger for the current
platform. Some platforms the popup should be triggered on mouse down, some
on mouse up, and some on clicked events...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

721 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