Failed to add to JFrame after removeAll ()

I have a relatively small class called "LoadingWindow", this class has a constructor that calls the Initialize function to set the frame. I did not test some things to try to decide why it will not be updated. During testing, I added "this.removeAll ();" to the chapter on the initialization method. Turns out I can't add anything after that. Everything that I add, just will not show.

Here is a slightly stripped down version of the class:

public class LoadingWindow extends JFrame{ public JPanel panel; public JProgressBar bar; private JLabel label; public LoadingWindow() { this.Initialize(); } public void Initialize() { this.removeAll(); this.setSize(300, 150); panel = new JPanel(new BorderLayout()); bar = new JProgressBar(0,100); label = new JLabel("Please remain calm, we're just loading..."); panel.add(bar,BorderLayout.CENTER); panel.add(label,BorderLayout.SOUTH); this.add(panel); this.validate(); this.repaint(); this.setVisible(true); } } 

The window itself pops up correctly, with a title. However, the window itself is completely empty.

I implement this class statically so that four other objects can access it in the EditorPanel class. It is defined as:

 public static LoadingWindow loadingWindow; 

and initialized in the constructor with:

 loadingWindow = new LoadingWindow(); 

Then a double check is performed inside the functions that use it to show it if it is hidden.

 if(!EditorPanel.loadingWindow.isVisible()){EditorPanel.loadingWindow.Initialize();} 

In general, I'm a little confused why the content is shown, and I am very interested in any questions, and I am ready to provide any necessary information. Google did not provide much, and each answer that I found, I have already implemented "for example, redrawing and checking."

I look forward to your reply!

~ Travis

+4
source share
2 answers

removeAll() , as @TomHawtintackline (+1 to it) said, is not forwarded to JFrames contentPane; e.g. add() , remove() or setLayout() .

That way, when you do JFrame#removeAll() , it removes the root panel of the JFrame . See How to use root panels for interesting reading and may prove fruitful for future endeavors.

You should:

getContentPane (). removeAll (); // will make sure that we remove all components from the content area

  • Also, without the need to add a JFrame (unless you add functionality beyond its current capabilities), just instantiate the JFrame and use

  • Don't call setSize rather use the appropriate LayoutManager and / or override getPreferredSize() from the JPanel and return the sizes that match the drawings, so you can call pack() on the JFrame before setting it visible.

  • I do not see the need for validate() and repaint() , they should be called only when the component is added to the visible container. Even the sooner revalidate() , which will work to add and remove a component (it calls validate() calls)

+12
source

I assume removeAll removes the root panel.

JFrame is a container and contains a number of components that are part of what we consider a frame. In JFrame (and JApplet ) it was hacked, so some methods act on the content panel instead of the frame itself. Use another method or one of the special methods when the forwarding does not work and it will be corrupted.

See the API docs for JFrame.remove and note that removeAll not overridden.

The safest solution is to ignore the “useful” forwarding and manipulate the content panel itself (either through getContentPane , or maybe it's better to create your own JPanel and use setContentPane ). (You should also not extend classes such as JFrame or Thread without the need to follow standard coding rules.)

+3
source

All Articles