The Java implementation implements the progress bar using the progress bar of Java when copying files

  • 2020-04-01 03:01:24
  • OfStack

Thought analysis:

Because you want both an action panel and a progress bar, you definitely want two forms that inherit from the JFrame class.
Look at the called progress bar form first, it doesn't need to be done manually, so the internal implementation of the class is a method. Because file operations are designed, exceptions are caught. First according to the need to copy files to create File object, and according to copy the files after creating save address File object, then create a FileOutputStream object, and then create a FileInputStream object, after is ProgressMonitorInputStream object, and then read the File, if always takes more than 2 seconds, a progress monitor will automatically pop up window. Then define byte array, and then use the while loop read a file, use FileOutputStream class write () method through the stream to write data, then use FileOutputStream class close () method to close the output stream, finally use ProgressMonitorInputStream kind close () method to close the input stream. You can see that this method takes three parameters: the parent window that pops up, the address of the file to be copied, and the folder to be copied to.
The code is as follows:

ProgressMonitorTest. Java:


package cn.edu.xidian.crytoll;
import java.io.FileInputStream;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.ProgressMonitorInputStream;
public class ProgressMonitorTest {

    public void useProgressMonitor(JFrame frame, String copyPath, String newPath) {
        try {
            File file = new File(copyPath); //Create a File object based on the File to be copied
            File newFile = new File(newPath); //Create a File object based on the save address of the copied File
            FileOutputStream fop = new FileOutputStream(newFile); //Create the FileOutputStream object
            InputStream in = new FileInputStream(file);
            //Read the file, if the total time is more than 2 seconds, will automatically pop up a progress monitoring window.
            ProgressMonitorInputStream pm = new ProgressMonitorInputStream(
                    frame, " File reading, please wait ...", in);
            int c = 0;
            byte[] bytes = new byte[1024]; //Define byte array
            while ((c = pm.read(bytes)) != -1) { //Loop read file
                fop.write(bytes, 0, c); //Write data through a stream
            }
            fop.close(); //Close the output stream
            pm.close(); //Close the input stream
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

3. Now look at the main form. The JLabel, JTextField, and so on are the old buttons for selecting files and selecting folders. The most important is the "start copying" button, which will be responsible for popping up the progress bar. This requires a new thread, so the main form not only inherits the JFrame class, but also implements the Runnable interface. His uncle's.

4. At the beginning of the copy button on the specific methods, first create a Thread objects as a new Thread, then call the object's start () method, overloading implement the run () method, in this method to create a progress bar object, use the JTextField class getText () method for replicating to address and to copy a file path, and then call the progress bar method in the class.

The code is as follows:


package cn.edu.xidian.crytoll;
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;

public class UserMonitorFrame extends JFrame implements Runnable{

    
    private static final long serialVersionUID = 8674569541853793419L;
    private JPanel contentPane;
    private JTextField fileField;
    private JTextField searchTextField;
    private JTextField replaceTextField;
    private File file;
    private JTextField textField;
    private JTextField textField_1;

    
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UserMonitorFrame frame = new UserMonitorFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    
    public UserMonitorFrame() {
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 501, 184);
        setTitle(" Use a progress bar when reading a file ");
        getContentPane().setLayout(null);

        JLabel label = new JLabel("u6587u4EF6u5730u5740uFF1A");
        label.setBounds(10, 10, 70, 15);
        getContentPane().add(label);

        textField = new JTextField();
        textField.setBounds(90, 7, 300, 21);
        getContentPane().add(textField);
        textField.setColumns(10);

        JButton button = new JButton("u9009u62E9u6587u4EF6");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        button.setBounds(400, 6, 93, 23);
        getContentPane().add(button);

        JLabel label_1 = new JLabel("u590Du5236u5730u5740uFF1A");
        label_1.setBounds(10, 40, 70, 15);
        getContentPane().add(label_1);

        textField_1 = new JTextField();
        textField_1.setBounds(90, 38, 300, 21);
        getContentPane().add(textField_1);
        textField_1.setColumns(10);

        JButton button_1 = new JButton("u9009u62E9u5730u5740");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_1_actionPerformed(e);
            }
        });
        button_1.setBounds(400, 39, 93, 23);
        getContentPane().add(button_1);

        JButton button_2 = new JButton("u5F00u59CBu590Du5236");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_copyButton_actionPerformed(e);
            }
        });
        button_2.setBounds(182, 69, 93, 23);
        getContentPane().add(button_2);
    }
    protected void do_button_actionPerformed(ActionEvent e){
        JFileChooser chooser=new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        //Displays the file open dialog box
        int option = chooser.showOpenDialog(this);
        //Make sure the user presses the open button instead of the cancel button
        if (option != JFileChooser.APPROVE_OPTION)
            return;
        //Gets the file object selected by the user
        file = chooser.getSelectedFile();
        //Displays file information to the text box
        textField.setText(file.toString());
    }
    protected void do_button_1_actionPerformed(ActionEvent e){
        JFileChooser chooser=new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int option=chooser.showOpenDialog(this);
        if(option!=JFileChooser.APPROVE_OPTION)
            return;
        file=chooser.getSelectedFile();
        textField_1.setText(file.toString());
    }
  //Determines the copy button click event
    protected void do_copyButton_actionPerformed(ActionEvent arg0) {
       Thread thread = new Thread(this);
       thread.start();
    }
   //Application of multithreading technology to achieve the read operation
    @Override
    public void run() {
        ProgressMonitorTest test = new ProgressMonitorTest();
        String path = textField.getText();
        String save = textField_1.getText();
        test.useProgressMonitor(this,path,save+path.substring(path.lastIndexOf("."),path.length()));

    }
}


Related articles: