Clearing contents of a panel?
Hi i have a panel called cPanel and i have shapes drawn into it like Ovals etc. I have given an option if they want to continue drawing
int option = JOptionPane.showConfirmDialog(null,"Try again?","Finished",
JOptionPane.YES_NO_OPTION);
if(option == JOptionPane.YES_OPTION)
{
cPanel.// something to clear what is on my panel
}
My questtion is, when they select yes, how do i clear my cPanel of its contents? Thannks
[668 byte] By [
nvidia1a] at [2007-11-26 18:55:01]

if(option == JOptionPane.YES_OPTION)
{
cPanel = new YourPanel();
}
Post Swing related questions in the swing forum, please.
http://forum.java.sun.com/forum.jspa?forumID=57
There are several ways to do this, depending on the implementation of your panel. Remember that whatever you've put in the paintComponent(Graphics g) method of your panel will be called whenever the panel is made visible, so simply drawing a big white rectangle on it will only work until you (say) minimise and maximise it again.
One approach will be for the class to hold an internal Shape Vector, and it's paintComponent method simply paints the background, then loops through this Vector and paints each one. That way, you can just make a clear() method that removes all the components of the Vector and calls the repaint() method, leaving you with a blank panel again.
Another would be to have an internal boolean flag isClear, and your paintComponent method only paints if (!isClear), and pressing a certain button sets the flag to true and repaints the component. That, however, would not (easily) let you start repainting different shapes, but would make it easy to make the previously drawn shapes disappear/come back again.
Hope this all makes sense...
> if(option == JOptionPane.YES_OPTION)
> {
>
> cPanel = new YourPanel();
> }
>
>
> Post Swing related questions in the swing forum,
> please.
> http://forum.java.sun.com/forum.jspa?forumID=57
I'm sure you'll correct me if I'm wrong, but won't the old panel still be displayed? I mean, after it's added to the container, that's the object that the container has a handle on and will display, so reasigning cPanel to a new JPanel would just mean you're trying to minipulate an object that's not being displayed.