danishanish
danishanish

Reputation: 345

Why does setVisible() remove my components?

So in the following code, putting frame.add(OpeningSplash) before SetVisible(true) works properly (that is, displays the image) but putting it after doesn't. How would I fix this?

public static void main (String Args[]){
    JFrame frame = new JFrame("Swords & Sworcery");               
    frame.setSize(1920,1080);
    frame.setUndecorated(true);
    frame.setVisible(true);
    frame.add(new OpeningSplash());

    final JFXPanel fxPanel = new JFXPanel(); 
    frame.add(fxPanel); 
    initFX(fxPanel); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    MUSIK.playIntro();

}

Upvotes: 0

Views: 167

Answers (1)

TejjD
TejjD

Reputation: 2561

It is quite simple to understand. Think of all of your components as peripherals to your computer. You are essentially putting your computer ON before attaching all of the peripherals.

setVisible() completes the addition of all entities to your JFrame so that when you display, everything is intact.

Refer to "Why to use setVisible() below our code!"

So you should invoke your adding of components before setting the frame visible.

Hope this clears up things

Upvotes: 2

Related Questions