Solved

Java programming

Posted on 2014-01-06
3
178 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 86
thymeleaf natural templating vs JSP 2 68
diffSum example 4 26
Groovy problem when using SOAPUI : DispatchException occurred 7 29
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 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

21 Experts available now in Live!

Get 1:1 Help Now