Solved

Java programming

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 84
custom annotations 9 38
oracle 11g 23 80
Windows 10 IE Certificate Issue 10 45
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

776 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