Question

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.

Was it helpful?

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

OTHER TIPS

You can eather use :

pack();

it'll causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.

Or you can use

setSize()

To resizes this component so that it has width width and height height.

Call setVisible last, after you've initialised your primary UI

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top