swing - java event for when the component becomes visible -


i have jscrollpane wrapped around jpanel contains potentially hundreds of jlabels show thumbnail images (one thumbnail per jlabel). memory reasons don't want build thumbnails. want build thumbnails jlabels visible , remove thumbnails when jlabels become not visible. become visible/invisible when user scrolls jpanel. tried implement loading/unloading thumbnail using componentlistener this:

addcomponentlistener( new componentadapter() {      @override     public void componentshown( componentevent e ) {         seticon( new imageicon( getthumb() ) );     }      @override     public void componenthidden( componentevent e ) {         seticon( null );     } }); 

but doesn't work. jlabels empty. use scroll event , calculate thumbnails should loaded before know if there simpler solution.

the "visible" property not mean visible "on screen". indicates if the component displayed or not. since components visible default , listeners notified when property changes listener never notified.

to best of knowledge there no dedicated event involved telling component when enters visible region of display. note setting icon on label may alter preferred size, breaking entire layout. can worked around manually giving labels fixed preferred size (which should simple in case of thumbnails).

a lazy approach overwrite paintcomponent on labels , check if thumb needs loaded in paintcomponent:

 protected void paintcomponent(graphics g) {      if (geticon() == null) {          // create thumbnail      }      super.paintcomponent(g);  } 

this isn't best approach, code run inside swings event dispatch thread. means delay in loading thumbnail block rendering of ui.

a saner approach imo request loading of thumbnail , defer actual loading background thread. when thread completes loading, can use swingutilities.invoke (or invokelater) update label (which triggers repaint automatically if i'm not mistaken).

the effect labels scrolled in briefly show empty, update thumb available.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -