Dear All,
I would like to change the content pane of frame after pressing
a button by the method: frame.setContentFrame(newContentFrame);
However, after press the button, the Frame is frozen~~
How can I make it work? THANKS A LOT~~

import javax.swing.*;
import java.awt.event.*;
public class Test{
        JFrame main;
        JPanel panel1;
        JPanel panel2;

        public Test(){
            main = new JFrame();
            panel1 = new JPanel();
            panel2 = new JPanel();
            JLabel label1 = new JLabel("hihihihiihhihi");
            panel1.add(label1);

            JLabel label2 = new JLabel("eeeeee");
            panel2.add(label2);

            JButton butt1 = new JButton("Change");
            butt1.addMouseListener(new MouseAdapter() {
                    public void mouseClicked(MouseEvent evt) {
                        main.setContentPane(panel2);
                    }
                });
                panel1.add(butt1);

           main.setContentPane(panel1);
           main.setBounds(100,100,100,100);
           main.setVisible(true);
        }
}

i JUST CALLED: new Test(); in main method~

Dani AI

Generated

The symptom in the sample posted by is a UI-refresh/threading problem rather than a mysterious JVM hang. Replacing the content pane with setContentPane(...) alone does not force Swing to re-layout and repaint, and Swing code must be created/updated on the Event Dispatch Thread (EDT). was correct to flag validation, and 's pointer to the JFrame docs is a helpful reference for finer details.

Practical checklist to fix the freeze:

  • Construct and update the GUI on the EDT (use SwingUtilities.invokeLater(...)).
  • Use an ActionListener on JButton instead of a MouseListener for button clicks.
  • After swapping panels call layout/paint helpers: frame.revalidate(); frame.repaint(); (or frame.validate() and frame.repaint() in older code). Call frame.pack() if the frame should resize to preferred sizes.
  • For many alternative views consider CardLayout to switch panels instead of replacing the content pane.
  • If the UI still locks, look for long-running work on the EDT (move it to a SwingWorker or background thread).

Example pattern (minimal, different from the original snippet):

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        final JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel panel1 = new JPanel();
        final JPanel panel2 = new JPanel();

        JButton switchBtn = new JButton("Switch");
        switchBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                frame.setContentPane(panel2);
                frame.revalidate();
                frame.repaint();
                // frame.pack(); // optional if sizes should adjust
            }
        });
        panel1.add(switchBtn);

        frame.setContentPane(panel1);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
});

Troubleshooting notes: if swapping panels has no visible effect, confirm components have reasonable preferred sizes (pack helps) and ensure no blocking code runs on the EDT. The combination of EDT construction + revalidate()/repaint() (or using CardLayout) reliably handles view swapping.

Recommended Answers

All 2 Replies

Call validate() on the JFrame after you reset the panel. You also might want to try creating the GUI like the java tutorials recommend i.e. what they do here. (Although I don't think it is causing your problem, can't hurt to do it correctly)

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.