001 package jcolibri.test.test14; 002 003 import java.awt.Dimension; 004 import java.awt.Rectangle; 005 import java.awt.Toolkit; 006 007 import javax.swing.JFrame; 008 import javax.swing.JLabel; 009 import javax.swing.JPanel; 010 import javax.swing.JProgressBar; 011 import javax.swing.SwingConstants; 012 013 /** 014 * Progress bar window (using swing) that shows method progress. 015 * 016 * @author Juan A. Recio-García 017 * @version 1.0 018 */ 019 public class SwingProgressBar extends JFrame implements jcolibri.util.ProgressListener { 020 private static final long serialVersionUID = 1L; 021 022 JPanel jPanel1 = new JPanel(); 023 024 JProgressBar jProgressBar1 = new JProgressBar(); 025 026 JLabel jLabel1 = new JLabel(); 027 028 double stepPercentage; 029 030 double progress; 031 032 public SwingProgressBar() { 033 try { 034 init(); 035 } catch (Exception e) { 036 e.printStackTrace(); 037 } 038 } 039 040 private void init() throws Exception { 041 jPanel1.setLayout(null); 042 this.getContentPane().setLayout(null); 043 jPanel1.setBounds(new Rectangle(5, 5, 333, 80)); 044 this.setResizable(false); 045 this.setTitle("Method Progress"); 046 jProgressBar1.setStringPainted(true); 047 jProgressBar1.setBounds(new Rectangle(30, 40, 274, 16)); 048 jLabel1.setHorizontalAlignment(SwingConstants.CENTER); 049 jLabel1.setHorizontalTextPosition(SwingConstants.CENTER); 050 jLabel1.setText(""); 051 jLabel1.setBounds(new Rectangle(30, 9, 275, 23)); 052 this.getContentPane().add(jPanel1, null); 053 jPanel1.add(jLabel1, null); 054 jPanel1.add(jProgressBar1, null); 055 int w = 350; 056 int h = 100; 057 this.setSize(w, h); 058 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 059 int x = (dim.width - w) / 2; 060 int y = (dim.height - h) / 2; 061 this.setLocation(x, y); 062 } 063 064 /** 065 * Sets the label text. 066 * 067 * @param text 068 * label text. 069 */ 070 void setText(String text) { 071 jLabel1.setText(text); 072 } 073 074 /** 075 * Sets the limit of the progress bar count. 076 * 077 * @param limit 078 * limit of the progress bar count. 079 */ 080 void setLimit(int limit) { 081 this.stepPercentage = 100.0 / (double) limit; 082 } 083 084 /** 085 * Increase the progress bar count in 1 unit. 086 */ 087 public void step() { 088 if(!jProgressBar1.isIndeterminate()) 089 { 090 progress += stepPercentage; 091 jProgressBar1.setValue((int) progress); 092 } 093 /* 094 Rectangle progressRect = getBounds(); 095 progressRect.x = 0; 096 progressRect.y = 0; 097 this.paintComponents(this.getGraphics()); 098 */ 099 } 100 101 /** 102 * Initializes the progress bar frame. 103 * 104 * @param text 105 * text of the label. 106 * @param limit 107 * limit of the progress bar count. 108 */ 109 public void init(String text, int limit) { 110 if(limit>-1) 111 { 112 jProgressBar1.setIndeterminate(false); 113 jProgressBar1.setStringPainted(true); 114 this.setVisible(true); 115 jProgressBar1.setValue(0); 116 setText(text); 117 setLimit(limit); 118 progress = 0; 119 } 120 else 121 init(text); 122 } 123 124 /** 125 * Initializes the progress bar frame for an indeterminated length. 126 * 127 * @param text 128 * text of the label. 129 */ 130 private void init(String text) 131 { 132 jProgressBar1.setIndeterminate(true); 133 jProgressBar1.setStringPainted(false); 134 this.setVisible(true); 135 setText(text); 136 } 137 138 public void finish() { 139 this.setVisible(false); 140 141 } 142 143 144 }