Reputation: 3699
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.
Upvotes: 8
Views: 36447
Reputation: 2670
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.
Upvotes: 0
Reputation: 30099
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);
Upvotes: 23