JFrame removes JPanels and adds new JPanel

I have a SwingWorker that sends an HTTP request, and I override the SwingWorker done () method to change the content in the JFrame. I want to basically delete everything and add a new panel of participants in the JFrame depending on the values ​​returned from the server.

Now the problem that I am facing is that when I call the following methods below on the JFrame, it does not remove anything from the JFrame and does not change the contents contained in 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 below, but I would rather modify the contents of the JFrame and then load a new one.

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

I looked at the previous answers here and on Google, and some of them use validate () or invalidate (), showing redraw () to redraw the JFrame.

/ .

: , , - .

+5
4

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();
            }
        });
    }
}
+8

repaint()/validate() ContentPane. JFrame? JFrame#pack().

+1

modification of your code

f.setContentPane(new MainDisplay(f)); 
f.getContentPane().invalidate();
f.getContentPane().validate();
f.getContentPane().repaint();
+1
source

You can try using Frame.pack()work again for me. Or try one of the following methods:

Frame.setOpaque(false);
Frame.setEnabled(false);
Frame.setVisible(false);
Frame.removeAll();
0
source

All Articles