Note that there are some explanatory texts on larger screens.

plurals
  1. POLWUIT: Load images in background thread
    text
    copied!<p>I have a list that contains about 20 image URLs and some other things.</p> <p>I want to display the other things (description) and allow the user to interact with the app while I load the 20 images.</p> <p>What I noticed is that no matter what I tried, I can't interact with the form until the images finished loading even though I am doing the loading in another thread.</p> <p>This is my solution I am using now.</p> <pre><code>private Container createServerItems() throws Exception { Container list = new Container(new BoxLayout(BoxLayout.Y_AXIS)); final int size = mediaList.size(); final Button buttons[] = new Button[size]; System.out.println("In here: " + size); for (int i = 0; i &lt; size; i++) { Container mainContainer = new Container(new BorderLayout()); Media m = new Media(); m.fromJSONString(mediaList.elementAt(i).toString()); buttons[i] = new Button("please wait"); final int whichButton = i; Display.getInstance().callSerially(new Runnable() { public void run() { try { System.out.println(MStrings.replaceAll(m.getImgURL(), "\"", "")); final StreamConnection streamConnection = (StreamConnection) Connector.open(MStrings.replaceAll(m.getImgURL(), "\"", "")); Image image = Image.createImage(streamConnection.openInputStream()); streamConnection.close(); buttons[whichButton].setText(""); buttons[whichButton].setIcon(image.scaled(32, 32)); } catch (Exception e) { } } }); TextArea t = new TextArea(m.getDesc()); t.setEditable(false); t.setFocusable(false); t.setGrowByContent(true); mainContainer.addComponent(BorderLayout.WEST, buttons[i]); mainContainer.addComponent(BorderLayout.CENTER, t); list.addComponent(mainContainer); } return list; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload