0

I am writing a Java application which loads a file before performing any processing. I want to use an animated gif while the file is loading. I was able to create a JFrame which pops up as soon as the file loading starts and disappears just before the file is displayed in the GUI. Taking the help of these two answers, I was able to get this JFrame in place- Load a gif image untill a method execution and Creating a nice "LOADING..." animation

However, I am not able to display text or animated gif inside this JFrame. The JFrame is empty and shows nothing, but comes up and disappears on the desired time. Here is my code for the showProgress() and hideProgress() methods:

private JFrame loadingFrame = new JFrame("Notification");

private void showProgress() {
        //progress show code here
        ImageIcon loading = new ImageIcon("ajax-loader.gif");
        loadingFrame.add(new JLabel("File loading in progress. Please wait... ", loading, JLabel.CENTER));
        loadingFrame.pack();
        loadingFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loadingFrame.setSize(500, 200);
        loadingFrame.setLocationRelativeTo(this);
        loadingFrame.validate();
        loadingFrame.repaint();
        loadingFrame.setVisible(true);
    }

 private void hideProgress() {
        //progress show code here
        loadingFrame.setVisible(false); //you can't see me!
        loadingFrame.dispose(); //Destroy the JFrame object
    }

This is how the JFrame looks: enter image description here

The .gif file is in the same folder as the .java file. Can someone point to where am I going wrong?

With reference to the answer provided here Issue with gif animation in Java swing, I made changes to my showProgress() method, which now calls a URL available on the internet. Here is the code:

 public void showProgress() throws Exception{
        //  imageURL = getClass().getResource("Processing.gif");
        //  loading = new ImageIcon(imageURL);
        ImageIcon loading = new ImageIcon(
                new URL("https://i.stack.imgur.com/8IXqb.gif"));
       // loadingLabel = new JLabel("Please wait...");

        //JFrame
        loadingFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loadingFrame.setSize(400,300);
        loadingFrame.setLocationRelativeTo(this);
        loadingFrame.add(new JLabel(loading, JLabel.CENTER));
        loadingFrame.setVisible(true);
    } 
Community
  • 1
  • 1
novicegeek
  • 773
  • 2
  • 9
  • 29
  • 1
    Try using `ImageIcon loading = new ImageIcon(getClass().getResource("ajax-loader.gif"));` – MadProgrammer Apr 21 '16 at 10:22
  • Gives a `NullPointerException` – novicegeek Apr 21 '16 at 10:24
  • 1
    Then the file is unlikely in the place you think it is. Is the java file compiled and added to a jar file or is the class file on the disk (in a directory)? If it's in a jar, you need to inspect the jar (it's just a zip file) and ensure that both the class file and the image file are in the jar file and where you're expecting them to be. If it's a class file, you need to do the same thing, but just check the directory – MadProgrammer Apr 21 '16 at 10:28
  • Yes the java file is complied. But, you are right, I cannot see the gif file in the target folder where all the class files are present. I placed the gif file in this folder, so now the class file and the gif are in the same folder. I ran the application again, but still the same issue. I am also not sure why I cannot see the JLabel. – novicegeek Apr 21 '16 at 10:37
  • *"The .gif file is in the same folder as the .java file."* A 'folder' is a GUI representation of a directory that is irrelevant to this question, which is all to do with directories or paths within a Zip file (e.g. a Jar file of the app.). Speaking of which: Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Apr 21 '16 at 10:41
  • 1
    Oh, and neither of those questions nor the accepted answer seems to mention the EDT, understanding of which is critical to getting a 'loading' animation to work. Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. – Andrew Thompson Apr 21 '16 at 10:43
  • Yes. I understand your point on EDT and would try to implement it. However, for your suggestion related using a URL, I tried your using it, but still the jFrame displays nothing. I also tried your suggestion posted for this question: http://stackoverflow.com/questions/36432480/issue-with-gif-animation-in-java-swing?rq=1 I used exactly the same URL. – novicegeek Apr 21 '16 at 11:11
  • 1
    For sure now onwards @AndrewThompson – novicegeek Apr 21 '16 at 15:33
  • @AndrewThompson I pretty much understood the concept of Concurrency in Swing and also saw numerous examples for implementation. But still I am struggling to make it work for my GUI application. I am not sure how the doInBackground() process could return data for 3 different GUI components once the file is read. – novicegeek Apr 30 '16 at 12:08
  • The doInBackground() method should connect to R, use a Rscript to read the file, use another R script to extract some header information from the file, get back the values and then place them in three different components - As a JTree in a panel, the header information should go in JTextArea and the actual data from the file should be shown as a heat map in a separate panel. And, while this is being done in the background, a progress bar has to be displayed. Can you please help me with this? – novicegeek Apr 30 '16 at 12:09
  • @MadProgrammer May be you also have some inputs for my previous two comments. Thank you! – novicegeek Apr 30 '16 at 12:13
  • *"However, for your suggestion related using a URL,"* Either a deployed Jar file that includes the image, or to simply hot-link to one on the net, uses the same thing - an URL. In fact, we can even form a URL that points to a local file, but that's less useful ere. *"I tried your using it, but still the jFrame displays nothing."* Wait, you lost me there. Are you saying you tried it with the link to [this image](http://i.stack.imgur.com/8IXqb.gif) that I used in the self contained code in that answer? If so, please edit the question to show the new code that tried to load that image. – Andrew Thompson Apr 30 '16 at 12:39
  • @AndrewThompson I have added the new code. On a different note, since you suggested to using a different thread for the file reading process, I am trying to get this working and I am stuck (as mentioned in my previous two comments). – novicegeek Apr 30 '16 at 14:05
  • Here is where I have posted the SwingWorker issue: http://stackoverflow.com/questions/36956364/problems-in-using-swingworker-class-for-reading-a-file-and-implementing-a-jprogr – novicegeek Apr 30 '16 at 15:19
  • *"I have added the new code."* Sorry, thought it had already been mentioned, but I meant include a [mcve] of the new code. The answer that hot-linked to an image was an **MCVE.** I.E. it has everything you need to see the problem (including imports and a `main` method). – Andrew Thompson May 01 '16 at 01:55

0 Answers0