java - My Swing GUI does not update. Why? -
so, making gui in user has ability add or remove panels. simulate that, made timertask
takes name of target jpanel
, parent contains jpanel
.
counts down 0 , after removes jpanel
parent , calls repaint()
on parent , grandparent.
although old panel gets removed, other components contained in parent not come take place. can seen in picture scroll bar still indicated 2 components.
i have manually drag scroll bar down see change , when component below comes take place.
why happening?
here how components nested:
jscrollpane (grandparent) | |-> jpanel (parent) | |-> jpanel (target)
sscce not possible because jpanels
populated data xml file , uses jaxb .. not possible :( here snippet timertask
import java.util.timertask; import javax.swing.*; public class removertask extends timertask{ studentpanel s; jpanel h; jscrollpane p; public removertask(studentpanel s,jpanel holder,jscrollpane pane){ this.s = s; h = holder; p = pane; } @override public void run(){ long x = integer.max_value/2; jframe frame = new jframe("counter"); jtextarea area = new jtextarea(); jscrollpane pane = new jscrollpane(area); pane.setpreferredsize(new java.awt.dimension(350, 350)); frame.add(pane); frame.pack(); frame.setvisible(true); while(x > 100){ x = x - 1500; area.append("value of x: " + x + "\n"); } if(!frame.isvisible()){ frame.dispose(); } h.remove(s); h.repaint(); pane.repaint(); } }
you can still make sscce, don't worry xml data etc. simplify problem, man. also, don't use java.util.timer swing app rather javax.swing.timer, swing timer. else making swing changes off of swing edt dangerous.
a problem: never call revalidate()
on container. call revalidate()
before calling repaint()
on container when making changes it. revalidate tells layout managers layout components necessary change gui.
Comments
Post a Comment