Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a JButton invisible, but clickable?

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.

like image 568
Stan Avatar asked Apr 13 '11 19:04

Stan


People also ask

Which event is performed on the button click in Java?

addActionListener () method registers the ActionListener to the Java Button and the behaviour we want in response to the action is coded inside the “actionPerformed ()“ method. The actionPerformed () method is called just after the user executes any action. Which Event is Performed on the Button Click in Java? Then Answer is “Action Event”.

How to add ActionListener to a JButton in Java?

For this, we have to call addActionListner () method using the object of the JButton class. The parameter of the addActionListener () method is the object of that class in which ActionListener interface is implemented or can say in which we have defined actionPerformed () method.

What is the background color of the JFrame when Button is clicked?

Now we can see that as soon as we click on the button, the background color of the JFrame has been changed to pink. Which Method is Used to Handle Button Click Event?

Which method is used to handle button click event?

Now we can see that as soon as we click on the button, the background color of the JFrame has been changed to pink. Which Method is Used to Handle Button Click Event? The Answer is “addActionListener ()” method.


1 Answers

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);
like image 69
Jeff B Avatar answered Oct 01 '22 20:10

Jeff B