ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 05:50 PM.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.