• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JDialogでモーダルなJPorgressBar付きSplash Screenを表示する
#navi(../)
RIGHT:Posted by [[aterai]] at 2009-11-09
*JDialogでモーダルなJPorgressBar付きSplash Screenを表示する [#oab96a8e]
JDialogでモーダルなJPorgressBar付きSplash Screenを表示します。

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTRSxG9iaI/AAAAAAAAAg8/Wpd3hycacS4/s800/ProgressSplashScreen.png)

**サンプルコード [#id39b4bd]
#code{{
final JFrame frame = new JFrame();
final JDialog splashScreen  = new JDialog(frame, Dialog.ModalityType.DOCUMENT_MODAL);
final JProgressBar progress = new JProgressBar();
EventQueue.invokeLater(new Runnable() {
  public void run() {
    splashScreen.setUndecorated(true);
    splashScreen.getContentPane().add(new JLabel(new ImageIcon(getClass().getResource("splash.png"))));
    splashScreen.getContentPane().add(progress, BorderLayout.SOUTH);
    splashScreen.pack();
    splashScreen.setLocationRelativeTo(null);
    splashScreen.setVisible(true);
  }
});
SwingWorker<Void,Void> worker = new SwingWorker<Void,Void>() {
  public Void doInBackground() {
    try{
      int current = 0;
      int lengthOfTask = 120;
      while(current<lengthOfTask && !isCancelled()) {
        try {
          Thread.sleep(50);
        }catch(InterruptedException ie) {
          ie.printStackTrace();
          return null;
        }
        setProgress(100 * current++ / lengthOfTask);
      }
    }catch(Exception ex) {
      ex.printStackTrace();
    }
    return null;
  }
  public void done() {
    splashScreen.dispose();
  }
};
worker.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
  public void propertyChange(java.beans.PropertyChangeEvent e) {
    if("progress".equals(e.getPropertyName())) {
      progress.setValue((Integer)e.getNewValue());
    }
  }
});
worker.execute();

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
EventQueue.invokeLater(new Runnable() {
  public void run() {
    frame.setVisible(true);
  }
});
}}

**解説 [#febdc412]
上記のサンプルでは、以下のようなスプラッシュスクリーンを表示しています。
- モーダルなJDialogに、画像とJProgressBarを追加
- Dialog#setUndecorated(true)として、タイトルバーを非表示
- SwingWorkerを使って、進捗状態を表示

**参考リンク [#w26f24db]
-[[JWindowを使ったSplash Screenの表示>Swing/SplashScreen]]

**コメント [#i7601646]
#comment