java - How to add components to JFrame once it's visible without having to resize it? -
i have program have jframe
jbutton
in it. when user clicks jbutton
, components
of jframe
removed, , jpanel
red background added it.
when click jbutton
, red jpanel
not become visible unless resize jframe
(i using windows 7). there way achieve want without having manually resize jframe
?
here part of code using:
public class demo implements actionlistener{ public static void main(string args[]){ ............... button.addactionlistener(this); //'button' object of jbutton class. frame.setvisible(true); //'frame' object of jframe class. ............ } public void actionperformed(actionevent ae){ frame.removeallcomponents(); frame.add(panel1); //panel1 object of jpanel class red background. /* here problem lies. panel1 not visible me unless manually resize jframe. */ } }
for removing (and then, example, add new jcomponents) jcomponents jpanel or top-level containers have call, once , on end of action:
revalidate(); repaint();
and if resize or change jcomponents:
validate(); repaint();
Comments
Post a Comment