Click to See Complete Forum and Search --> : Help on removing awt components


JAIKISH
April 2nd, 1999, 04:13 AM
Hi,
I have a class that extends Frame. I have two panels in it - one that contains two buttons ("add" and "remove") and the other panel is empty and has FlowLayout as it's layout manager. The panels are added to the Frame using BorderLayout.

I wrote the eventhandler for the "add" button to add a new button to the initially empty panel, each time the "add" button is clicked. I am calling validate() and pack() for the container after adding the new button. Also, I wrote the handler for the "remove" button to remove the last added button. I have got two problems.

1. When I keep adding the buttons, I can't control the size of the Frame and have the new buttons wrapped around, instead the Frame is becoming wider and wider. I tried using setSize but it doesn't work.

2. Component.remove() is not working. I am doing this from an inner class which listens to the "remove" button. I am calling validate() on the container after calling remove. But the button is not removed.

Can anyone help.

thanx in advance
JAIKISH

Zafir Anjum
April 2nd, 1999, 07:46 AM
1. If you don't want the frame to resize, don't call pack().
2. You have to call Container.remove(Component);

JAIKISH
April 2nd, 1999, 11:13 AM
Thanx for the suggestions, I want the Frame to resize only in the vertical direction when the buttons spill over to the next line. How can I do that? Right now, I am adding the components and just saying pack() so that it resizes on its own. From your suggestion, I think I will have to manually resize the Frame each time I add a new button to acheive my goal. If so how can I know whether the button has jumped to the next line ? However, I tried resizing the Frame with a hardcoded size and without calling pack, but to no avail. It refused to resize and the new button is visible only if I forcefully resize the Frame while using.

Now for the remove part of the problem, I am using Container.remove(Component), but it is not working. What can be the problem ? I added a containerListener to the panel containing the buttons and tested. componentRemoved event is not triggered at all. But I am able to remove the button by calling removeNotify, which destroys the peer component (it is not supposed to be called directly according to the documentation)

thanx
JAIKISH