How to use JProgressBar in my code

Hello people,

i am getting stuck, i cannot figure out how to use ProgressMonitorInputStream in my code. Can some one give an example to use JProgressBar,ProgressMonitor or ProgressMonitorInputStream, which one is suitable for my code. I want the Dialog occur during the zipping, it is nice if there is procent output like from 0% until 100%, so i know if the zipping finishs. Any help would be appreciated.

import javax.swing.*;

import javax.swing.ProgressMonitorInputStream.*;

import java.awt.*;

import java.awt.event.*;

import java.util.zip.*;

import java.io.*;

public class ZipGui extends JFrame

{

private JFileChooser jfc = new JFileChooser();

private JLabel source_jlabel = new JLabel("File to be zipped :");

private JLabel destination_jlabel = new JLabel("Destination :");

private JTextField source_jtextfield = new JTextField("c:\\directory");

private JTextField destination_jtextfield = new JTextField("c:\\destination+name");

private JButton source_jbutton = new JButton("Browser");

private JButton destination_jbutton = new JButton("Browser");

private JLabel ziporexit_jlabel = new JLabel("Action :");

private JButton zip_jbutton = new JButton("Zip");

private JButton exit_jbutton = new JButton("Exit");

public ZipGui(String title)

{

super(title);

Container pane = getContentPane();

pane.setLayout(new GridLayout(3,3));

pane.add(source_jlabel);

pane.add(source_jtextfield);

pane.add(source_jbutton);

pane.add(destination_jlabel);

pane.add(destination_jtextfield);

pane.add(destination_jbutton);

pane.add(ziporexit_jlabel);

pane.add(zip_jbutton);

pane.add(exit_jbutton);

source_jbutton.addActionListener(new BrowserActionListener());

destination_jbutton.addActionListener(new BrowserActionListener());

zip_jbutton.addActionListener(new ZipActionListener());

exit_jbutton.addActionListener

(

new ActionListener()

{

public void actionPerformed(ActionEvent ae)

{

Runtime.getRuntime().exit(0);

}

}

);

}

public class BrowserActionListener implements ActionListener

{

public void actionPerformed(ActionEvent ae)

{

if(ae.getSource() == source_jbutton)

{

//browser both files and directories

jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

int state = jfc.showOpenDialog(null);

if(state == JFileChooser.APPROVE_OPTION)

{

source_jtextfield.setText(jfc.getSelectedFile().getPath());

}

else if(state == JFileChooser.CANCEL_OPTION)

{

return;

}

}

else if(ae.getSource() == destination_jbutton)

{

//only browser directory to be zipped

jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

int state = jfc.showOpenDialog(null);

if(state == JFileChooser.APPROVE_OPTION)

{

destination_jtextfield.setText(jfc.getSelectedFile().getPath());

}

else if(state == JFileChooser.CANCEL_OPTION)

{

return;

}

}

}

}

public class ZipActionListener implements ActionListener

{

public void actionPerformed(ActionEvent ae)

{

try

{

zip();

}

catch(IOException ioe)

{

JOptionPane.showMessageDialog(null,ioe.toString());

}

}

}

public void zip() throws IOException

{

byte[] buffer = new byte[1024];

int pos = 0;

FileOutputStream fos = new FileOutputStream(new File(destination_jtextfield.getText()));

ZipOutputStream zos = new ZipOutputStream(fos);

File file = new File(source_jtextfield.getText());

String absolute = file.getAbsolutePath();

String relativ = absolute.substring(absolute.lastIndexOf(File.separator)+1);

FileInputStream fis = new FileInputStream(file);

ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(this,"Zip "+relativ,fis);

ZipEntry entry = new ZipEntry(relativ);

zos.setMethod(ZipOutputStream.DEFLATED);

zos.setLevel(9);

zos.putNextEntry(entry);

while((pos = pmis.read(buffer)) != -1)

{

zos.write(buffer,0,pos);

}

fis.close();

zos.close();

pmis.close();

}

public static void main(String[] args)

{

String LookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();

try

{

UIManager.setLookAndFeel(LookAndFeel);

}

catch(Exception e)

{

e.getMessage();

}

ZipGui zip = new ZipGui("ZipMaker");

zip.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

zip.setVisible(true);

zip.pack();

}

}

[4870 byte] By [bruslia] at [2007-10-2 10:47:46]
# 1
Use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] when posting code so the code is readable.[url http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html]How to Use Progress Bars[/url]
camickra at 2007-7-13 3:03:26 > top of Java-index,Desktop,Core GUI APIs...