NonameSL
NonameSL

Reputation: 27

How do I click a JLabel using MouseListener?

I am trying to make this program for minecraft, and now im just getting started. I want that if you click a label, it will check what label is it and will do something.

addMouseListener(new MouseAdapter() { 
          public void mousePressed(MouseEvent me) { 

            System.out.println(me.getX()+", "+me.getY()+"."); 
            Object source = me.getSource();
            int intx =  me.getX();
            int inty = me.getY();

            if(me.getX()>=1 && me.getY()>=1 && me.getX()<=70 && me.getY()<=45){
                permissionsframe.setLocation(810,250);
                System.out.println(p1p.length);
                permissionsframe.pack();
                permissionsframe.setSize(200, 200);
                permissionsframe.setVisible(true);
                JLabel playerperms = new JLabel("Player "+p1s+" has "+p1p.length+" permissions.");
                playerperms.setBounds(1, 1, 150, 150);
                permissionsframe.add(playerperms);
                System.out.println("You chose "+player1.getText()+".");
            }
            else{
                System.out.println("You did not click any label.");
            }

        }
    });

This selection area is adapted to the name I have now - NonameSL. But if the name will be longer or shorter, the selection area will obviuosly be different...

Is there a way to get the excact label? I tried if(source.equals(player1))(Player 1 is the label) but I placed the label in 1, 1 and I have to click the excact point that I defined the label in, X=1, Y=1. How can I make a mouse listener listen to a label?

Upvotes: 1

Views: 5281

Answers (2)

Kristian
Kristian

Reputation: 202

The correct way to do this is to use the .getComponent() method instead of the .getSource() since this is MouseEvent which is something different than ActionEvent.

Implement the Listener to your class:

public class Main implements MouseListener {

public static void main(String[] args) {
    
    //setup JFrame and some label and then 

    label.addMouseListener(this);
}

@Override
    public void mousePressed(MouseEvent e) {
        if (e.getComponent().equals(label)) {

        System.out.println("clicked");
    }
}
//the other orderride methods..

Upvotes: 0

Delpes
Delpes

Reputation: 1050

There is no need to check if the mouse coordinates are inside your JLabel. You can bind a Listener to every JLabel an process the click/press event in your MyMouseListener.class

To do so:

You have to add the MouseListener to every JLabel:

MyMouseListener myMouseListener = new MyMouseListener();

label01.setName("name01");
label01.addMouseListener(myMouseListener);

label02.setName("name02");
label02.addMouseListener(myMouseListener);

To identify the JLabel you could do something like this:

class MyMouseListener extends MouseAdapter {

    @Override
    public void mouseClicked(MouseEvent e) {
         JLabel l = (JLabel) e.getSource();

         if(l.getName().equals("name01"))
             doSomething01();
         else if(l.getName().equals("name02"))
             doSomething02();
    }
}

Upvotes: 1

Related Questions