java - Updating JLabel Dynamically causes GUI to distort - Works after resizing window -


i have gui. gui jframe has panel[gridbaglayout]. gridbag layout has 3 different components. middle component panel[gridbaglayout] has jlabel i'm talking about. contains jscrollbar taking values , updating jlabel user moves bar.

the code i'm using value , update jlabel:

public class drinkadjustmentlistener implements adjustmentlistener{      @override     public void adjustmentvaluechanged(adjustmentevent e) {         drinklabel.settext("percentage " + e.getvalue() + "%");     } } 

i understand when coding android, main thread ui thread well. swing don't believe that's case , i'm not sure how update gui. fine , it's else that's causing distortion, perhaps layout manager?

before:

before

after:

after

this example code run demonstrating trying achieve. surprisingly worked. have make longer example exploit problem.

public class gui {  private jlabel jlabel;  public void displaygui(){      jframe jframe = new jframe();     jframe.setsize(500,500);      jpanel mainpanel = new jpanel(new gridbaglayout());     mainpanel.setpreferredsize(new dimension(400,400));      jlabel = new jlabel("some percentage 0%");      gridbagconstraints c = new gridbagconstraints();     c.gridx = 0;     c.gridy = 0;      mainpanel.add(jlabel,c);      jscrollbar jscrollbar = new jscrollbar();     jscrollbar.addadjustmentlistener(new myadjustmentlistener());      c = new gridbagconstraints();     c.gridx = 0;     c.gridy = 1;      mainpanel.add(jscrollbar,c);      jframe.add(mainpanel);      jframe.pack();     jframe.setvisible(true); }  public class myadjustmentlistener implements adjustmentlistener{      @override     public void adjustmentvaluechanged(adjustmentevent e) {         jlabel.settext("some percentage " + e.getvalue() + "%");     } } 

}

edit 8/15/2017, 11:30am: found workaround. figured since when resize window seemed repaint , looked correct. put jframe.repaint() after every time settext called in adjustmentlistener. side note looked if entire gui getting redrawn inside "options jpanel" seen in picture.

in swing, listeners executed on ui thread. is, safe update ui elements directly methods adjustmentvaluechanged, actionperformed etc.

only if update initiated thread, have use swingutilities.invokelater() , similar methods.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -