Solved

Java programming

Posted on 2014-01-06
3
177 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 66
strDist challenge 35 84
pairs challenge 5 44
java  and programming certification ? 4 63
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

743 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

13 Experts available now in Live!

Get 1:1 Help Now