7

How do I make a JButton in java, invisible, but clickable?

button.setVisible(false); 

makes the button invisible, but unclickable, is there any method that makes it invisible, but clickable?

I tried doing:

button.setVisible(false);
button.setEnabled(true);

but that didn't work either. I want to do this because I want to have a button with an image, if I put the invisible JButton over the image, the button will respond when you click the image, or invisible button.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Stan
  • 3,659
  • 14
  • 35
  • 42
  • 1
    Help me, learning Java perhaps? I just wan't to create a invisible button, it's as simple as that. – Stan Apr 13 '11 at 19:05
  • You know that visibility is don't you? – halfdan Apr 13 '11 at 19:05
  • I want to create a small menu with images as buttons, so I make the button invisible, place it on a image which is just as big as the button and voila. – Stan Apr 13 '11 at 19:07
  • You are a programmer not a wizard... :D Unesuful thing to learn and impossible to do – Fseee Apr 13 '11 at 19:07
  • 9
    Wow everyone feeling a little harsh today? Let's all be respectful. – Jeff B Apr 13 '11 at 19:09
  • @Stan: If you want a clickable image, see [this](http://stackoverflow.com/questions/2819884/create-images-clickable-on-jpanel) – Jeremy Apr 13 '11 at 19:11
  • 12
    What if you want to build in an Easter Egg where clicking a small-yet-invisible button pops up a game where you have to herd cats into a box using a pair of electrostatically-charged cucumbers? Go easy, it's a valid question. – Town Apr 13 '11 at 19:13
  • 2
    @Town: I think there is a function for that: `new JInvisibleStaticCucumberBoxCatButton()` – Jeff B Apr 13 '11 at 19:17
  • 1
    @Jeff B - Bad wording, this is a class:P – Petar Minchev Apr 13 '11 at 19:20
  • @Jeff B: I really should get into Java... :D – Town Apr 13 '11 at 21:54
  • XD thank you all for those comments! made my whole day – PulsePanda Oct 02 '12 at 13:14

2 Answers2

23

I think you mean transparent, rather than invisible.

This will make a clickable button that is not "visible", i.e. transparent:

button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);

This answers your asked question, but if your intent is to make an image clickable, there is a better way for that, too:

ImageIcon myImage = new ImageIcon("images/myImage.jpg");
JButton button = new JButton(myImage);
Jeff B
  • 29,943
  • 7
  • 61
  • 90
0

Well, there is no point so since there is no point there is no standard way to do this, but it's possible to override the paint method of JButton and do nothing in it like:

class InvisibleButton extends JButton {

    @Override
    public void paint(Graphics g){
          // Do nothing here
    }
}

Try playing around with this.

Pang
  • 9,564
  • 146
  • 81
  • 122
Troydm
  • 2,642
  • 3
  • 24
  • 35