Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

mouse right click to a menu..?

0 views
Skip to first unread message

KA

unread,
Nov 7, 2000, 3:00:00 AM11/7/00
to
Is there a possibility in java for making something like in windows where
you get a menu when you right click on your mouse..?


--
Kenneth

Syed Ali

unread,
Nov 7, 2000, 7:30:27 PM11/7/00
to
Nothing ready-made. Here are the steps on how to do it:

1. Register the mouse listener with the JComponent (e.g. JTree) on which
you want to listen to the mouse clicks. (MouseAdapter class is the
implementation of MouseListener interface).
2. Within your MouseListener.mouseReleased(MouseEvent evt) handler, check
if right-click is detected (using evt.isPopupTrigger()).
3. When you detect right-click (i.e. if (evt.isPopupTrigger() == true) ),
then you prepare the JPopupMenu object, add the relevant JMenuItem(s) to it,
and use the JPopupMenu.show() method to display it to the user.

Of course, above steps apply if you will be using JDK1.2 or higher.

- Syed.

"KA" <kenneth_...@vip.cybercity.dk> wrote in message
news:8u8gm7$r1g$1...@news.cybercity.dk...

Raffael Herzog

unread,
Nov 8, 2000, 3:00:00 AM11/8/00
to
On Wed, 08 Nov 2000, sa...@cox.rr.com wrote:

> Nothing ready-made. Here are the steps on how to do it:
>
> 1. Register the mouse listener with the JComponent (e.g. JTree) on
> which you want to listen to the mouse clicks. (MouseAdapter class
> is the implementation of MouseListener interface). 2. Within your
> MouseListener.mouseReleased(MouseEvent evt) handler, check if
> right-click is detected (using evt.isPopupTrigger()). 3. When you
> detect right-click (i.e. if (evt.isPopupTrigger() == true) ), then
> you prepare the JPopupMenu object, add the relevant JMenuItem(s) to
> it, and use the JPopupMenu.show() method to display it to the user.

And a Mac user? There's also a platform independent solution for this:
MouseEvent.isPopupTrigger():

addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent evt) {
if ( ! evt.isPopupTrigger() ) {
popupMenu.show(parentComponent, evt.getX(),
evt.getY());
}
}
public void mouseReleased(MouseEvent evt) {
if ( ! evt.isPopupTrigger() ) {
popupMenu.show(parentComponent, evt.getX(),
evt.getY());
}
}
});


> Of course, above steps apply if you will be using JDK1.2 or higher.

This also applies to JDK1.1 and AWT.


--
Raffael Herzog
her...@raffael.ch

May the penguin be with you!

KA

unread,
Nov 9, 2000, 3:00:00 AM11/9/00
to
> addMouseListener(new MouseAdapter() {
> public void mousePressed(MouseEvent evt) {
> if ( ! evt.isPopupTrigger() ) {
> popupMenu.show(parentComponent, evt.getX(),
> evt.getY());
> }
> }
> public void mouseReleased(MouseEvent evt) {
> if ( ! evt.isPopupTrigger() ) {
> popupMenu.show(parentComponent, evt.getX(),
> evt.getY());
> }
> }
> });

parentComponent.?? What is that.? I't won't work.??

Kenenth


KA

unread,
Nov 9, 2000, 3:00:00 AM11/9/00
to
I get this error.?

java.awt.IllegalComponentStateException: component must be showing on the
screen to determine its location at
java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:1242) at
java.awt.Component.getLocationOnScreen(Component.java:1216) at
javax.swing.JPopupMenu.show(JPopupMenu.java:731) at
frema.<init>(frema.java:20) at test.main(test.java:5) Process Exit...


What's wrong..?


0 new messages