Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JLabel mouse click on icon or text

When it is clicked on JLabel, I want to understand if the click was on "Icon part", or "Text part" of the JLabel, so that different action can be taken. Is there a clever way to do that? Or just I have to solve it relatively with the coordinates of the icon and text?

like image 589
Mert Mertce Avatar asked Dec 08 '12 12:12

Mert Mertce


1 Answers

+1 to @aymeric comment.

What about having two different JLabels

However I do understand why you might be hesitating

negative: requires maintenance of 2 labels.

My clever (:P) solution to this is create your own abstract component - which accepts icon and text as parameters for constructor - by extending JPanel and than adding 2 JLabels to the JPanel, each label has its on MouseAdapter which calls abstract method xxxClicked() (thus any implementing class must override these methods).

Here is an example I made:

enter image description here

import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                ImageIcon ii = null;
                try {
                    //I dont remmend getScaledInstance just used it for speed of code writing
                    ii = new ImageIcon(ImageIO.read(new URL("http://www.candonetworking.com/java.gif")).getScaledInstance(32, 32, Image.SCALE_SMOOTH));
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                MyLabel ml = new MyLabel(ii, "Something") {
                    @Override
                    void iconClicked() {
                        System.out.println("Icon clicked");
                    }

                    @Override
                    void textClicked() {
                        System.out.println("Text clicked");
                    }
                };

                frame.add(ml);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

abstract class MyLabel extends JPanel {

    JLabel iconLabel;
    JLabel textLabel;
    MouseAdapter iconMA;
    MouseAdapter textMA;

    public MyLabel(ImageIcon icon, String text) {
        iconLabel = new JLabel(icon);
        textLabel = new JLabel(text);
        iconMA = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                super.mouseClicked(me);
                iconClicked();
            }
        };
        textMA = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                super.mouseClicked(me);
                textClicked();
            }
        };
        iconLabel.addMouseListener(iconMA);
        textLabel.addMouseListener(textMA);
        add(iconLabel);
        add(textLabel);
    }

    abstract void iconClicked();

    abstract void textClicked();

    public JLabel getIconLabel() {
        return iconLabel;
    }

    public JLabel getTextLabel() {
        return textLabel;
    }
}
like image 55
David Kroukamp Avatar answered Nov 01 '22 01:11

David Kroukamp