Java Forms implementation method for dynamically loading disk files _java

Source: Internet
Author: User

When you use the graphical interface operating system, when you open a folder system, all files and subfolders under that folder are automatically listed. This example implements a similar feature: First let the user select a folder, the program dynamically lists all the files under that folder, and if the file is a hidden file, displays "hidden files" in the property bar. A progress bar is used to indicate whether or not to complete.

Ideas are as follows:

The first is the view layer. JTextField, JButton, JScrollPane, jtable god horse will not have to mention, are old pots friends, here because to use the progress bar, so there will be a new pot friend: JProgressBar control. Controls naturally want to create objects as member variables, in addition to file-type member variables to represent the selected folder.
This instance involves only one control event, and that is the Select Folder button. In its events, select folders and list information God Horse is a cliché, the key is the use of JProgressBar controls. After the user selects a folder, use the JProgressBar control's Setindeterminate (true) method to set the scroll bar to start scrolling, after the file information is displayed, and then through the setindeterminate of the JProgressBar control ( False) method to stop scrolling the scroll bar.
The code is as follows:

Copy Code code as follows:

Import Java.awt.BorderLayout;
Import Java.awt.EventQueue;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import Java.io.File;

Import Javax.swing.JButton;
Import Javax.swing.JFileChooser;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
Import Javax.swing.JProgressBar;
Import Javax.swing.JScrollPane;
Import javax.swing.JTable;
Import Javax.swing.JTextField;
Import Javax.swing.border.EmptyBorder;
Import Javax.swing.table.DefaultTableModel;
Import Javax.swing.UIManager;

public class Filelistframe extends JFrame {

/**
*
*/
Private static final long serialversionuid = -615665572894071265l;
Private JPanel ContentPane;
Private JTextField Choosetextfield;
Private JButton Choosebutton;
Private JScrollPane ScrollPane;
Private JTable table;
Private JProgressBar ProgressBar;
Private File Choosefile;

/**
* Launch the application.
*/
public static void Main (string[] args) {
try {
Uimanager.setlookandfeel ("Com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
catch (Throwable e) {
E.printstacktrace ();
}
Eventqueue.invokelater (New Runnable () {
public void Run () {
try {
Filelistframe frame = new Filelistframe ();
Frame.setvisible (TRUE);
catch (Exception e) {
E.printstacktrace ();
}
}
});
}

/**
* Create the frame.
*/
Public Filelistframe () {
Settitle ("\u7a97\u4f53\u52a8\u6001\u52a0\u8f7d\u78c1\u76d8\u6587\u4ef6");
Addwindowlistener (New Windowadapter () {
@Override
public void windowactivated (WindowEvent arg0) {
Do_this_windowactivated (arg0);
}
});
Setdefaultcloseoperation (Jframe.exit_on_close);
SetBounds (100, 100, 450, 300);
ContentPane = new JPanel ();
Contentpane.setborder (New Emptyborder (5, 5, 5, 5));
Setcontentpane (ContentPane);
Contentpane.setlayout (New BorderLayout (0, 0));

JPanel panel = new JPanel ();
Contentpane.add (panel, Borderlayout.north);

Choosetextfield = new JTextField ();
Panel.add (Choosetextfield);
Choosetextfield.setcolumns (13);

Choosebutton = new JButton ("\u9009\u62e9\u6587\u4ef6\u5939");
Choosebutton.addactionlistener (new ActionListener () {
public void actionperformed (ActionEvent arg0) {
Do_choosebutton_actionperformed (arg0);
}
});
Panel.add (Choosebutton);

ProgressBar = new JProgressBar ();
Panel.add (ProgressBar);

ScrollPane = new JScrollPane ();
Contentpane.add (ScrollPane, Borderlayout.center);

Table = new JTable ();
Scrollpane.setviewportview (table);
}

protected void do_choosebutton_actionperformed (ActionEvent arg0) {
JFileChooser filechooser = new JFileChooser ();
Filechooser.setfileselectionmode (jfilechooser.directories_only);
Filechooser.setmultiselectionenabled (FALSE);
int result = Filechooser.showopendialog (this);
if (result = = Jfilechooser.approve_option) {
Choosefile = Filechooser.getselectedfile ();//Get the folder selected by the user
Choosetextfield.settext (Choosefile.getabsolutepath ());//Display the folder selected by the user
Progressbar.setindeterminate (TRUE);//Set scroll bar to start scrolling
Final file[] Subfiles = Choosefile.listfiles ()//Get all Files (clips) in the folder selected by the user
Final DefaultTableModel model = (DefaultTableModel) Table.getmodel ();
Model.setrowcount (0);//Empty table
New Thread () {//Start a fresh
public void Run () {
for (int i = 0; i < subfiles.length i++) {//Traverse user-selected folder
if (Subfiles[i].isfile ()) {//To determine if it is a file
Object[] Property = new Object[3];
Property[0] = i + 1;//Save serial number
PROPERTY[1] = Subfiles[i].getname ()//Save file name
PROPERTY[2] = "";
if (Subfiles[i].ishidden ()) {//To determine if it is a hidden file
PROPERTY[2] = "Hidden file";
}
Model.addrow (property);//Add a record to the table
Table.setmodel (model);//Update table
}
try {
Thread.Sleep (100);/thread hibernate 0.1 seconds to implement dynamic loading
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
Progressbar.setindeterminate (false);/Stop progress bar scrolling
};
}.start ();

}
}

protected void do_this_windowactivated (WindowEvent arg0) {
DefaultTableModel model = (DefaultTableModel) Table.getmodel ();
Model.setcolumnidentifiers (new object[] {"Ordinal", "filename", "property"});
}

}

Effect as shown:

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.