When using the graphical interface operating system, when opening a folder, the system will automatically list all files and subfolders under the folder. This example implements a similar function: first let the user select a folder, and the program will dynamically list all files under the folder; if the file is a hidden file, the "Hide File" will be displayed in the property bar. Use the progress bar to indicate whether it is completed.
The idea is as follows:
First is the view layer. There is no need to mention JTextField, JButton, JScrollPane, and JTable. They are all old friends. Because the progress bar is used here, there will be a new friend: the JProgressBar control. Controls naturally need to create objects as member variables, and in addition, there must be File-type member variables to represent the selected folder.
This instance only involves an event of one control, that is, the Select folder button. In its events, it is common to choose folders and list information. The key is the usage of the JProgressBar control. After the user selects a folder, use the setIndeterminate(true) method of the JProgressBar control to set the scroll bar to start scrolling; after the file information is displayed, the scroll bar is stopped by the setIndeterminate(false) method of the JProgressBar control to stop scrolling.
The code is as follows:
The code copy is 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 the scrollbar to start scrolling
final File[] subFiles = chooseFile.listFiles();// Get all files (folders) in the folder selected by the user
final DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);// Clear the table
new Thread() {// Start a new thread
public void run() {
for (int i = 0; i < subFiles.length; i++) {// traverse the folder selected by the user
if (subFiles[i].isFile()) {// Determine whether it is a file
Object[] property = new Object[3];
property[0] = i + 1;// Save the serial number
property[1] = subFiles[i].getName();// Save the file name
property[2] = "";
if (subFiles[i].isHidden()) {// Determine whether it is a hidden file
property[2] = "Hidden file";
}
model.addRow(property);// Add record to the table
table.setModel(model);// Update the table
}
try {
Thread.sleep(100);// Thread sleeps for 0.1 seconds to achieve dynamic loading
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressBar.setIndeterminate(false);// Stop the progress bar scrolling
};
}.start();
}
}
protected void do_this_windowActivated(WindowEvent arg0) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setColumnIdentifiers(new Object[] { "Serial Number", "File Name", "Properties" });
}
}
The effect is shown in the picture: