Java implementation uses the progress bar (Java Implementation progress bar) when copying files _java

Source: Internet
Author: User
Tags gettext

Thinking Analysis:

Because you have to have both an operator panel and a progress bar, there are definitely two forms that inherit the JFrame class.
First look at the called Progress bar form, it does not require manual operation, so the internal implementation of a class method is OK. The exception is caught because of the design file operation. You first create a file object based on the files you want to copy, and create a file object based on the saved address of the copied file, then create the FileOutputStream object, and then create the FileInputStream object. The Progressmonitorinputstream object is then read, and a Progress Watch window will automatically pop up if the total time elapsed is longer than 2 seconds. Next you define the byte array, use the while loop to read the file, use the Write () method of the FileOutputStream class to write the data through the stream, and then use the close () method of the FileOutputStream class to turn off the output stream. Finally, the input stream is closed using the close () method of the Progressmonitorinputstream class. This method requires three parameters: pops up its parent window, the file address to copy, and the folder you want to copy to.
The code is as follows:

Progressmonitortest.java:

Copy Code code as follows:

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 files you want to copy
File NewFile = new file (NewPath); Create a file object based on the saved address of the copied files
FileOutputStream FOP = new FileOutputStream (newFile); Creating FileOutputStream Objects
InputStream in = new FileInputStream (file);
Read the file, if the total time elapsed more than 2 seconds, will automatically pop up a progress monitoring window.
Progressmonitorinputstream pm = new Progressmonitorinputstream (
Frame, "in file read, please later ...", 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 by streaming
}
Fop.close (); Turn off the output stream
Pm.close (); Close the input stream
catch (Exception ex) {
Ex.printstacktrace ();
}
}
}

3. Look at the main form again. JLabel, JTextField, needless to say, select files and select Folders The two buttons are commonplace, the most important is the "Start copying" button, it will be responsible for pop-up this progress bar, which needs to open up a new thread, so the main form not only to inherit the JFrame class, You also implement the Runnable interface. His uncle's.

4. In the beginning of the specific method of copying a button, first create a thread object as a new thread, then call the object's start () method, overload the Execute run () method, create a progress bar object in the method, and use the GetText () of the JTextField class. Method gets the address of the file to be copied and the path to copy to, and then calls the method in the progress bar class.

The code is as follows:

Copy Code code 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;

/**
* Launch the application.
*/
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 ();
}
}
});
}

/**
* Create the frame.
*/
Public Usermonitorframe () {
Setresizable (FALSE);
Setdefaultcloseoperation (Jframe.exit_on_close);
SetBounds (100, 100, 501, 184);
Settitle ("Use progress bar when reading files");
Getcontentpane (). setlayout (NULL);

JLabel label = new JLabel ("\u6587\u4ef6\u5730\u5740\uff1a");
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 ("\u9009\u62e9\u6587\u4ef6");
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 ("\u590d\u5236\u5730\u5740\uff1a");
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 ("\u9009\u62e9\u5730\u5740");
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 ("\u5f00\u59cb\u590d\u5236");
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);
Show File Open dialog box
int option = Chooser.showopendialog (this);
Determines that the user presses the Open button, not the Cancel button
if (option!= jfilechooser.approve_option)
Return
Gets the file object selected by the user
File = Chooser.getselectedfile ();
Display file information to a 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 ());
}
Determine the Copy button click event
protected void do_copybutton_actionperformed (ActionEvent arg0) {
Thread thread = new thread (this);
Thread.Start ();
}
Using multithreading technology to realize 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 ());

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.