It's not a secret anymore!*
The moose likes Swing / AWT / SWT and the fly likes Hide the Button When User Clicks On It Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Hide the Button When User Clicks On It " Watch "Hide the Button When User Clicks On It " New topic
Author

Hide the Button When User Clicks On It

Susan Bach
Greenhorn

Joined: Jan 20, 2010
Posts: 4

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

Joined: Jun 21, 2008
Posts: 423
If your button is on the Swing form, this should work:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37971
    
  22
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

Joined: Jan 20, 2010
Posts: 4

Ireneusz Kordal wrote:If your button is on the Swing form, this should work:


Thanks, but it did not work.
Susan Bach
Greenhorn

Joined: Jan 20, 2010
Posts: 4

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

Joined: Jun 09, 2003
Posts: 4632
> Thanks, but it did not work.

only because the button is not listening
Susan Bach
Greenhorn

Joined: Jan 20, 2010
Posts: 4

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
 
jQuery in Action, 2nd edition
 
subject: Hide the Button When User Clicks On It
 
Similar Threads
questions regarding system tray and tray icon
Listeners
comprehension problem gwt & events
Edit all the cell in a row in GWT
Wicket Framework - not able to get return value back from confirmation dialog