Κυριακή 16 Μαΐου 2010

Simple Swing Waiting Dialog

A common issue is how to inform user that GUI is not frozen due to a BUG but because it waits a process to be completed. We all know the "waiting dialogs". But as far as I know there is not any standard "waiting dialog" in Swing API. So I have implemented mine and I give you the source code..


/*
* WaitingDialog.java
*
* Created on May 15, 2010, 11:28:57 AM
*/
package view.widgets.waiting;

import javax.swing.JDialog;

/**
*
* @author George J. Anagnostaros [g.anagnostaros@gmail.com]
*/
public abstract class WaitingDialog extends javax.swing.JDialog {

/** Creates new form WaitingDialog */
public WaitingDialog(JDialog parent) {
super(parent);
initComponents();
this.setAlwaysOnTop(true);
this.setResizable(false);
}

@Override
public void setVisible(boolean isVisible) {
super.setVisible(isVisible);
}

public void execute() {
new Thread() {

public void run() {
Thread th = new Thread(new Runnable() {

@Override
public void run() {
try {
setModal(false);
setAlwaysOnTop(true);
setVisible(true);
executeTask();
} catch (Exception ex) {
handleExcepiton(ex);
}
}
});
th.start();
try {
th.join();
} catch (InterruptedException ex) {
System.out.println("Oh god...");
}
dispose();
}
}.start();
}

/**
* Handling of exception that occured while executing task
* @param e
*/
protected abstract void handleExcepiton(Exception e);

/**
* The task that should be executed while waiting
*/
protected abstract void executeTask() throws Exception;

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {

message = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

message.setText("Please wait..");
getContentPane().add(message, java.awt.BorderLayout.CENTER);

pack();
}//

// Variables declaration - do not modify
private javax.swing.JLabel message;
// End of variables declaration
}

LinkWithin

Blog Widget by LinkWithin

Mobile edition