By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

How can I load an image from a URl,display it in another class without freezing the c

100+
P: 108
I have a class test2 containing a button.When I press this button, I want to instantiate an object from Test3 and display the image in a panel on the newly created object..

test2 class

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class test2 extends javax.swing.JFrame {
  3.  
  4.     /** Creates new form test2 */
  5.     public test2() {
  6.         initComponents();
  7.     }
  8.  
  9.     /** This method is called from within the constructor to
  10.      * initialize the form.
  11.      * WARNING: Do NOT modify this code. The content of this method is
  12.      * always regenerated by the Form Editor.
  13.      */
  14.     @SuppressWarnings("unchecked")
  15.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  16.     private void initComponents() {
  17.  
  18.         jButton1 = new javax.swing.JButton();
  19.  
  20.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  21.  
  22.         jButton1.setText("jButton1");
  23.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  24.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  25.                 jButton1ActionPerformed(evt);
  26.             }
  27.         });
  28.  
  29.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  30.         getContentPane().setLayout(layout);
  31.         layout.setHorizontalGroup(
  32.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  33.             .addGroup(layout.createSequentialGroup()
  34.                 .addGap(160, 160, 160)
  35.                 .addComponent(jButton1)
  36.                 .addContainerGap(167, Short.MAX_VALUE))
  37.         );
  38.         layout.setVerticalGroup(
  39.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  40.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  41.                 .addContainerGap(228, Short.MAX_VALUE)
  42.                 .addComponent(jButton1)
  43.                 .addGap(49, 49, 49))
  44.         );
  45.  
  46.         pack();
  47.     }// </editor-fold>
  48.  
  49.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  50.             String url="http://www.flash-slideshow-maker.com/images/help_clip_image004.jpg";
  51.             System.out.append("Loadiiing ....");
  52.             Test3 t=new Test3(url);// TODO add your handling code here:
  53.     }
  54.  
  55.     /**
  56.     * @param args the command line arguments
  57.     */
  58.     public static void main(String args[]) {
  59.         java.awt.EventQueue.invokeLater(new Runnable() {
  60.             public void run() {
  61.                 new test2().setVisible(true);
  62.             }
  63.         });
  64.     }
  65.  
  66.     // Variables declaration - do not modify
  67.     private javax.swing.JButton jButton1;
  68.     // End of variables declaration
  69.  
  70. }
  71.  
  72.  

Test3 class
Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Test3 extends javax.swing.JFrame {
  3.  
  4.     /** Creates new form Test3 */
  5.     public Test3(String url) {
  6.         initComponents();
  7.         try {
  8.             System.out.append("Loadiiing 1....");
  9.             displayMap(url, jPanel1, new JLabel());
  10.             System.out.append("Loaded2.......... ....");
  11.         } catch (MalformedURLException ex) {
  12.             Logger.getLogger(Test3.class.getName()).log(Level.SEVERE, null, ex);
  13.         }
  14.     }
  15.  
  16.  
  17.     // <editor-fold defaultstate="collapsed" desc="Generated Code">
  18.     private void initComponents() {
  19.  
  20.         jPanel1 = new javax.swing.JPanel();
  21.  
  22.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  23.  
  24.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  25.         jPanel1.setLayout(jPanel1Layout);
  26.         jPanel1Layout.setHorizontalGroup(
  27.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  28.             .addGap(0, 359, Short.MAX_VALUE)
  29.         );
  30.         jPanel1Layout.setVerticalGroup(
  31.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  32.             .addGap(0, 268, Short.MAX_VALUE)
  33.         );
  34.  
  35.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  36.         getContentPane().setLayout(layout);
  37.         layout.setHorizontalGroup(
  38.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  39.             .addGroup(layout.createSequentialGroup()
  40.                 .addGap(20, 20, 20)
  41.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  42.                 .addContainerGap(21, Short.MAX_VALUE))
  43.         );
  44.         layout.setVerticalGroup(
  45.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  46.             .addGroup(layout.createSequentialGroup()
  47.                 .addGap(21, 21, 21)
  48.                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  49.                 .addContainerGap())
  50.         );
  51.  
  52.         pack();
  53.     }// </editor-fold>
  54.  
  55.  
  56.      private void displayMap(String url,JPanel panel,JLabel label) throws MalformedURLException{
  57.         URL imageurl=new URL(url);
  58.         Image image=(Toolkit.getDefaultToolkit().createImage(imageurl));
  59.         ImageIcon icon = new ImageIcon(image);
  60.         label.setIcon(icon);
  61.         panel.add(label);
  62.        // System.out.println(panel.getSize().width);
  63.         this.getContentPane().add(panel);
  64.     }
  65.     public static void main(String args[]) {
  66.         java.awt.EventQueue.invokeLater(new Runnable() {
  67.             public void run() {
  68.               //  new Test3().setVisible(true);
  69.             }
  70.         });
  71.     }
  72.  
  73.     // Variables declaration - do not modify
  74.     private javax.swing.JPanel jPanel1;
  75.     // End of variables declaration
  76.  
  77. }
  78.  
  79.  

My output:

Loadiiing ....Loadiiing 1....

When I press the jButton1, the test2 from freezes until the image loads.Are there alternative solutions to do this?
Dec 26 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.