LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-26-2004, 05:56 PM   #1
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Rep: Reputation: 15
(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
 
Old 01-26-2004, 06:44 PM   #2
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
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
  }
};

Last edited by coolman0stress; 01-26-2004 at 06:50 PM.
 
Old 01-26-2004, 07:50 PM   #3
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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();
  }
}
 
Old 01-26-2004, 08:26 PM   #4
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Awesome, you learn something new everyday.
 
Old 01-27-2004, 08:24 AM   #5
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
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
 
Old 01-27-2004, 09:07 AM   #6
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
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.
 
Old 01-27-2004, 01:44 PM   #7
dave bean
Member
 
Registered: Jun 2003
Location: UK
Distribution: Slackware 9.1
Posts: 136

Original Poster
Rep: Reputation: 15
yes thats a good explanation
thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java plugin installed correctly for Firefox but not able to view any java applet tvn Linux - Software 10 04-15-2010 03:13 AM
Java Programming: Java Runtime Environment not found when trying to compile murbz Linux - Software 2 03-26-2009 04:04 AM
Resizing a JFrame when JPanel resizes? 95se Programming 1 03-23-2005 05:48 PM
[JAVA] JPanel over JPanel ... not being displayed properly linux_ub Programming 0 08-13-2004 12:21 PM
2 Questions: java calling system commands? PERL vs Java? randomx Programming 28 11-28-2003 09:24 PM


All times are GMT -5. The time now is 10:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration