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:
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:
and of coure the other method, 'stopload()' does:
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:
Code :
Frame.pack();
Frame.revalidate();
Frame.repaint()
or also
Code :
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:
Code :
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.
Re: Loading icon GIF not showing properly
Can you make a SSCCE (small sample program that compiles and executes) that shows your problem?
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.
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.
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.
Re: Loading icon GIF not showing properly
Quote:
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.
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.
Re: Loading icon GIF not showing properly
You'll have to make a SSCCE that shows what happens.
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.
Code :
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
Re: Loading icon GIF not showing properly
Quote:
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.
Re: Loading icon GIF not showing properly
How do I trick that then?
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.
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?
Re: Loading icon GIF not showing properly
Quote:
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.
Quote:
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.
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)
Re: Loading icon GIF not showing properly
Quote:
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()
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
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!! :D
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.