20

I want to replace a JButton by a JLabel and I want my code to perform some action when the JLabel is clicked.

When I had the JButton I used action listener to handle clicks on the button:

myButton.addActionListener(new clicksListener(arg1,this))

When I replaced myButton by myLabel I got the following error message in the Eclipse:

The method addActionListener(ChipsListener) is undefined for the type JLabel

But I do know that it should be possible to attach a click handler to the JLabel. Does anybody know how it can be done?

jzd
  • 23,473
  • 9
  • 54
  • 76
Roman
  • 124,451
  • 167
  • 349
  • 456

4 Answers4

34

Add a MouseListener to the JLabel.

Because JLabel is a Component, you can add MouseListeners to it. Use that interface and write the mouseClicked event on your MouseListener to handle the click.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • I don't get your logic here. Do you mean all `Component` can add `MouseListener` (any references)? And what kind of widget can add `ActionListener`? – Tony Dec 31 '14 at 10:55
  • 2
    Yes, the `addMouseListener()` method is defined on `Component`. This class that generates `MouseEvent` objects when a mouse enters or exits the component, or when a button is pressed within the component or released after being pressed within the component. Any class which extends `Component` can have any number of `MouseListener` references listening for these events. `addActionListener()` is defined on `AbstractButton` and generates `ActionEvent` objects when the button is pressed. Since `JLabel` doesn't extend `AbstractButton`, it does not generate `ActionEvent` objects. Try `JButton`. – Erick Robertson Dec 31 '14 at 20:23
12
/*add a mouselistener instead and listen to mouse clicks*/
    jlable.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    System.out.println("Yay you clicked me");
                }

            });
kupaff
  • 329
  • 4
  • 5
  • 1
    Always try to provide some explanation beyond just posting code. Meanwhile it could be obviously trivial for your, it might difficult for other people to understand. – Henrique Barcelos Jan 05 '16 at 16:15
12

An easier approach is to just use a JButton since it already supports this functionality by using an ActionListener.

You can make the JButton look like a JLabel by using:

button.setBorderPainted( false );

This approach is for when you want to handle a mouseClick, since an ActionEvent is guaranteed to be generated, whereas as mouseClicked event when using a MouseListener may not be generated in all situations, which can confuse the user.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I am afraid it's more complicated, see http://stackoverflow.com/questions/3025320/draw-a-jbutton-to-look-like-a-jlabel-or-at-least-without-the-button-edge – xmedeko Oct 11 '12 at 14:18
  • What are the situations in which the mouseClicked event won't be generated? – Buffalo Apr 03 '13 at 07:37
  • 1
    @Buffalo, A mouseClicked event is generated when a mousePressed and mouseReleased event are generated at the same point. So if the user move the mouse by a pixel between these two events you don't get the mouseClicked. – camickr Apr 03 '13 at 15:28
  • 1
    Thanks for the reply! isn't this the default implementation for a plethora of applications? besides, you could do your magic in the MouseListener's mousePressed implementation if you really wanted to overcome this. – Buffalo Apr 04 '13 at 12:36
3

Sure. JLabels have a method which allow you to attach a MouseListener object. The MouseListener object requires a MouseAdapter() object. MouseAdapter is an abstract class which ideally serves as an Adapter for for creating on the fly listeners to do things.

JLabel lb = new JLabel(An image icon, if you'd like.);

//now lets attach a listener to your JLabel
lb.addMouseListener(new MouseAdapter() 
{
    @Override
    public void mouseClicked(MouseEvent e) 
    {
        //THIS CODE WILL RUN WHEN THE EVENT OF CLICKING IS RECEIVED.
        //example of something you could do below.
        new Random().ints().limit(60).forEach(System.out::println);
    }
});
Eric Lang
  • 274
  • 1
  • 2
  • 16