Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java programming

Posted on 2014-01-06
3
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 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…
Suggested Courses

704 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