Please, i am writing a program in java to read a file, perform some operation on each line of the file and them write back to another file. I would like to know how to use a progress bar to mark the progress of the task.
Hi Friend,
Try the following code:
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.html.*; public class ProgressBarExample extends JFrame{ int i=0; Timer timer; public ProgressBarExample() { final JLabel label=new JLabel(); final JButton button = new JButton("Start"); final JProgressBar pb = new JProgressBar(0, 20); pb.setValue(0); pb.setStringPainted(true); JPanel panel = new JPanel(); panel.add(button); panel.add(pb); JPanel panel1 = new JPanel(); panel1.add(panel, BorderLayout.NORTH); panel1.add(label, BorderLayout.CENTER); add(panel1); setSize(300,100); setVisible(true); timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent evt) { if (i == 20){ Toolkit.getDefaultToolkit().beep(); timer.stop(); button.setEnabled(true); pb.setValue(0); String str = "<html><font color=red>" + "<b>" + "File is readed." + "</b>" + "</font>" + "</html>"; label.setText(str); } i = i + 1; pb.setValue(i); } }); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { button.setEnabled(false); int i = 0; String str = "<html><font color=green><b>File Reading is in process.......</b></font></html>"; label.setText(str); timer.start(); try{ BufferedReader reader = new BufferedReader(new FileReader("c:/data.txt")); String line=null; while ((line=reader.readLine())!=null){ String data = line.replaceAll(" ",""); FileWriter fstream = new FileWriter("c:/out.txt",true); BufferedWriter output = new BufferedWriter(fstream); output.write(data); output.newLine(); output.close(); } } catch(Exception e){} } }); } public static void main(String[] args) { ProgressBarExample spb = new ProgressBarExample(); } }
Thanks
Hi Friend,
Try the following code:
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.html.*; public class ProgressBarExample extends JFrame{ int i=0; Timer timer; public ProgressBarExample() { final JLabel label=new JLabel(); final JButton button = new JButton("Start"); final JProgressBar pb = new JProgressBar(0, 20); pb.setValue(0); pb.setStringPainted(true); JPanel panel = new JPanel(); panel.add(button); panel.add(pb); JPanel panel1 = new JPanel(); panel1.add(panel, BorderLayout.NORTH); panel1.add(label, BorderLayout.CENTER); add(panel1); setSize(300,100); setVisible(true); timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent evt) { if (i == 20){ Toolkit.getDefaultToolkit().beep(); timer.stop(); button.setEnabled(true); pb.setValue(0); String str = "<html><font color=red>" + "<b>" + "File is readed." + "</b>" + "</font>" + "</html>"; label.setText(str); } i = i + 1; pb.setValue(i); } }); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { button.setEnabled(false); int i = 0; String str = "<html><font color=green><b>File Reading is in process.......</b></font></html>"; label.setText(str); timer.start(); try{ BufferedReader reader = new BufferedReader(new FileReader("c:/data.txt")); String line=null; while ((line=reader.readLine())!=null){ String data = line.replaceAll(" ",""); FileWriter fstream = new FileWriter("c:/out.txt",true); BufferedWriter output = new BufferedWriter(fstream); output.write(data); output.newLine(); output.close(); } } catch(Exception e){} } }); } public static void main(String[] args) { ProgressBarExample spb = new ProgressBarExample(); } }
Thanks
Ads