Swing/ProgressSplashScreen のバックアップ(No.8)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/ProgressSplashScreen へ行く。
- 1 (2011-03-26 (土) 23:13:17)
- 2 (2012-07-20 (金) 11:21:50)
- 3 (2013-01-04 (金) 16:01:37)
- 4 (2013-04-06 (土) 05:28:19)
- 5 (2013-06-20 (木) 12:02:31)
- 6 (2014-11-22 (土) 03:59:58)
- 7 (2014-12-14 (日) 14:37:34)
- 8 (2015-02-18 (水) 15:05:40)
- 9 (2016-11-29 (火) 13:55:07)
- 10 (2017-11-28 (火) 13:55:17)
- 11 (2019-08-01 (木) 22:51:48)
- 12 (2021-03-31 (水) 02:34:06)
- 13 (2024-02-10 (土) 17:48:26)
- title: JDialogでモーダルなJProgressBar付きSplash Screenを表示する tags: [JDialog, JProgressBar, SwingWorker, ModalityType] author: aterai pubdate: 2009-11-09T14:54:41+09:00 description: JDialogでモーダルなJProgressBar付きSplash Screenを表示します。
概要
JDialog
でモーダルなJProgressBar
付きSplash Screen
を表示します。
Screenshot
Advertisement
サンプルコード
final JFrame frame = new JFrame("@title@");
final JDialog splashScreen = new JDialog(
frame, Dialog.ModalityType.DOCUMENT_MODAL);
final JProgressBar progress = new JProgressBar();
System.out.println(splashScreen.getModalityType());
EventQueue.invokeLater(new Runnable() {
@Override 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>() {
@Override 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;
}
@Override public void done() {
splashScreen.dispose();
}
};
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override public void propertyChange(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() {
@Override public void run() {
frame.setVisible(true);
}
});
View in GitHub: Java, Kotlin解説
上記のサンプルでは、以下のようなスプラッシュスクリーンを表示しています。
- モーダルな
JDialog
に、画像とJProgressBar
を追加 Dialog#setUndecorated(true)
として、タイトルバーを非表示SwingWorker
を使って、進捗状態を表示
JTabbedPane
に順次タブを追加していくテスト
//package example;
//-*- mode:java; encoding:utf8n; coding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.beans.*;
import java.util.List;
import javax.swing.*;
public class ProgressSplashScreenTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
final JFrame frame = new JFrame();
final JDialog splashScreen = new JDialog(
frame, Dialog.ModalityType.DOCUMENT_MODAL);
final JProgressBar progress = new JProgressBar();
final JTabbedPane tabbedPane = new JTabbedPane();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(tabbedPane);
progress.setStringPainted(true);
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
splashScreen.setUndecorated(true);
splashScreen.getContentPane().add(
new JLabel(new SplashScreenIcon()));
splashScreen.getContentPane().add(progress, BorderLayout.SOUTH);
splashScreen.pack();
splashScreen.setLocationRelativeTo(null);
splashScreen.setVisible(true);
}
});
SwingWorker<Void, String> worker = new SwingWorker<Void, String>() {
@Override public Void doInBackground() {
try {
int current = 0;
int lengthOfTask = 120;
while (current <= lengthOfTask && !isCancelled()) {
try {
Thread.sleep(50); //dummy
} catch (InterruptedException ie) {
ie.printStackTrace();
return null;
}
if (current == 20) {
publish("showFrame");
} else if (current % 24 == 0) {
publish("title: "+current);
}
setProgress(100 * current++ / lengthOfTask);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override protected void process(List<String> chunks) {
for (String cmd : chunks) {
if (cmd.equals("showFrame")) {
frame.setSize(512, 320);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} else {
tabbedPane.addTab(cmd, new JLabel(cmd));
tabbedPane.setSelectedIndex(tabbedPane.getTabCount()-1);
progress.setString("Loading: "+cmd);
}
}
}
@Override public void done() {
splashScreen.dispose();
}
};
worker.addPropertyChangeListener(new PropertyChangeListener() {
@Override public void propertyChange(PropertyChangeEvent e) {
if ("progress".equals(e.getPropertyName())) {
progress.setValue((Integer)e.getNewValue());
}
}
});
worker.execute();
}
}
class SplashScreenIcon implements Icon {
@Override public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g.create();
g2.translate(x, y);
g2.setPaint(Color.GREEN);
g2.fillRect(10, 10, 180, 80);
g2.translate(-x, -y);
g2.dispose();
}
@Override public int getIconWidth() {
return 200;
}
@Override public int getIconHeight() {
return 100;
}
}