Magazine
 
Tips ‘n’ Tricks
 

pb = new JProgressBar(0, 20);
pb.setValue(0);
pb.setStringPainted(true);
label = new JLabel(“Roseindia.net”);
JPanel panel = new JPanel();
panel.add(button);
panel.add(pb);
JPanel panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(panel, BorderLayout.NORTH);
panel1.add(label, BorderLayout.CENTER);
panel1.setBorder
(BorderFactory.createEmptyBorder(20, 20, 20, 20));
frame.setContentPane(panel1);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
//Create a timer.
timer = new Timer(interval, new
ActionListener() {
public void actionPerformed(ActionEvent
evt) {
if (i == 20){
timer.stop();
button.setEnabled(true);
pb.setValue(0);
String str = “<html>” + “<font
color=\”#FF0000\”>” + “<b>” +
“Downloading completed.” + “</b>” + “</
font>” + “</html>”;
label.setText(str);
i = i + 1;
pb.setValue(i);
}
}
);
}
class ButtonListener implements
ActionListener {
public void actionPerformed(ActionEvent
ae) { button.setEnabled(false);
i = 0;
String str = “<html>” +

 

“<font color=\”#008000\”>” + “<b>” +
“Downloading is in process.......” + “</b>”
+ “</font>” +“</html>”;
label.setText(str);
timer.start();
}}
public static void main(String[] args) {
SwingTimer spb = new SwingTimer();
}}

Compile and Run:

C:\JavaJazzup>javac SwingTimer.java
C:\JavaJazzup>java SwingTimer
Output:
The output of the above program appears as shown in the figure below.



When the start button is clicked, a string “Downloading is in process….” appears at the bottom as in the figure below.



The above string is replaced by the string “Downloading completed.” when the task is completed.

Sept 2007 | Java Jazz Up | 67
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF