gif(Swing) and hibernate

I making an application with a lot of hibernate interaction and loading different images on panels from the data acquired from db. Now i want to add a gif to a popup(dialog) showing that the software is processing something. I have the following code to load the gif.

publicclass LoadingThreadextends JDialog{

JProgressBar progressBar;

Image image;

public LoadingThread(){

setBounds(100, 100, 100, 100);

ImageIcon img =new ImageIcon("C:\\loading.gif");

image = img.getImage();

MediaTracker tracker =new MediaTracker(this);

tracker.addImage(image, 0);

try{

tracker.waitForID(0);

}catch (InterruptedException ie){ie.printStackTrace();}

}

publicvoid paint(Graphics g)

{

g.drawImage(image,20,20,this);

}

publicboolean imageUpdate( Image img,int flags,int x,int y,

int w,int h )

{

System.out.println("Image update: flags="+flags+

" x="+x+" y="+y+" w="+w+" h="+h);

repaint();

returntrue;

}

}

Now the problem is that when the hibernate stuff is goin on together with other swing updates this popup dialog stops updating the gif and restarts it when everything else is done which obviously is coz of thread time, i have tried it with a different thread as well but it doesn't work,

Anybody has better idea for doin this?

Adnan

[2584 byte] By [commathgurua] at [2007-11-27 10:40:51]
# 1

Rather than using an animated gif to show progress why aren't you using a progressmonitor?

http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html#monitors

c0demonk3ya at 2007-7-28 19:09:15 > top of Java-index,Desktop,Core GUI APIs...
# 2

thanks for ur reply,

I had a look at them earlier but what i am looking for now is an animated gif either on the cursor or a popup window(request from our engineering department:D)

cheers,

Adnan

commathgurua at 2007-7-28 19:09:15 > top of Java-index,Desktop,Core GUI APIs...
# 3

If you have to go the animated gif way then the way I can see it working is to update the dialog from a seperate thread.

Try googling ExecutorService and Executors.newSingleThreadExecutor()

c0demonk3ya at 2007-7-28 19:09:15 > top of Java-index,Desktop,Core GUI APIs...
# 4

Though, personaly, I dont like spoon feeding, but by looking at your code (and problem) I thought of giving it a try.

Check the code, hopefully it will give you some idea of using threads.

public class CheckProgress {

public CheckProgress() {

JButton btn = new JButton("Start Long Task");

btn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

final LoadingThread lt = new LoadingThread();

boolean MODEL_DIALOG = false;

if (MODEL_DIALOG) {

lt.setModal(true);

SwingUtilities.invokeLater(new Runnable() {

public void run() {

lt.setVisible(true);

}

});

} else {

lt.setVisible(true);

}

new Thread(new Runnable() {

public void run() {

// your time consuming job will go here..

long i = 0;

do {

i++;

System.out

.println("Long runnig task is running...!!(i="+i+")");

try {

Thread.sleep(100);

} catch (Exception e) {

}

} while (i < 100);

// long job finished, dispose the dialog, in EDT

SwingUtilities.invokeLater(new Runnable() {

public void run() {

lt.setVisible(false);

lt.dispose();

}

});

}

}).start();

}

});

JPanel p = new JPanel();

p.add(btn);

// create your frame and add panel to it...and run!!

JFrame frame = new JFrame("Check Progress");

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.setSize(300,300);

frame.getContentPane().add(p);

frame.setVisible(true);

}

public static void main(String[] arg) {

new CheckProgress();

}

}

class LoadingThread extends JDialog {

Image image;

public LoadingThread() {

setSize(260, 160);// <-- as per your animated gif.

ImageIcon img = new ImageIcon("anim.gif");

image = img.getImage();

MediaTracker tracker = new MediaTracker(this);

tracker.addImage(image, 0);

try {

tracker.waitForID(0);

} catch (InterruptedException ie) {

ie.printStackTrace();

}

// <-- this is something that I added to the class.

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

}

public void paint(Graphics g) {

g.drawImage(image, 0, 0, this);

}

public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) {

// System.out.println("Image update: flags=" + flags + " x=" + x + " y="

// + y + " w=" + w + " h=" + h);

repaint();

return true;

}

}

1. if you dont set: DO_NOTHING_ON_CLOSE to dialog, user can close the dialog even the task is not completed.

2. if MODEL_DIALOG variable is false, user can click the "Start Long Task" button again to again start the same task, that will pop another dialog with animation. if you want, you can manage it by disabling/enabling the button in the flow. If the value is true, user can not do anything untill the task is over.

3. you may want to add some functionality to terminate the task in between, somehow..!!! :)

vijaywadnerea at 2007-7-28 19:09:15 > top of Java-index,Desktop,Core GUI APIs...