Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ProgressBar

Status
Not open for further replies.

apffal

Technical User
Apr 10, 2004
97
PT
I'm building a setup application which copies a big file to the target machine.
How must I modify the ProgressBarDemo (from Sun website) in order to show the progress of copying files ?
Thanks for help.

There is the ProgressBarDemo code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ProgressBarDemo extends JFrame {
public final static int ONE_SECOND = 1000;

private JProgressBar progressBar;
private Timer timer;
private JButton startButton;
private LongTask task;
private JTextArea taskOutput;
private String newline = "\n";

public ProgressBarDemo() {
super("ProgressBarDemo");
task = new LongTask();

//Create the demo's UI.
startButton = new JButton("Start");
startButton.setActionCommand("start");
startButton.addActionListener(new ButtonListener());

progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(0);
progressBar.setStringPainted(true);

taskOutput = new JTextArea(5, 20);
taskOutput.setMargin(new Insets(5,5,5,5));
taskOutput.setEditable(false);

JPanel panel = new JPanel();
panel.add(startButton);
panel.add(progressBar);

JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.NORTH);
contentPane.add(new JScrollPane(taskOutput), BorderLayout.CENTER);
contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
setContentPane(contentPane);

//Create a timer.
timer = new Timer(ONE_SECOND, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
progressBar.setValue(task.getCurrent());
taskOutput.append(task.getMessage() + newline);
taskOutput.setCaretPosition(
taskOutput.getDocument().getLength());
if (task.done()) {
Toolkit.getDefaultToolkit().beep();
timer.stop();
startButton.setEnabled(true);
progressBar.setValue(progressBar.getMinimum());
}
}
});
}

/**
* The actionPerformed method in this class
* is called when the user presses the start button.
*/
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
startButton.setEnabled(false);
task.go();
timer.start();
}
}

public static void main(String[] args) {
JFrame frame = new ProgressBarDemo();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

frame.pack();
frame.setVisible(true);
}
 
What have you got so far (apart from copying an example from Sun :p ) ?

The LongTask class - I would implement your own :

It needs to extend Thread, and when you hit go() it needs to start copying the file (and should kick off the Thread).
The getLengthOfTask() method should return the total size of the file you are copying.
The getCurrent() shoud return the number of bytes copied so far.


--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top