JButton or JLabel?

Discussion in 'Java' started by pek, Jun 18, 2008.

  1. pek

    pek Guest

    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?
     
    pek, Jun 18, 2008
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bao

    Help JButton

    Bao, Jul 18, 2003, in forum: Java
    Replies:
    2
    Views:
    458
    Kevin Pors
    Jul 19, 2003
  2. Sandip Chitale
    Replies:
    0
    Views:
    711
    Sandip Chitale
    Aug 7, 2003
  3. Brian J. Sayatovic
    Replies:
    1
    Views:
    647
    Karsten Lentzsch
    Aug 13, 2003
  4. Replies:
    5
    Views:
    3,047
    Andrew Thompson
    Dec 26, 2005
  5. Duane Evenson
    Replies:
    5
    Views:
    1,491
    RedGrittyBrick
    Sep 27, 2007
Loading...

Share This Page