java - Why do I have to use setvisible() on my JFrame when I change components? -


so sake of simplicity set little test code figure out problem. have jframe , added 'this' (i extended main class jcomponent save time). component fills in red background. have sleep 2 seconds , type this.

f.remove(this); thing t = new thing(); f.add(t); f.setvisible(true); 

f being jframe object , 'thing' class extending jcomponent paints blue background..

when comment out setvisible() no longer changes blue.. i've tried using t.setvisible(true) seems have make frame visible again, not component

does know why have call that... or if there way change components within single frame?

"basically have jframe , added 'this' (i extended main class jcomponent save time). component fills in red background. have sleep 2 seconds , type this."

  1. don't "sleep" program. instead use java.swing.timer perform repeated tasks on gui or animation. see more @ how use swing timers. can see bunch of timer examples here , here , here , here , here

  2. instead of trying add , remove panels using cardlayout allows switch between views. avoid lot of problems come with adding , removing components/containers. see more @ how use cardlayout. see simple example here.

  3. to answer main question, whenever remove , add components frame, need revalidate() it. setvisible() takes care of you.


side note

  • seems lot adding removing background panels) change background. why not setbackround()? can switch colors use of timer

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -