Search code examples
javajbuttonjlabelmouselistenerimageicon

Suggestion Required between JLabel and JButton for MouseListener


Well, my program is nearly finished. I am using JLabels with ImageIcon. But some time when I clicked outside of a JLabel/ImageIcon the mouse event is generated un-wantedly on some other label.

I heard that ImageIcon in JLabels are not good for MouseListener, because even in my program sometime events are wrongly generate when I clicked on one JLabel ImageIcon which disabled for mouselistener then an active mouselistener jlabel catch that event and activated.

Please suggest me should I drop the idea for using JLabel with ImageIcon and use JButtonp instead?

Your Suggestions and comments will guide me

Thanks


Solution

    1. Conceptually, a label is not intended to be interactive (except tooltips or mouseover effects).

    2. Next you should think of your users. What about those who prefer to use keyboard?

    3. Buttons have a standard (well implemented) behaviour for different mouse events? (Try to click on it and drag the mouse out, for instance)

    Go with JButton and ActionListener.