Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hide the Button When User Clicks On It

 
Susan Bach
Greenhorn
Posts: 4
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I code to hide a regular button when a user clicks on it? I am missing something in my go method and the suggestions thus far have not worked.


public void go() {
frame = new JFrame("Hide the Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
button = new JButton("Click to Hide");
frame.getContentPane().add(button);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
button.setVisible(false);
}
}
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your button is on the Swing form, this should work:
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

I shall move this thread to our GUIs forum.

You probably don't want to hide a button, but inactivate it, so you can't click it twice. Like this.

myButton.setEnabled(false);

The default for a button is enabled; after clicking on it you may want to reactivate it, by putting something like this in the ActionListener on another button (or similar methods)

myButton.setEnabled(true);
 
Susan Bach
Greenhorn
Posts: 4
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ireneusz Kordal wrote:If your button is on the Swing form, this should work:


Thanks, but it did not work.
 
Susan Bach
Greenhorn
Posts: 4
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to JavaRanch

I shall move this thread to our GUIs forum.

You probably don't want to hide a button, but inactivate it, so you can't click it twice. Like this.

myButton.setEnabled(false);

The default for a button is enabled; after clicking on it you may want to reactivate it, by putting something like this in the ActionListener on another button (or similar methods)

myButton.setEnabled(true);


No, I am sure I want to hide it. Thanks.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Thanks, but it did not work.

only because the button is not listening
 
Susan Bach
Greenhorn
Posts: 4
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:> Thanks, but it did not work.

only because the button is not listening


Your statement about listening gave me enough of a hint to solve my problem. I added the statement:

button.addActionListener(this);

It worked, the button is gone on clicking it!

Thanks,
Susan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic