When a graphical interface is used, the system automatically lists all files and subfolders in a folder when a folder is opened. This instance Implements similar functions: first, let the user select a folder, the program will dynamically list all the files in the folder; if the file is a hidden file, "Hide File" is displayed in the property bar ". The progress bar is used to indicate whether the process is complete.
The idea is as follows:
The Code 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. using wevent; 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 =-inline; private JPanel contentPane; private JTextField chooseTextField; private JButton chooseButton; private extends 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 \ Users \ u78C1 \ u76D8 \ u6587 \ u4EF6"); addWindowListener (new WindowAdapter () {@ Override public void windowActivated (invalid wevent arg0) {do_this_windowActivated (arg0) ;}}); setdefaclocloseoperation (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 actionreceivmed (ActionEvent arg0) {do_choosebutton_actionreceivmed (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_actionreceivmed (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 (); // obtain the chooseTextField folder selected by the user. setText (chooseFile. getAbsolutePath (); // display the user-selected folder progressBar. setIndeterminate (true); // set the scroll bar to start rolling final File [] subFiles = chooseFile. listFiles (); // obtain all the files (folders) in the selected folder final DefaultTableModel model = (DefaultTableModel) table. getModel (); model. setRowCount (0); // clear the table new Thread () {// start the new Thread public void run () {for (int I = 0; I <subFiles. length; I ++) {// traverse the selected folder if (subFiles [I]. isFile () {// determine whether the Object is a file [] 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 a record table to the table. setModel (model); // update table} try {Thread. sleep (100); // thread sleep for 0.1 seconds to achieve dynamic loading} catch (InterruptedException e) {e. printStackTrace () ;}} progressBar. setIndeterminate (false); // stop progress bar rolling };}. start () ;}} protected void do_this_windowActivated (protected wevent arg0) {DefaultTableModel model = (DefaultTableModel) table. getModel (); model. setColumnIdentifiers (new Object [] {"no.", "file name", "attribute "});}}
Effect