1 16 package scriptella.interactive; 17 18 import javax.swing.JFrame ; 19 import javax.swing.JProgressBar ; 20 import javax.swing.SwingUtilities ; 21 import java.awt.Dimension ; 22 import java.awt.HeadlessException ; 23 24 25 31 public class SwingProgressIndicator extends ProgressIndicatorBase { 32 private ProgressWindow w; 33 34 public SwingProgressIndicator(String title) { 35 w = new ProgressWindow(title); 36 } 37 38 protected void show(final String label, final double progress) { 39 if (!w.isVisible()) { 40 w.setVisible(true); 41 } 42 43 w.update(label, (int) (progress * 100)); 44 45 try { 46 Thread.sleep(400); 47 } catch (InterruptedException e) { 48 e.printStackTrace(); } 50 } 51 52 protected void onComplete(final String label) { 53 super.onComplete(label); 54 w.setVisible(false); 55 w.dispose(); 56 } 57 58 private static class ProgressWindow extends JFrame { 59 JProgressBar pb = new JProgressBar (0, 100); 60 61 public ProgressWindow(String title) throws HeadlessException { 62 super(title); 63 pb.setStringPainted(true); 64 pb.setPreferredSize(new Dimension (500, 20)); 65 getContentPane().add(pb); 66 pack(); 67 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 68 69 setLocationRelativeTo(null); 70 } 71 72 public void update(final String message, final int progress) { 73 SwingUtilities.invokeLater(new Runnable () { 74 public void run() { 75 pb.setValue(progress); 76 pb.setString(message); 77 } 78 }); 79 } 80 } 81 } 82 | Popular Tags |