Solved

Java programming

Posted on 2014-01-06
3
180 Views
Last Modified: 2014-01-07
I have several labels and buttons in an applications and would like to detect a right mouse button click.  I have looked through the web but can find nothing suitable.  I would like to be able to detect a right click on anyone of the buttons or labels and then go to a function/method to do my business.  I am using Netbeans but can put code in if someone can suggest some code.  Thanks.
0
Comment
Question by:sargent240
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 39761734
Just add a MouseListener that listens for right-clicks to any component you're interested in:

import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;


public class F extends JFrame {
    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();
            cp.setLayout(new FlowLayout());

            JLabel label = new JLabel("Right-click me!");
            label.addMouseListener(new RightClickListener());
            cp.add(label);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        F f = new F();
                        f.setGui();
                        f.setSize(200, 200);
                        f.setVisible(true);
                    }
                });
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private class RightClickListener extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            if ((e.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                System.out.println("Right-clicked");
            }
        }
    }
}

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39761849
Just as a helpful comment : all questions in the Java TA are about "Java Programming", so it would be in your interest, I'd say, to provide a little more detail on the issue via the question title. It makes it archivally more recoverable too.
0
 

Author Closing Comment

by:sargent240
ID: 39762761
Perfect, Thank you CEHJ!  Exactly what I was looking for.  I made several mods but the basics is what I needed.  And thanks for the added tip about posting.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

821 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