Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to add and remove buttons, conditionally?

843806May 29 2009 — edited May 29 2009
I'm having a bit of trouble changing the neccessary buttons in my program as it is running.

I can add, and remove them just fine in the Jpanel's constructor, but I can't seem to in any methods outside of the constructor. Is this just how buttons work? I assumed that they work like other listeners and that I can call them at any time.

For example, here's how I have a button created in my JPanel's constructor.
   someButton = new JButton("something");
            add(someButton);
            someButton.setActionCommand("something");
            someButton.addActionListener(this);
This works fine in the constructor. When the class is called, it has a shiny button. But, I have some other buttons that I need to bring up, only at certain times, within this JPanel. I tried to do it the same way. First by putting that exact code in it's own method. But it failed to do anything, during run time. I know these conditions are being met and that the method itself is being called, as that was the first thing I checked.

What am I missing about the way buttons work?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 26 2009
Added on May 29 2009
2 comments
226 views