Here is a very simple segment of code. What I want is when I click on the button "First Button" then the window become empty. What actually happens is the window just becomes unresponsive. What could be the problem and what could be a solution??

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author Muhammad Anas
 */
public class ClearingJframe
{
    private static JFrame mainWindow;
    public static void main(String[] args)
    {
        mainWindow = new JFrame( "Clearing a JFrame" );
        mainWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        JButton button1 = new JButton( "First Button" );
        mainWindow.add( button1 );
        button1.addActionListener(
            new ActionListener()
            {
                public void actionPerformed( ActionEvent eve )
                {
                    mainWindow.removeAll();
                }
            }
        );

        mainWindow.pack();
        mainWindow.setLocation( 500, 500 );
        mainWindow.setVisible( true );
    }
}

Currently I want not to be concerned with issues like Event Dispatch Thread etc. for atleast today because I have not studied Multi Threading yet (Actually all discusions about this issue that I found via google are mixed up with the discusion of these topics). So, if a quick and dirty solution is possible then for now, I would preffer that.
Thanks!!

Recommended Answers

All 10 Replies

You should read the JFrame API doc about the method you are using. It is not one of the methods that has been overridden As a conveniance.
Then Call repaint() to have the GUI redrawn.

The API doc for removeAll says:

This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to reflect the changes.

hmmm .. I have just tried these three combinations. none of them worked.
1)

public void actionPerformed( ActionEvent eve )
{
    mainWindow.removeAll();
    mainWindow.repaint();
}

2)

public void actionPerformed( ActionEvent eve )
{
    mainWindow.removeAll();
    mainWindow.validate();
}

3)

public void actionPerformed( ActionEvent eve )
{
    mainWindow.removeAll();
    mainWindow.revalidate();
}

anyother suggestions? or did I misunderstood something??

did I misunderstood something

You should read the JFrame API doc about the method you are using. It is not one of the methods that has been overridden As a conveniance.

Sorry Norm, I don't understand your post either - maybe I'm having a "senior moment"?
Anyway, the problem here is that some of us forgot what the contents of a JFrame are - they are glass pane, content pane etc. When you add a component to a JFrame the method just forwards the request to the content pane, which is where the component is added. So to remove the components you have to remove them from the content pane, ie you need

 mainWindow.getContentPane().removeAll();

... followed by a quick

mainWindow.repaint();

(despite the API doc, the validate doesn't seem to be necessary)

well do you want to say that it is not the appropriate method for this task? If yes then what could be another way to accmplish what I am trying to do i.e. remove all components from a JFrame??

By the way, Java SE 7 API doc of removeAll() method of java.awt.Container class says:

Removes all the components from this container. This method also notifies the layout manager to remove the components from this container's layout via the removeLayoutComponent method.

This method changes layout-related information, and therefore, invalidates the component hierarchy. If the container has already been displayed, the hierarchy must be validated thereafter in order to reflect the changes.

what I can understand from it is that I should call the validate() or revalidate() method after calling it :(
Edit:
@Norm

Sorry, I should have specified which part of the doc
For anyone not able to find and/or to read the API doc for the JFrame class:

As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:

frame.add(child);

Thank you very much James. It worked!! :)

Ah - OK, thank you Norm.I understand now.
Muhammad - if your problem is solved please mark this thread as "solved" for future reference.

yeah sure ... Actually it had been a while since I visited daniweb last time so I missed the old "Mark as Solved" button. Now I have found its alternative :)

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.