6

I currrently have a SwingWorker that sends a HTTP Request and I override the SwingWorker's done() method to change contents in a JFrame. I want to basically remove everything and add a new members panel on the JFrame depending on the values returned from the Server.

Now, the problem I am facing is that when I invoke the following methods below on the JFrame, it doesn't remove anything from the JFrame nor does it change it's contents contained within the Frame.

//TODO: Investigate why JFrame content pane won't repaint.
f.removeAll();
//Pass the frame f reference only into MainDisplay, it doesn't actually do anything apart from allowing a class to add a JMenuBar on the JFrame.
f.add(new MainDisplay(f)); 
f.getContentPane().invalidate();
f.getContentPane().validate();
f.getContentPane().repaint();

The current fix I have is this below but I would rather change the contents of the JFrame rather then loading a new one up.

f.dispose();
f=new ApplicationFrame();

I've looked through previous answers on here and on Google and some state use validate() or invalidate() whilst calling repaint() to repaint the JFrame.

Any suggestions/help would be much appreciated.

Edit: I think I am going to debug more since there must be something else going wrong.

unleashed
  • 915
  • 2
  • 16
  • 35
  • Does anyone know if using remove or removeAll removes the actual memory located for Panel/Panels on a Frame? or does it just remove it from the Frame? – unleashed Nov 23 '11 at 20:17
  • It just removes the component(s) from the frame. It's up to the JVM garbage collector to "remove" the memory... or rather deallocate the memory. The GC doesn't guarantee this will happen during the lifetime of your program. Also, if the components that were removed are referenced elsewhere in the program then they will not be GC'd (except in special circumstances using weak references). – Jason Sep 04 '15 at 19:07

4 Answers4

9

for example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    public MyFrame() {
        final JPanel parentPanel = new JPanel();
        parentPanel.setLayout(new BorderLayout(10, 10));

        final JPanel childPanel1 = new JPanel();
        childPanel1.setBackground(Color.red);
        childPanel1.setPreferredSize(new Dimension(300, 40));

        final JPanel childPanel2 = new JPanel();
        childPanel2.setBackground(Color.blue);
        childPanel2.setPreferredSize(new Dimension(800, 600));

        JButton myButton = new JButton("Add Component ");
        myButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                parentPanel.remove(childPanel1);
                parentPanel.add(childPanel2, BorderLayout.CENTER);
                parentPanel.revalidate();
                parentPanel.repaint();
                pack();
            }
        });
        setTitle("My Empty Frame");
        setLocation(10, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        parentPanel.add(childPanel1, BorderLayout.CENTER);
        parentPanel.add(myButton, BorderLayout.SOUTH);
        add(parentPanel);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyFrame myFrame = new MyFrame();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

modification of your code

f.setContentPane(new MainDisplay(f)); 
f.getContentPane().invalidate();
f.getContentPane().validate();
f.getContentPane().repaint();
isograph
  • 11
  • 1
  • 2
    Hi isograph The difference between the code you posted here and the code you modified is only the line `f.removeAll()`. If you feel this makes a significant difference to the code, please consider expanding your answer to explain why. If you just removed that line because it's unnecessary, perhaps leave a comment to the question instead. Answers should propose solutions to the question. Comments are more appropriate for suggesting enhancements to questions or other answers. – Crippledsmurf Aug 07 '12 at 02:08
1

You may try using Frame.pack() again it worked for me. Or try one od those following methods:

Frame.setOpaque(false);
Frame.setEnabled(false);
Frame.setVisible(false);
Frame.removeAll();
Alex
  • 643
  • 7
  • 17
1

You are trying to repaint()/validate() the ContentPane. Did you try doing same on the JFrame? You can also try JFrame#pack().

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19