Making a JButton invisible, but clickable?
Asked Answered
S

2

8

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.

Senatorial answered 13/4, 2011 at 19:3 Comment(11)
Help me, learning Java perhaps? I just wan't to create a invisible button, it's as simple as that.Senatorial
You know that visibility is don't you?Rifle
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.Senatorial
You are a programmer not a wizard... :D Unesuful thing to learn and impossible to doPustulate
Wow everyone feeling a little harsh today? Let's all be respectful.Homogenetic
@Stan: If you want a clickable image, see thisVilipend
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.Cistercian
@Town: I think there is a function for that: new JInvisibleStaticCucumberBoxCatButton()Homogenetic
@Jeff B - Bad wording, this is a class:PDemobilize
@Jeff B: I really should get into Java... :DCistercian
XD thank you all for those comments! made my whole dayServomechanical
H
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);
Homogenetic answered 13/4, 2011 at 19:8 Comment(3)
Yeah, it says I have to wait 1 minute.Senatorial
Oh, I have one more small question, how do I change a JButton's border color?Senatorial
@Senatorial - Take a look at setBorder method:)Demobilize
L
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.

Livingston answered 13/4, 2011 at 19:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.