filechooser

package streamandfile;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.filechooser.FileSystemView;
import java.io.*;
import javax.swing.table.DefaultTableModel;
import java.util.*;
public class MainFrame extends JFrame {
    JLabel lbl_dir = new JLabel();
    JTextField txt_fileName = new JTextField();
    JButton btn_open = new JButton(new ImageIcon(MainFrame.class.getResource(
            "tips.gif")));
    JTable table;
    DefaultTableModel model;
    JScrollPane table_pan;
    Vector head;

    public MainFrame() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        lbl_dir.setFont(new java.awt.Font("宋体", Font.BOLD, 17));
        lbl_dir.setText("文件或目录");
        lbl_dir.setBounds(new Rectangle(13, 27, 102, 29));
        txt_fileName.setFont(new java.awt.Font("宋体", Font.BOLD, 13));
        txt_fileName.setBounds(new Rectangle(106, 26, 234, 34));
        btn_open.setBounds(new Rectangle(347, 27, 39, 32));
        String[] title = {"Name", "isFile", "Size", "isRead", "isWrite",
                         "ModifyDate", "Hidden"};
        Vector v1 = new Vector();
        head = new Vector();
        for (int i = 0; i < title.length; i++) {
            head.add(i, title[i]);
        }
        model = new DefaultTableModel(v1, head);
        table = new JTable(model);
        table_pan = new JScrollPane();
        table_pan.setBounds(new Rectangle(4, 65, 391, 198));
        this.getContentPane().add(lbl_dir, null);
        this.getContentPane().add(txt_fileName);
        this.getContentPane().add(btn_open);
        this.getContentPane().add(table_pan);
        table_pan.getViewport().add(table);
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(400, 200);
        this.setLocation((screen.width - 400) / 2, (screen.height - 200) / 2);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        btn_open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                File file = openFile();
                if (file == null) {
                    JOptionPane.showMessageDialog(txt_fileName, "没有选择文件");
                } else {
                    try {
                        if(file!=null)
                        {
                            Vector v = new Vector();
                            table.remove(0);
                            table.updateUI();
                            txt_fileName.setText(file.toURL().toString());
                            v.add(0, file.getName());
                            v.add(1, new Boolean(file.isFile()));
                            v.add(2, new Long(file.length()));
                            v.add(3, new Boolean(file.canRead()));
                            v.add(4, new Boolean(file.canWrite()));
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTimeInMillis(new Long(file.lastModified()));
                            v.add(5, String.valueOf(calendar.getTime()));
                            v.add(6, new Boolean(file.isHidden()));
                            
                            if(table.getColumnCount()>1)
                            {
                                model.insertRow(0,v);
                                model.removeRow(1);
                                table.updateUI();
                            }else
                            {
                                model.addRow(v);
                                table.updateUI();
                            }
                        }
                    } catch (Exception ee) {

                    }
                }
            }

        });
    }

    private File openFile() {
        JFileChooser filechooser = new JFileChooser(FileSystemView.
                getFileSystemView());
        int bool = filechooser.showDialog(this, "打开文件");
        File file = null;
        if (bool != JFileChooser.ERROR_OPTION) {
            file = filechooser.getSelectedFile();
        }
        return file;
    }

    public static void main(String[] args) {
        MainFrame mainframe = new MainFrame();
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值