473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

108 100+
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
0 1328

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

Similar topics

3
by: Terry Carroll | last post by:
I've got a small batch image-processing program (it adds the time a digital photo was taken to the lower right of the image), and as a feature, I wanted to show a thumbnail of each image it was...
2
by: Tyrone Slothrop | last post by:
I am coding a site which involves uploading images. All of the PHP and display is OK but the client wants to be able to display the image thumbnail on the upload page and show the full image on...
1
by: Dev | last post by:
Dear Friends, I am passing the image name, size (in bytes) and imgaeformat (like jpg or bmp or pdf) through the network. I want display the image into picturebox without saving image...
1
by: Qindong Z. | last post by:
My asp.net application send image to web browser. The browser begin to display part of image before download finished. (In Slow Network). How can I force brower to display image after the image fully...
2
by: Manny Chohan | last post by:
HI Guys, I need to load image in table cell from codebehind. i have the following code: TableRow tRow = new TableRow(); Table2.Rows.Add(tRow); TableCell tCell1= new TableCell(); if...
0
by: adubra | last post by:
Hi there, I am using a device context (DC) and a buffer to successfully draw to screen. However, when I update the DC at very high frame rate and drag the frame containing the image very quickly...
2
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
The auto-generated line of code to fill a DataGridView once it is placed on the designer surface in VS2005 is typically: this.xyzTableAdapter.Fill(this.myDataSet.MyMember); But if the query...
1
by: shotokan99 | last post by:
i have this issue: showpic.php ================================================ $ch = curl_init(); $timeout = 0; curl_setopt ($ch, CURLOPT_URL, $xmyurl); curl_setopt ($ch,...
2
by: abcomp01 | last post by:
how to load image and display in flash from http post? i would to http post some website and return captcha something and display captcha from response some website from using actionscript...
2
by: nomaneagle | last post by:
ok this is the thing I have right now which is working quite well beside its a bit slow: Public Function GetList() As List(Of SalesOrder) Try Dim list As New List(Of...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.