How to clear the content of a JFrame?

my code is something like this:

there is a main window with buttons 1 and 2 (2 is for exit)

when I click button 1, a new window should appear with lots of components , etc then inside it is a cancel button , now when i click the cancel button I made its visibility to false (setVisible(false))

now it should be back to the main window , but when I click button 1 again,
the called frame is so messed up , its like a the components has been added again to the frame and the old still remains.

1.You can use the remove() (similar to add()) method available to remove all the component before setting the visible to false.
2.Better, you keep a flag variable to test whether button 1 is pressed 1st time or 2nd time. When 1st time it is pressed, do the usual procedure else only set it visibility to true.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.