Java窗体动态加载磁盘文件的实现方法

在使用图形界面操作系统时,当打开一个文件夹系统会自动列出该文件夹下的所有文件及子文件夹。本实例实现了类似的功能:首先让用户选择一个文件夹,程序会动态列出该文件夹下的所有文件;如果该文件是隐藏文件,就在属性栏中显示“隐藏文件”。要用到进度条来表示是否完成。

思路如下:

首先是视图层。JTextField、JButton、JScrollPane、JTable神马的就不用再提了,都是老盆友了,在这里因为要用到进度条,所以会有一个新盆友:JProgressBar控件。控件们自然是要作为成员变量创建对象的,此外还要有File型成员变量来代表选中的文件夹。这个实例只涉及了一个控件的事件,那就是选择文件夹按钮。在它的事件里,选择文件夹及列出信息神马的都是老生常谈了,关键是JProgressBar控件的用法。在用户选择文件夹后,使用JProgressBar控件的setIndeterminate(true)方法设置滚动条开始滚动;在文件信息显示完毕后,再通过JProgressBar控件的setIndeterminate(false)方法停止滚动条的滚动。代码如下:

代码如下:
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();// 获得用户选择的文件夹            chooseTextField.setText(chooseFile.getAbsolutePath());// 显示用户选择的文件夹            progressBar.setIndeterminate(true);// 设置滚动条开始滚动            final File[] subFiles = chooseFile.listFiles();// 获得用户选择的文件夹中的所有文件(夹)            final DefaultTableModel model = (DefaultTableModel) table.getModel();            model.setRowCount(0);// 清空表格            new Thread() {// 开始新的线程                public void run() {                    for (int i = 0; i < subFiles.length; i++) {// 遍历用户选择的文件夹                        if (subFiles[i].isFile()) {// 判断是否是一个文件                            Object[] property = new Object[3];                            property[0] = i + 1;// 保存序号                            property[1] = subFiles[i].getName();// 保存文件名                            property[2] = "";                            if (subFiles[i].isHidden()) {// 判断是否是一个隐藏文件                                property[2] = "隐藏文件";                            }                            model.addRow(property);// 向表格中添加记录                            table.setModel(model);// 更新表格                        }                        try {                            Thread.sleep(100);// 线程休眠0.1秒实现动态加载                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                    progressBar.setIndeterminate(false);// 停止进度条滚动                };            }.start();        }    }    protected void do_this_windowActivated(WindowEvent arg0) {        DefaultTableModel model = (DefaultTableModel) table.getModel();        model.setColumnIdentifiers(new Object[] { "序号", "文件名", "属性" });    }}

效果如图:

以上是 Java窗体动态加载磁盘文件的实现方法 的全部内容, 来源链接: utcz.com/p/207353.html

回到顶部