1 8 9 package foxtrot.examples; 10 11 import java.awt.BorderLayout ; 12 import java.awt.Container ; 13 import java.awt.Dimension ; 14 import java.awt.Toolkit ; 15 import java.awt.event.ActionEvent ; 16 import java.awt.event.ActionListener ; 17 import java.util.ArrayList ; 18 19 import javax.swing.DefaultComboBoxModel ; 20 import javax.swing.JButton ; 21 import javax.swing.JFrame ; 22 import javax.swing.JPanel ; 23 import javax.swing.JProgressBar ; 24 import javax.swing.SwingUtilities ; 25 import javax.swing.border.EmptyBorder ; 26 27 import foxtrot.Job; 28 import foxtrot.Worker; 29 30 38 public class ProgressExample extends JFrame 39 { 40 private JButton button; 41 private JProgressBar bar; 42 private boolean running; 43 private boolean taskInterrupted; 44 45 public static void main(String [] args) 46 { 47 ProgressExample example = new ProgressExample(); 48 example.setVisible(true); 49 } 50 51 public ProgressExample() 52 { 53 super("Foxtrot Example"); 54 55 button = new JButton ("Run Task !"); 56 button.addActionListener(new ActionListener () 57 { 58 public void actionPerformed(ActionEvent e) 59 { 60 if (running) 61 onCancelClicked(); 62 else 63 onRunClicked(); 64 } 65 }); 66 67 bar = new JProgressBar (); 68 bar.setStringPainted(true); 69 70 setDefaultCloseOperation(EXIT_ON_CLOSE); 71 72 Container c = getContentPane(); 73 c.setLayout(new BorderLayout (0, 0)); 74 75 JPanel main = new JPanel (new BorderLayout (0, 0)); 76 main.setBorder(new EmptyBorder (35, 35, 35, 35)); 77 c.add(main, BorderLayout.CENTER); 78 79 JPanel p = new JPanel (new BorderLayout (20, 20)); 80 p.add(bar, BorderLayout.NORTH); 81 p.add(button, BorderLayout.SOUTH); 82 83 main.add(p); 84 85 setSize(300, 200); 86 87 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 88 Dimension size = getSize(); 89 int x = (screen.width - size.width) >> 1; 90 int y = (screen.height - size.height) >> 1; 91 setLocation(x, y); 92 } 93 94 private void onRunClicked() 95 { 96 running = true; 98 99 setTaskInterrupted(false); 102 103 button.setText("Cancel"); 106 107 ArrayList list = getData(); 110 111 button.setText("Run Task !"); 113 114 running = false; 116 117 if (list != null) 120 { 121 DefaultComboBoxModel model = new DefaultComboBoxModel (list.toArray()); 125 } 126 } 127 128 private void onCancelClicked() 129 { 130 132 setTaskInterrupted(true); 133 } 134 135 private ArrayList getData() 136 { 137 return (ArrayList )Worker.post(new Job() 138 { 139 public Object run() 140 { 141 ArrayList list = new ArrayList (); 142 StringBuffer buffer = new StringBuffer (); 143 144 int max = 20; 146 for (int i = 1; i <= max; ++i) 147 { 148 try 150 { 151 Thread.sleep(250); 152 } 153 catch (InterruptedException ignored) 154 { 155 } 156 157 Object data = new Object (); 159 list.add(data); 160 161 buffer.setLength(0); 163 buffer.append("Step ").append(i).append(" of ").append(max); 164 165 if (isTaskInterrupted()) 166 { 167 buffer.append(" - Interrupted !"); 168 update(i, max, buffer.toString()); 169 break; 170 } 171 else 172 { 173 update(i, max, buffer.toString()); 175 } 176 } 177 178 if (isTaskInterrupted()) 179 { 180 list.clear(); 183 return null; 184 } 185 else 186 { 187 return list; 188 } 189 } 190 }); 191 } 192 193 private void update(final int index, final int max, final String string) 194 { 195 199 SwingUtilities.invokeLater(new Runnable () 200 { 201 public void run() 202 { 203 bar.setMaximum(max); 204 bar.setValue(index); 205 bar.setString(string); 206 } 207 }); 208 } 209 210 private synchronized boolean isTaskInterrupted() 211 { 212 return taskInterrupted; 218 } 219 220 private synchronized void setTaskInterrupted(boolean value) 221 { 222 taskInterrupted = value; 225 } 226 } 227 | Popular Tags |