0

Trying to show a progress bar during heavy Java loading. Gif image loads fine when it is run separately but with 2 threads running in parallel, only the frame comes up and no Gif image.

package com.manas.progress;

import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


    public class ImagePanel extends JPanel implements Runnable{


        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
         public void run() {
            // TODO Auto-generated method stub
              URL url = null;
            try {
                url = new URL("File:\\progress_bar.gif");
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                Icon icon = new ImageIcon(url);
                JLabel label = new JLabel(icon);

                JFrame f = new JFrame("Animation");
                f.getContentPane().add(label);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
        }
      }

Calling Method:

public void create(String json,String userName, String password, String filePath) throws ParserConfigurationException, SAXException, IOException {
    String auth = userName +":"+password;
    byte[] encodedBytes = Base64.encodeBase64(auth.getBytes());
    ImagePanel p1 = new ImagePanel();
    Thread thread = new Thread(p1);
    thread.run();   
        httpPost(json, new String(encodedBytes));
      }
Manas
  • 177
  • 3
  • 13
  • Please, post full code on both threads calling. And what's `p.run()` method? ImagePanel doesn't have such method – Ivan Pronin Aug 09 '17 at 20:33
  • updated the code – Manas Aug 09 '17 at 20:48
  • `thread.run();`??? No, that won't create a new background thread to run. Rather you should be calling `thread.start()`. Better still since this is Swing, use a `SwingWorker`. – Hovercraft Full Of Eels Aug 09 '17 at 20:53
  • 1
    You've also got Swing code in code that you may be trying to run off of the event code, which is not allowed. You need to separate the long running code from the GUI code, so that they can be called on separate threads. Please check out [Lesson: Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) -- for more on this. – Hovercraft Full Of Eels Aug 09 '17 at 21:00
  • 1
    [Here's an example](https://stackoverflow.com/a/23034216/522444) of using a JProgressBar with a background thread, here using a SwingWorker, and [here's another example](https://stackoverflow.com/a/14098406/522444), the first is mine, the 2nd is MadProgrammers. – Hovercraft Full Of Eels Aug 09 '17 at 21:06

0 Answers0