JButton or JLabel?

P

pek

Let's say you want to create a small game using Swing components
instead of overriding the paint method.. But you want the components
to look more graphical than the usual look and feel they have (without
of course creating a Look and Feel). What would you prefer in order to
draw a transparent icon that will react on mouse over and clicking on
it? Go ahead and run HouseOfCards in http://treazy.com/apps/javacorner
to see what do I mean. In the game, every button is a swing component,
a JLabel in particular.

With a JButton you can use the ActionListener to listen when the
button is clicked and MouseListener for mouse entered/exited for the
mouse over effect. But, you have to customize the borders so it would
look less of a component.
On the other hand, using JLabel is probably easier. You only need
to add a MouseListener where you would add code to the clicked,
entered and exited to perform the same thing with the button AND avoid
changing anything as opposed to JButton's border.

Assuming that in both cases you want the component's background to
be transparent (setOpaque(false)), is using a JLabel for buttons a bad
practice? Are there any performance issues or disadvantages?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top