0

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.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Altair
  • 168
  • 1
  • 9
  • call frame.validate() and frame.repaint() together. – ultrajohn Apr 17 '14 at 11:48
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) That code violates the EDT rule. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Apr 17 '14 at 12:06

3 Answers3

1

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.

Salah
  • 8,567
  • 3
  • 26
  • 43
  • Don't use `setSize()`, use `pack()`. Setting the size is a guess, packing it results in the correct (minimum) size needed to display the components. E.G. as seen in [this answer](http://stackoverflow.com/a/5630271/418556). Ironically, that code calls `pack()` on *PLAF change,* rather than adding new components, which in that GUI are shown in a scroll pane with no need to pack the UI! ;) – Andrew Thompson Apr 17 '14 at 12:09
1

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

e.g.

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

source: more information here

ultrajohn
  • 2,527
  • 4
  • 31
  • 56
  • @Crypto Ok. Then don't forget to accept this as the correct answer. :) – ultrajohn Apr 17 '14 at 11:58
  • Actually, the code should be revalidate() then repaint(). The revalidate() is used to invoke the layout manager. The repaint() is then sometimes needed to make sure the components are repainted. So really the answer is not correct. – camickr Apr 17 '14 at 15:09
1

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366