1 36 37 40 41 42 import javax.swing.*; 43 import javax.swing.event.*; 44 import javax.swing.text.*; 45 import javax.swing.border.*; 46 import javax.swing.colorchooser.*; 47 import javax.swing.filechooser.*; 48 import javax.accessibility.*; 49 50 import java.awt.*; 51 import java.awt.event.*; 52 import java.beans.*; 53 import java.util.*; 54 import java.io.*; 55 import java.applet.*; 56 import java.net.*; 57 58 65 public class ProgressBarDemo extends DemoModule { 66 67 70 public static void main(String [] args) { 71 ProgressBarDemo demo = new ProgressBarDemo(null); 72 demo.mainImpl(); 73 } 74 75 78 public ProgressBarDemo(SwingSet2 swingset) { 79 super(swingset, "ProgressBarDemo", "toolbar/JProgressBar.gif"); 82 83 createProgressPanel(); 84 } 85 86 javax.swing.Timer timer = new javax.swing.Timer (18, createTextLoadAction()); 87 Action loadAction; 88 Action stopAction; 89 JProgressBar progressBar; 90 JTextArea progressTextArea; 91 92 void updateDragEnabled(boolean dragEnabled) { 93 progressTextArea.setDragEnabled(dragEnabled); 94 } 95 96 public void createProgressPanel() { 97 getDemoPanel().setLayout(new BorderLayout()); 98 99 JPanel textWrapper = new JPanel(new BorderLayout()); 100 textWrapper.setBorder(new SoftBevelBorder(BevelBorder.LOWERED)); 101 textWrapper.setAlignmentX(LEFT_ALIGNMENT); 102 progressTextArea = new MyTextArea(); 103 104 progressTextArea.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_area_name")); 105 progressTextArea.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_area_description")); 106 textWrapper.add(new JScrollPane(progressTextArea), BorderLayout.CENTER); 107 108 getDemoPanel().add(textWrapper, BorderLayout.CENTER); 109 110 JPanel progressPanel = new JPanel(); 111 getDemoPanel().add(progressPanel, BorderLayout.SOUTH); 112 113 progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, text.length()) { 114 public Dimension getPreferredSize() { 115 return new Dimension(300, super.getPreferredSize().height); 116 } 117 }; 118 progressBar.getAccessibleContext().setAccessibleName(getString("ProgressBarDemo.accessible_text_loading_progress")); 119 120 progressPanel.add(progressBar); 121 progressPanel.add(createLoadButton()); 122 progressPanel.add(createStopButton()); 123 } 124 125 public JButton createLoadButton() { 126 loadAction = new AbstractAction(getString("ProgressBarDemo.start_button")) { 127 public void actionPerformed(ActionEvent e) { 128 loadAction.setEnabled(false); 129 stopAction.setEnabled(true); 130 if (progressBar.getValue() == progressBar.getMaximum()) { 131 progressBar.setValue(0); 132 textLocation = 0; 133 progressTextArea.setText(""); 134 } 135 timer.start(); 136 } 137 }; 138 return createButton(loadAction); 139 } 140 141 public JButton createStopButton() { 142 stopAction = new AbstractAction(getString("ProgressBarDemo.stop_button")) { 143 public void actionPerformed(ActionEvent e) { 144 timer.stop(); 145 loadAction.setEnabled(true); 146 stopAction.setEnabled(false); 147 } 148 }; 149 return createButton(stopAction); 150 } 151 152 public JButton createButton(Action a) { 153 JButton b = new JButton(); 154 b.putClientProperty("displayActionText", Boolean.TRUE); 158 b.setAction(a); 159 return b; 160 } 161 162 163 int textLocation = 0; 164 165 String text = getString("ProgressBarDemo.text"); 166 167 public Action createTextLoadAction() { 168 return new AbstractAction("text load action") { 169 public void actionPerformed (ActionEvent e) { 170 if(progressBar.getValue() < progressBar.getMaximum()) { 171 progressBar.setValue(progressBar.getValue() + 1); 172 progressTextArea.append(text.substring(textLocation, textLocation+1)); 173 textLocation++; 174 } else { 175 timer.stop(); 176 loadAction.setEnabled(true); 177 stopAction.setEnabled(false); 178 } 179 } 180 }; 181 } 182 183 184 class MyTextArea extends JTextArea { 185 public MyTextArea() { 186 super(null, 0, 0); 187 setEditable(false); 188 setText(""); 189 } 190 191 public float getAlignmentX () { 192 return LEFT_ALIGNMENT; 193 } 194 195 public float getAlignmentY () { 196 return TOP_ALIGNMENT; 197 } 198 } 199 } 200 201 202 | Popular Tags |