Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As suggested in the article <a href="http://download.oracle.com/javase/tutorial/uiswing/concurrency/index.html" rel="nofollow noreferrer"><em>Concurrency in Swing</em></a>, your button handler's query may be blocking the event dispatch thread. Using <a href="http://download.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html" rel="nofollow noreferrer"><code>javax.swing.SwingWorker</code></a> is one approach to loading images in the background, while displaying progress and keeping the GUI thread alive.</p> <p>Addendum: Here's a <a href="http://sscce.org/" rel="nofollow noreferrer">sscce</a> that loads the SO logo:</p> <pre><code>import java.awt.*; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.*; /** @see http://stackoverflow.com/questions/4530659 */ public class WorkerTest extends javax.swing.JFrame { private JPanel panel = new JPanel(); private JLabel label = new JLabel("Loading..."); public WorkerTest() { this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); label.setHorizontalTextPosition(JLabel.CENTER); label.setVerticalTextPosition(JLabel.BOTTOM); this.add(label); this.pack(); } private void start() { new ImageWorker().execute(); } public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { @Override public void run() { WorkerTest wt = new WorkerTest(); wt.setVisible(true); wt.start(); } }); } class ImageWorker extends SwingWorker&lt;Icon, Void&gt; { private static final String TEST = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png"; private ImageIcon icon; @Override protected Icon doInBackground() throws IOException { Image image = ImageIO.read(new URL(TEST)); icon = new ImageIcon(image); return icon; } @Override protected void done() { label.setIcon(icon); label.setText("Done"); panel.setPreferredSize(new Dimension( icon.getIconWidth(), icon.getIconHeight())); WorkerTest.this.pack(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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