Creating a Download Status Dialog Box


The ProgressTest.java file helps create a Download Status dialog box for the File Dialog application. This dialog box displays a progress bar and a label to show the download status.

Listing 3-6 shows the contents of the ProgressTest.java file:

Listing 3-6: The ProgressTest.java File
start example
 /* Imports java.util package class. */ import java.util.*; /* Imports java.awt package classes. */ import java.awt.*; /* Imports java.awt.event package class. */ import java.awt.event.*; /* Imports javax.swing package classes. */ import javax.swing.*; /* Imports javax.swing.event package class. */ import javax.swing.event.*; /* Imports java.io package class. */ import java.io.File; /* class ProgressTest - Creates a Download Status dialog box that indicates how much data is downloaded from the server. Fields: label - Contains the content of title label. ok - Creates an OK button. size - Contains the size of the file. path - Contains the path where the end user wants to save the file. th - Contains the instance of the downloading thread. Method: performTask() - This method is called when the ProgressBar is painted. actionPerformed() - This method is invoked when the end user clicks the OK or Cancel button. */ public class ProgressTest extends JDialog implements ActionListener {    /* Declares the objects of the JLabel class. */    JLabel label;    JLabel label_1;    JLabel label_2;    /* Declares the object of the JButton class. */    JButton ok;    /* Declares the object of the JProgressbar class. */    JProgressBar bar;    /* Declares the object of the GridBagLayout class. */    GridBagLayout gbl;    /* Declare the object of the GridBagConstraints class. */    GridBagConstraints gbc;    /* Declares the objects of the string class. */    String str;    String path;    /* Declares and initialize the size as integer. */    int size = 100;    /* Declares the object of the Thread class. */    Thread th;    int n = 0;    /*    ProgressTest() - This is the default constructor of the ProgressTest class.    Parameter:    size - Represents the file size.     path - Represents the file path where the end user saves the file.    th - Represents the instance of the Thread class.    */    public ProgressTest(int size, String path, Thread th)    {       this.size = size;       this.path = path;       this.th = th;       /* Sets the size of the Download Status dialog box. */       setSize(270, 150);       /* Sets the visibility of the Download Status dialog box. */       setVisible(true);       /*        Sets the reliability of the Download Status dialog box to false.        */       setResizable(false);       /* Sets the title of the Download Status dialog box. */       setTitle("Download Status");       /* Initializes the object of the G   ridBagLayout class. */       gbl = new GridBagLayout();       /* Sets the Layout. */       getContentPane().setLayout(gbl);       /* Creates an object of the GridBagConstraints class. */       gbc = new GridBagConstraints();       /*        Initializes the label object and add it to the 1,1,1,1 position with WEST alignment.        */       gbc.gridx = 1;          gbc.gridy = 1;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       label = new JLabel(" File is downloading");       getContentPane().add(label, gbc);       /*        Initializes a blank label object and add it to the 1,2,1,1 position with WEST alignment.        */       gbc.gridx = 1;          gbc.gridy = 2;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       label_1 = new JLabel(" ");       getContentPane().add(label_1, gbc);       /*        Initializes an object of the JprogressBar class and adds it to the 1,3,1,1 position with CENTER alignment.        */       gbc.gridx = 1;          gbc.gridy = 3;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.CENTER;       bar = new JProgressBar();       /* Sets the size of the progress bar. */       bar.setPreferredSize(new Dimension(225, 20));       bar.setMinimum( 0 );       bar.setMaximum( size );       bar.setValue( 0 );       bar.setBorderPainted(true);       bar.setStringPainted(true);        getContentPane().add(bar, gbc);       /*        Initializes a blank label object and adds it to the 1,4,1,1 position with WEST alignment.        */       gbc.gridx = 1;          gbc.gridy = 4;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.WEST;       label_2 = new JLabel(" ");       getContentPane().add(label_2, gbc);       /*        Initializes an object of the Button class and adds it to the 1,5,1,1 position with CENTER alignment.        */       gbc.gridx = 1;          gbc.gridy = 5;       gbc.gridwidth = 1;       gbc.gridheight = 1;       gbc.anchor = GridBagConstraints.CENTER;       ok = new JButton(" Cancel ");       ok.addActionListener(this);       getContentPane().add(ok, gbc);          n = 100;       /*        Starts the progress bar until the file is downloaded.        */        for( int iCtr = 1; iCtr < size+1; iCtr +=n )       {          /*           Calls the performTask() method to insert a time delay.           */          performTask( iCtr );          /* Updates the progress indicator and label. */          label.setText( "Downloading "+ iCtr + " bytes of " + size + " bytes.");          /* Creates an object of the Rectangle class that gets the label bound. */          Rectangle labelRect = label.getBounds();          labelRect.x = 0;          labelRect.y = 0;          /* Paints the label. */          label.paintImmediately( labelRect );          /* Sets the value to the progress bar. */          bar.setValue( iCtr );          /*           Creates an object of the Rectangle class that gets the progress bar bound.           */          Rectangle progressRect = bar.getBounds();          progressRect.x = 0;          progressRect.y = 0;          /* Paints the progress bar. */             bar.paintImmediately( progressRect );       }    }    /*    performTask() - This method provides a time delay for the progress bar creating loop.     Paramter: ictr    Return Value: NA    */    public void performTask( int iCtr )    {       Random random = new Random( iCtr );       for( int i = 0; i < random.nextFloat() * 1000; i++ )       {          /* Runs the loop */       }    }    /*    actionPerformed() - This method is invoked when the end user clicks the button.    Parameter: ae - an ActionEvent object containing the details of the event.    Returns Value: NA    */    public void actionPerformed(ActionEvent ae)    {       String arg = (String)ae.getActionCommand();       if(arg.equals(" Cancel "))       {          /* Stops the downloading thread. */          th.stop();          try          {             /* Creates instance of the File class. */             File f = new File(path);             /* Performs deletion until the file is deleted. */             while(f.delete())             {                /* Delete the file. */                f.delete();              }             /* Hides the Download Status dialog box. */             this.setVisible(false);          }          catch(Exception e)          {             System.out.println("Error in I/O");          }                 }       else if(arg.equals(" OK "))       {          /* Hides the Download Status dialog box. */          this.setVisible(false);       }    }    } 
end example
 

Download this Listing .

In the above code, the ProgressTest() constructor creates the Download Status dialog box, as shown in Figure 3-3:

this figure shows the download status dialog box, which contains a progress bar that displays the download status.
Figure 3-3: The Download Status Dialog Box

If an end user clicks the Cancel button on the Download Status dialog box, the actionPerformed() method is invoked. This method stops the downloading thread. The actionPerformed() method then creates an instance of the File class and deletes the file that is being downloaded.




Java InstantCode. Developing Applications Using Java NIO
Java InstantCode. Developing Applications Using Java NIO
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 55

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net