예를들어 스윙 어플리케이션에서 Progress를 뛰어주고 싶을경우에 사용하면 되겠습니다.
Progress를 JDialog에서 생성하여 뛰워줘도 됩니다. 하지만 Progress 라는 것은 어떠한 작업을 할 경우
1. DataBase
2. File Load
... 등등이 되겠지요 가져오는 값들이 대용량일 경우에는 시간이 걸리기 때문에 이러한 상황에 대부분 Progress를 사용합니다.
이러한 경우 타이틀바가 필요가 없게되고 작업이 다 끝났을 경우 Progress 는 사라지기 때문에 JDialog보다는 JWindow를 사용하여 구현하는게 좋겟지요 UI상에서도 보기 좋습니다.
Source Code
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
class SwingApp extends JFrame implements ComponentListener {
private static final long serialVersionUID = -1706061272770838668L;
private static final int APP_WIDTH = 600;
private static final int APP_HEIGHT = 400;
private ProgressWindow theProgressWindow;
public SwingApp(String title) {
super(title);
setSize(APP_WIDTH, APP_HEIGHT);
Dimension theScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension theFrameSize = getSize();
int x = (int) (theScreenSize.getWidth() - theFrameSize.getWidth()) / 2;
int y = (int) (theScreenSize.getHeight() - theFrameSize.getHeight()) / 2;
this.setLocation(x, y);
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addComponentListener(this);
}
public void appearView() {
this.setVisible(true);
theProgressWindow = new ProgressWindow(this);
Thread destoryThread = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
theProgressWindow.dispose();
}
});
destoryThread.start();
}
public void componentResized(ComponentEvent e) { }
public void componentMoved(ComponentEvent e) {
this.theProgressWindow.setLocationRelativeTo(e.getComponent());
}
public void componentShown(ComponentEvent e) { }
public void componentHidden(ComponentEvent e) { }
}
class ProgressWindow extends JWindow {
private static final long serialVersionUID = -6524167698066722942L;
private static final String userDir = System.getProperty("user.dir");
private static final int PROGRESS_WIDTH = 50;
private static final int PROGRESS_HEIGHT = 50;
private JLabel imageLabel = new JLabel();
public ProgressWindow(JFrame theFrame) {
super(theFrame);
setSize(200, 150);
this.setLayout(null);
this.getRootPane().getContentPane().setBackground(Color.YELLOW);
String filePath = userDir + File.separator + "ajax-loader.gif";
ImageIcon imageIcon = new ImageIcon(filePath);
Dimension theWindowSize = getSize();
Dimension theLabelSize = new Dimension(PROGRESS_WIDTH, PROGRESS_HEIGHT);
int x = (int) (theWindowSize.getWidth() - theLabelSize.getWidth()) / 2;
int y = (int) (theWindowSize.getHeight() - theLabelSize.getHeight()) / 2;
imageLabel.setIcon(imageIcon);
imageLabel.setBounds(x, y, PROGRESS_WIDTH, PROGRESS_HEIGHT);
this.add(imageLabel);
setLocationRelativeTo(theFrame);
this.setVisible(true);
}
}
public class SwingTest {
public static void run() {
EventQueue.invokeLater(new Runnable() {
public void run() {
SwingApp theApp = new SwingApp("Swing App");
theApp.appearView();
}
});
}
public static void main(String[] args) {
run();
}
}
ajax-loader.gif File Download
URL : http://www.ajaxload.info/