LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   (java) mouselistener in JPanel (https://www.linuxquestions.org/questions/programming-9/java-mouselistener-in-jpanel-138962/)

dave bean 01-26-2004 04:56 PM

(java) mouselistener in JPanel
 
Hi
I have a JPanel which is full of JLabels. The JPanel has a mouseListener and I am using the mouse event object to determine the location of where i have clicked.

If i click on a JLabel in the JPanel can i have returned the name of the JLabel ?? At present i am returned the source as the panel name and the coordinates of the click. I know i could implement a listener on every JLabel but if i dont have to do this i can save a lot of code.

help much appreciated

coolman0stress 01-26-2004 05:44 PM

I don't believe so (from a quick glance). If you use MouseEvent's getSource() you'll always get the panel. The alternative to having a listener for each JLabel is to create one listener and then pass it to all the labels. Within this one listener you could figure out which label was actually pressed using the said getSource() method (and then possibly use a case switch to figure out what to do depending on what label was pressed).

Example:
Code:

MouseListener listener = new MouseAdapter() {
  public void mouseClicked(MouseEvent event) {
    JLabel label = (JLabel)event.getSource(); // figure out the source
    message.setText(label.getText()); // display the text in the clicked label
    // message is a JLabel
  }
};


Looking_Lost 01-26-2004 06:50 PM

It can be done if that's really the way you want to do it eg. one way like this

Code:


import javax.swing.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.*;

public class LabelMadness extends JFrame

{
  private  JPanel contentPane;
  private  JLabel lab1,lab2,lab3;
  private  Component selectedComponent;

  public LabelMadness()
  {
    super("Label Madness");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(400,400);
    contentPane=(JPanel)getContentPane();
   
    contentPane.setLayout(new FlowLayout());

    contentPane.addMouseListener(new MouseAdapter()
                      {
                      public void mouseClicked(MouseEvent me)
                        {
                            selectedComponent=contentPane.getComponentAt(me.getX(),me.getY());

                            if(selectedComponent !=contentPane)
                                  System.out.println("You clicked the component named: " + selectedComponent.getName());
                          }
                        }
                      );


    lab1=new JLabel("Label 1"); lab1.setName("lab1");
   
    lab2=new JLabel("Label 2"); lab2.setName("lab2");

    lab3=new JLabel("Label 3"); lab3.setName("lab3");

    contentPane.add(lab1); contentPane.add(lab2); contentPane.add(lab3);

    show();
  }



 public static void main(String args[])
 {
  LabelMadness fm=new LabelMadness();
  }
}


coolman0stress 01-26-2004 07:26 PM

Awesome, you learn something new everyday.

dave bean 01-27-2004 07:24 AM

excellent, thanks very much to you both !!

but i have a few questions if you have the time . .

you (lookingLost) seem to suggest that this isn't an ideal solution, why might it better to do things another way ? . . but maybe you just felt like calling the class labelMadness !

also could you please explain what is happening here, ive never seen a construction like this.

Code:

this.addMouseListener(new MouseAdapter()
  {
  public void mouseClicked(MouseEvent m){
  selectedComponent=getComponentAt(m.getX(),m.getY());

        System.out.println(selectedComponent.getName());
 
          }
    }
);

thanks again :D

coolman0stress 01-27-2004 08:07 AM

It's an annonymous class, a way for you to quickly extend a class without ever naming it. It's more or less a shortcut, but you can do it the old fashion way as well:

Code:

class MyMouseListener extends MouseAdaptor {
  public void mouseClicked(MouseEvent event) {
    // add your code here
  }
  // any other method you want to modify
}
// then somewhere else in your code
this.addMouseListener(new MyMouseListener());

With annonymous classes you can do all that in one step, very handy if you only want to use something once and don't care about the name.

Hope this helps.

dave bean 01-27-2004 12:44 PM

yes thats a good explanation
thanks :)


All times are GMT -5. The time now is 05:25 AM.