20

I've got a JButton that for various reasons I want to act like a button, but look like a JLabel. It doesn't actually have to be a JLabel under the hood, I just don't want the raised button edge to show up.

Is there an easy way to turn off the "button look" for JButtons but keep all the button functionality?

I could build some kind of composed subclass hyperbutton that delegated to a jlabel for display purposes, but I'm really hoping there's something along the lines of button.lookLikeAButton(false).

Electrons_Ahoy
  • 36,743
  • 36
  • 104
  • 127
  • 2
    Couldn't you use a `JLabel` directly and handle `onClickEvents`? – Nicolas C Jun 11 '10 at 18:55
  • Well, for starters, I literally need something I can pass as a JButton. Also, I don't just need onClickEvents, I actually need the whole button "thing" - actions, arming, etc. – Electrons_Ahoy Jun 28 '10 at 17:03

5 Answers5

33

You will want to do the following:

        setFocusPainted(false);
        setMargin(new Insets(0, 0, 0, 0));
        setContentAreaFilled(false);
        setBorderPainted(false);
        setOpaque(false);

You may want to exclude setFocusPainted(false) if you want it to actually paint the focus (e.g. dotted line border on Windows look and feel).

I have used the above code in cases where I have wanted an "icon only" button.

Avrom
  • 4,967
  • 2
  • 28
  • 35
  • 1
    Perfect! This did exactly what I was looking for. Thanks! – Electrons_Ahoy Jun 28 '10 at 17:01
  • 1
    I added these, and then it looked just like the JLabel and the cursor would change to a hand so the user knows it's clickable: `setBorder(new EmptyBorder(0,0,0,0)); setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));` – hepcat72 Mar 04 '16 at 21:00
  • As an alternative, you can also have the icon change (e.g. the shading) or the background color change to also let the user know it is clickable. – Avrom Sep 08 '16 at 13:42
2

Set the background color to transparent, and the border to an EmptyBorder instance.

E.g.

   JButton button = new JButton();
   button.setBackground(null);
   button.setOpaque(false);
   button.setBorder(new EmptyBorder());

The text will still move up and down as you click the button, and the button can still be "armed" by clicking, holding, and "disarmed" by moving the mouse out of the button area.

If you don't want this behaviour, then you probably don't want to use a button, and use a real label instead.

mdma
  • 56,943
  • 12
  • 94
  • 128
1
button.setBorderPainted( false );
button.setContentAreaFilled( false ); // ?
camickr
  • 321,443
  • 19
  • 166
  • 288
0
 setContentAreaFilled(false);
 setBorderPainted(false);
 setOpaque(false);

This three lines do the trick.

Sagar G.
  • 504
  • 7
  • 15
-1

Might it actually be easier just to add a mouse listener to a JLabel? You could adjust the colors on mousePressed and mouseReleased, and do your action processing on mouseClicked?

Curtis
  • 3,931
  • 1
  • 19
  • 26
  • 3
    There is so much more to a button than a mouse listener. What about linking it up to an action? What about armed/pressed/released? What about debouncing? Making a label act like a button is much more complicated than making a button look like a label, so go with the pixel amputation, it will be less painful. – shemnon Jun 11 '10 at 21:24