JLabel mouse click on icon or text
Asked Answered
F

1

7

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?

Fino answered 8/12, 2012 at 12:19 Comment(4)
What about having two different JLabels and put the effort on the layout instead?Balladist
Yes it also is an alternative but also has a negative: requires maintenance of 2 labels.Fino
Press? Release? Click? Drag? What do you want to happen? Asking for clever is not constructive. The geometry will depend on the text positioning settings.Maryannemarybella
The question is not about understanding different mouse action, and nevertheless in the question it is told "click". Please read question carefully to be able to answer the core, not unrelated details.Fino
S
3

+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;
    }
}
Stone answered 8/12, 2012 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.