javaswingrepaintevent-dispatch-threadthread-sleep

Making Swing components appear without need of resizing my application


I have problem with loading a component in my JFrame. The component doesn't appear until i reasize my window. I searched about that problem and find a solution with:

Thread repainter = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) { // I recommend setting a condition for your panel being open/visible
                    frame.repaint();
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException ignored) {
                    }
                }
            }
        });

        repainter.setName("Panel repaint");
        repainter.setPriority(Thread.MIN_PRIORITY);
        repainter.start();

The problem is that this work with loading another components after the first one, but when i load my aplication for the first time I still need to resize it.

I'll be glad to hear any better solution for solving this problem.

Thanks in advance.


Solution

  • it seems that you will need to call the frame's validate() method too.

    e.g.

     frame.repaint();
     frame.validate();
    

    source: more information here