Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Loading icon GIF not showing properly

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Loading icon GIF not showing properly

    Hello,

    I am trying to add a loading Icon on my application.
    It is basically a GIF of a rotating circle.

    Whenever the user presses the Search button, and sends a request to the DataBase for some information, the loading icon should be shown and then it should disappear whenever the loading process is completed.


    Here is a piece of code:

    Icon = new ImageIcon("load.gif"); 
    		JLabel L = new JLabel(Icon);
    		L.setBounds(0, 50, 65, 65);
    		L.setVisible(false);

    This is called on the init() method of the class.
    Whenever the user presses the search button the method 'loading()' is invoked, it basically does:

    		L.setVisible(true);

    and of coure the other method, 'stopload()' does:

    		L.setVisible(false);

    The problem is that the loading icon is not shown.
    I figured out that problem might be related to the fact that the GUI does not refresh (or show the component) in time and the icon is set back to visible(false) before it can be shown.
    In fact, if I do not put the 'stopload()' the icon is shown properly but, of course, does not stop.

    I have tried to add:

    Frame.pack();
    Frame.revalidate();
    Frame.repaint()

    or also

    Panel.validate();
    Panel.repaint();
    [the icon is added on this Panel]

    and this nothing...

    Basically the GUI is properly refreshed only when the Table (containing the results of the query) is shown/updated.

    I have also tried to invoke the start method using a Thread, eg:

    ThreadIcreated.start();

    but still nothing changes..


    PS. I always had problems in refreshing the Panel or Frame. In fact, for another issue I had to use some sort of trick in order to force the repaint of a specific component.
    I used:

    Panel.setvisible(false);
    Panel.setvisible(true);

    [I basically hide and show the panel]

    I tried to run that same method that works for another component, but it does not work for this...


    I wish you a good day!

    N.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loading icon GIF not showing properly

    Can you make a SSCCE (small sample program that compiles and executes) that shows your problem?

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loading icon GIF not showing properly

    I wanted to say that it is quite hard to come up with an SSCCE as the project is quite big and the resources are many.

    Do you think you could just look over it for the moment? I might make one tomorrow.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loading icon GIF not showing properly

    I'll wait for the SSCCE.
    You'd need some GUI with the label and some buttons. Press one button to show the icon, press the other button to stop showing the icon/hide the panel.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loading icon GIF not showing properly

    yep, that's what I did. But the problem is not showing the Label itself. Is to integrate with something else.
    Because if I put just a button that shows and hides the icon, everything is fine.

    The problem is when I want to start showing the icon BEFORE a determinate action (e.g. loading a table) and hide it when it is done.
    But the frame reloads only when the table is updated (therefore the purpose of the loading icon id defied).

    Is it clearer now?
    That's why it would be quite hard to reproduce a similar problem, as I am creating a table while getting info from a DB.

    I simply want to find a way to 'refresh' the panel, in the same way it is 'refreshed' when a new component is added. For the table I use the 'firetablechanged' in order to refresh the content of the table (if it is already shown).
    I would like to force the update of the GUI myself in order to see the Loading Icon before the method for the table update is invoked.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loading icon GIF not showing properly

    I want to start showing the icon BEFORE a determinate action
    How do you determine when the "BEFORE" event happens?

    Is this the problem: determining when "BEFORE" is?
    Once you can detect it is BEFORE the event then the rest you know how to do.

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loading icon GIF not showing properly

    Ok here is what happens:

    1-User selects some options;

    2-U. press on the Search Button.

    2.1*right at this time the Icon.setvisible(true) is used

    2.2*in the same action called by the JButton there is the method for searching

    2.3*whenever that is completed: Icon.setvisible(false) is used.

    3-Table is shown.


    Using this, the Icon is NOT shown.

    But if I remove 2.3 (setvisible(false)), after the Table is shown, also the Loading Icon appears (and of course remains there).
    The icon though appears at the same time of the Table.
    I want it to be shown BEFORE and be hidden right AFTER.

    Since it is shown ONLY when also the Table is shown, I figured out that maybe the problem is that whenever the Panel/Frame draws the Table it also draws the Icon. Therefore I am looking for a way to force this 'drawing' action when I modify the visible property of the Icon.

    I tried all the available methods and none of them actually did anything. It might be possible that this is not the problem, but I just follow my instinct and assumed.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loading icon GIF not showing properly

    You'll have to make a SSCCE that shows what happens.

  9. #9
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loading icon GIF not showing properly

    Here is a simple application showing what works and what does not.
    I hope I made it simple enough and of course clear enough as well.

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.*;
     
     
    public class Loading {
     
    	static protected JLabel LoadingLabel;
     
    	public static void main(String[] args) {
     
     
    		//Just a frame
    		JFrame f = new JFrame("Loading Test");
     
    		f.setSize(500, 500);
    		f.setLocation(0,0);
     
    		f.setLayout(new FlowLayout());
     
     
    		//The Loading Icon
    		ImageIcon icon = new ImageIcon("src/load.gif");
    		LoadingLabel = new JLabel(icon);
    		LoadingLabel.setSize(50, 50);
    		LoadingLabel.setVisible(false);
     
    		//Button showing Loading: it works
    		JButton b1 = new JButton("LoadStartOK");
    		b1.addActionListener(new ActionListener() {
     
    			public void actionPerformed(ActionEvent e) {
     
    				startLoad();
     
     
    			}
     
    		});
     
     
    		//Button stopping Loading: it works
    		JButton b2 = new JButton("LoadStopOK");
    		b2.addActionListener(new ActionListener() {
     
    			public void actionPerformed(ActionEvent e) {
     
    				stopLoad();
     
    			}
     
    		});
     
     
     
    		//Button showing loading and then after 2sec (the time for performing a method) and then it stops it
    		//It is bugged, in fact the loading is drawn only when the whole actioPerformed is completed
    		JButton bCHECK = new JButton("LoadStart+Stop Bugged");
    		bCHECK.addActionListener(new ActionListener() {
     
    			public void actionPerformed(ActionEvent e) {
     
    				startLoad();
     
     
    				try{
     
    					Thread.sleep(2000);
     
     
    				}catch(Exception r){}
     
    				stopLoad();
     
     
    			}
     
    		});
     
    		f.setVisible(true);
    		f.add(LoadingLabel);
    		f.add(b1);
    		f.add(b2);
    		f.add(bCHECK);
    	}
     
    //Just sets its visibility
    	protected static void startLoad()
    	{
     
    		LoadingLabel.setVisible(true);
     
     
     
    	}
    //Just sets its visibility	
    	protected static void stopLoad()
    	{
     
    		LoadingLabel.setVisible(false);
     
     
     
    	}
    }

    The "load.gif" is simply this file: http://www.melliat.com/images/load.gif
    Last edited by Nesh108; December 14th, 2011 at 03:40 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loading icon GIF not showing properly

    the loading is drawn only when the whole actionPerformed is completed
    Yes, that is the way it works. The JVM calls the actionPerformed method on its GUI thread.
    No GUI will be updated until the actionPerformed method exits.

  11. #11
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loading icon GIF not showing properly

    How do I trick that then?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loading icon GIF not showing properly

    If you want to start the showing of the image and then stop it at a later time, use a Timer.
    Start the showing, start the timer, exit the a..P.. method. The GUI will be updated to show the image.At a later time the timer will call a method that you provide where you can stop the showing of the image.

  13. #13
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loading icon GIF not showing properly

    Nono you don't get it, the problem is not hiding (for the moment)
    It so show the loading icon.

    The only purpose of the Loading Icon is to fill the empty gap between the press of the button and its result. If with the ActionPer. I need to wait for it to be completed then invoking the method there is pointless.

    Let's re-phrase then:

    Q: How do I do a Loading Icon for filling the time during the execution time of a method?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loading icon GIF not showing properly

    The only purpose of the Loading Icon is to fill the empty gap between the press of the button and its result.
    You should NOT be doing anything in the actionPerformed method that takes seconds. The GUI will be frozen while the actionPerformed method is executing.
    Perhaps the a...P... method should start a new Thread where you can do the long running job.

    How do I do a Loading Icon for filling the time during the execution time of a method?
    If the method is not using the GUI's thread, then you can call startLoad() at the beginning and stopLoad() at the end.

  15. #15
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loading icon GIF not showing properly

    Sadly the method is using the GUI thread as it is when a User presses a button.

    Basically, when a User presses that button, the DB is interrogated and the app retrieves the results and creates/updates the table.

    Since this might take few seconds, I want to show this loading icon (just for craic of it).


    in the A.P. there is something like this:

    button.AP---------

    //startLoading()

    doTheSearchOnTheDB(var1,var2,var3);

    //stopLoading()

    --------

    And I cant just launch a thread because, of course, I dont know how long it will take (might take 0.5s or 8s)

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loading icon GIF not showing properly

    And I cant just launch a thread
    Your SearchTheDB job looks like it should be done in a thread.
    Create the thread and start it in the a..P.. method and let it run until it completes.
    The a..P... method returns as soon as the the thread is started. The thread does these steps in the thread:

    //startLoading()

    doTheSearchOnTheDB(var1,var2,var3);

    //stopLoading()

  17. The Following User Says Thank You to Norm For This Useful Post:

    Nesh108 (December 14th, 2011)

  18. #17
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loading icon GIF not showing properly

    Great!
    It works with

    Thread1.start();

    The only problem is that now, if I press 'Search' again it gives an error, as I did not stop the Thread.

    #I also tried with 'Thread1.run()' but of course nothing changed from before

    How and when do I stop the thread?

    Should I stop the thread and start it whenever I press the Search btn?

    Example:

    button.AP----

    Thread1.stop();

    Thread1.start();

    -----------

    so if it was stopped it should not (hopefully) crash, if it was started it should stop it.

    This might work, but is this the correct way to do it?

    PS.

    I tried... It crashed.

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException

  19. #18
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Loading icon GIF not showing properly

    Thanks again, fixed by adding:

    Thread1 = new Thread(Search);
    Thread1.start();

    into the AP. In this way each time it is pressed a new thread is created, which is a legal operation!

    Thanksss!!

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Loading icon GIF not showing properly

    Or you could disable the button that starts the Thread when the thread starts and then enable it when the thread finishes.

Similar Threads

  1. JToggleButton's icon won't show up when pressed!
    By andreiutz10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2011, 05:03 AM
  2. window icon
    By luisp88 in forum AWT / Java Swing
    Replies: 6
    Last Post: September 29th, 2011, 01:56 PM
  3. Moving Icon in gridlayout SWING
    By Loodistobilo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 24th, 2010, 07:59 PM
  4. [SOLVED] I cant load the icon!!!!
    By chronoz13 in forum AWT / Java Swing
    Replies: 12
    Last Post: January 22nd, 2010, 03:52 AM
  5. Icon change and lib folder problem
    By LeonLanford in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2009, 03:00 AM