Search code examples
javaswingjcomponent

Why Swing is delaying to 'fully paint' the JFrame here?


I ran into a problem with my code here that I am not able to explain. I have two Jframes, the first one has a button that when clicked it leads to the second frame. This works fine. But one problem is here: the second frame is first appearing as a skeleton, without the JComponents I added to it. It is waiting until all statements are executed that is when it paints the JComponents on the JFrame displayed skeleton.

Why is that so? How can I make it "fully display/paint" all the components before executing the next line of code?

Below is my code

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 *
 * @author JWizard
 */
public class Test {

    private JFrame frame;
    private JButton button;

    public Test() {
        initGUI();
        button.addActionListener((ActionEvent e) -> {
            JFrame frame1 = new JFrame("Second Jframe");
            frame1.setPreferredSize(new Dimension(300, 300));
            frame1.getContentPane().add(new JLabel("Swing Component"));
            frame1.pack();
            frame1.setVisible(true);
            frame1.setLocationRelativeTo(null);
            /**
             * After executing the above line,
             * 'frame1.setLocationRelativeTo(null);', I am expecting the program
             * to have fully "painted" my fame1 GUI, showing the label.
             */
            try {
                Thread.sleep(7000);
                System.out.println("statement 1");
                Thread.sleep(7000);
                System.out.println("statement 2");//after executing this statment thats when
                //the program paints the components of frame1
            } catch (InterruptedException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
        });

    }

    private void initGUI() {
        frame = new JFrame("Frame 1");
        button = new JButton("Go to Frame 2");
        frame.setLayout(new FlowLayout(FlowLayout.LEADING));
        frame.setPreferredSize(new Dimension(300, 300));
        frame.getContentPane().add(button);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

    }

    public static void main(String[] args) {
        Test text = new Test();
    }

}

Solution

  • Give this a try:

    public Test() {
            initGUI();
            button.addActionListener((ActionEvent e) -> {
                JFrame frame1 = new JFrame("Second Jframe");
                frame1.setPreferredSize(new Dimension(300, 300));
                frame1.getContentPane().add(new JLabel("Swing Component"));
                frame1.pack();
                frame1.setVisible(true);
                frame1.setLocationRelativeTo(null);
    
                new SwingWorker<Void, Void>() {
                    @Override
                    protected Void doInBackground() throws Exception {
                        try {
                            Thread.sleep(7000);
                            System.out.println("statement 1");
                            Thread.sleep(7000);
                            System.out.println("statement 2");
                        } catch (InterruptedException ex) {
                            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        return null;
                    }
                }.execute();
            });
    
    }