实现:读取文件夹指定类型的文件,并显示到表格控件中。

应用:对于项目开发中的文件分类起到了抛砖引玉的作用。

效果图

124437433.jpg


实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package  com.lixiyu;
import  java.awt.BorderLayout;
import  java.awt.EventQueue;
import  java.awt.GridBagConstraints;
import  java.awt.GridBagLayout;
import  java.awt.Insets;
import  java.awt.event.ActionEvent;
import  java.awt.event.ActionListener;
import  java.io.File;
import  java.sql.Date;
import  javax.swing.JButton;
import  javax.swing.JFileChooser;
import  javax.swing.JFrame;
import  javax.swing.JLabel;
import  javax.swing.JPanel;
import  javax.swing.JScrollPane;
import  javax.swing.JTable;
import  javax.swing.JTextField;
import  javax.swing.border.EmptyBorder;
import  javax.swing.event.CaretEvent;
import  javax.swing.event.CaretListener;
import  javax.swing.table.DefaultTableModel;
public  class  ListCustomTypeFile  extends  JFrame {
     /**
      *
      */
     private  static  final  long  serialVersionUID = -6263975104443132420L;
     /**
      * 自定义扩展名过滤器
      *
      * @author DylanLee
      */
     private  final  class  CustomFilter  implements  java.io.FileFilter {
         @Override
         public  boolean  accept(File pathname) {
             // 获取用户设置的指定扩展名
             String extName = extNameField.getText();
             if  (extName ==  null  || extName.isEmpty())
                 return  false ;
             if  (!extName.startsWith( "." )) // 判断扩展名前缀
                 extName =  "."  + extName; // 完事扩展名前缀
             extName = extName.toLowerCase();
             // 判断扩展名与过滤文件名是否符合要求
             if  (pathname.getName().toLowerCase().endsWith(extName))
                 return  true ;
             return  false ;
         }
     }
                                  
     private  JPanel contentPane;
     private  JTextField extNameField;
     private  JTable table;
     private  File dir;
     private  JLabel label;
                                  
     /**
      * Launch the application.
      */
     public  static  void  main(String[] args) {
         EventQueue.invokeLater( new  Runnable() {
             public  void  run() {
                 try  {
                     ListCustomTypeFile frame =  new  ListCustomTypeFile();
                     frame.setVisible( true );
                 catch  (Exception e) {
                     e.printStackTrace();
                 }
             }
         });
     }
                                  
     /**
      * Create the frame.
      */
     public  ListCustomTypeFile() {
         setTitle( "\u663E\u793A\u6307\u5B9A\u7C7B\u578B\u7684\u6587\u4EF6" );
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setBounds( 100 100 450 300 );
         contentPane =  new  JPanel();
         contentPane.setBorder( new  EmptyBorder( 5 5 5 5 ));
         contentPane.setLayout( new  BorderLayout( 0 0 ));
         setContentPane(contentPane);
                                      
         JPanel panel =  new  JPanel();
         contentPane.add(panel, BorderLayout.NORTH);
         GridBagLayout gbl_panel =  new  GridBagLayout();
         gbl_panel.columnWidths =  new  int [] {  93 54 0  };
         gbl_panel.rowHeights =  new  int [] {  23 0 0  };
         gbl_panel.columnWeights =  new  double [] {  0.0 1.0 , Double.MIN_VALUE };
         gbl_panel.rowWeights =  new  double [] {  0.0 0.0 , Double.MIN_VALUE };
         panel.setLayout(gbl_panel);
                                      
         JButton button =  new  JButton( "\u9009\u62E9\u6587\u4EF6\u5939" );
         button.addActionListener( new  ActionListener() {
             public  void  actionPerformed(ActionEvent e) {
                 do_button_actionPerformed(e);
             }
         });
         GridBagConstraints gbc_button =  new  GridBagConstraints();
         gbc_button.anchor = GridBagConstraints.NORTH;
         gbc_button.gridx =  0 ;
         gbc_button.gridy =  0 ;
         panel.add(button, gbc_button);
                                      
         label =  new  JLabel( "\u6587\u4EF6\u5939" );
         GridBagConstraints gbc_label =  new  GridBagConstraints();
         gbc_label.fill = GridBagConstraints.HORIZONTAL;
         gbc_label.gridx =  1 ;
         gbc_label.gridy =  0 ;
         panel.add(label, gbc_label);
                                      
         JLabel label_1 =  new  JLabel(
                 "\u8F93\u5165\u6307\u5B9A\u6587\u4EF6\u6269\u5C55\u540D\u79F0\uFF1A" );
         GridBagConstraints gbc_label_1 =  new  GridBagConstraints();
         gbc_label_1.anchor = GridBagConstraints.EAST;
         gbc_label_1.insets =  new  Insets( 0 0 0 5 );
         gbc_label_1.gridx =  0 ;
         gbc_label_1.gridy =  1 ;
         panel.add(label_1, gbc_label_1);
                                      
         extNameField =  new  JTextField();
         extNameField.addCaretListener( new  CaretListener() {
             public  void  caretUpdate(CaretEvent e) {
                 do_extNameField_caretUpdate(e);
             }
         });
         extNameField.setText( ".gif" );
         GridBagConstraints gbc_extNameField =  new  GridBagConstraints();
         gbc_extNameField.insets =  new  Insets( 0 0 5 0 );
         gbc_extNameField.fill = GridBagConstraints.HORIZONTAL;
         gbc_extNameField.gridx =  1 ;
         gbc_extNameField.gridy =  1 ;
         panel.add(extNameField, gbc_extNameField);
         extNameField.setColumns( 10 );
                                      
         JScrollPane scrollPane =  new  JScrollPane();
         contentPane.add(scrollPane, BorderLayout.CENTER);
                                      
         table =  new  JTable();
         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
         table.setModel( new  DefaultTableModel( new  Object[][] {},  new  String[] {
                 "\u6587\u4EF6\u540D\u79F0" "\u6587\u4EF6\u5927\u5C0F" ,
                 "\u4FEE\u6539\u65E5\u671F"  }) {
             /**
                      *
                      */
                     private  static  final  long  serialVersionUID = 5274214559103654856L;
             boolean [] columnEditables =  new  boolean [] {  false false false  };
                                          
             public  boolean  isCellEditable( int  row,  int  column) {
                 return  columnEditables[column];
             }
         });
         table.getColumnModel().getColumn( 0 ).setPreferredWidth( 220 );
         table.getColumnModel().getColumn( 1 ).setPreferredWidth( 85 );
         table.getColumnModel().getColumn( 2 ).setPreferredWidth( 110 );
         scrollPane.setViewportView(table);
     }
                                  
     /**
      * 选择文件夹按钮的事件处理方法
      *
      * @param e
      */
     protected  void  do_button_actionPerformed(ActionEvent e) {
         JFileChooser chooser =  new  JFileChooser(); // 创建文件选择器
         // 设置选择器的过滤器
         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
         chooser.showDialog( this null );
         dir = chooser.getSelectedFile();
         getLabel().setText(dir.toString());
         // 获取过滤后的符合条件的文件数组
         listFiles();
     }
                                  
     /**
      * 显示文件夹中的文件
      */
     private  void  listFiles() {
         if  (dir ==  null )
             return ;
         // 获取符合条件的文件数组
         File[] files = dir.listFiles( new  CustomFilter());
         // 获取表格的数据模型
         DefaultTableModel model = (DefaultTableModel) table.getModel();
         model.setRowCount( 0 );
         for  (File file : files) { // 遍历文件数组
             // 创建表格行数据
             Object[] row = { file.getName(), file.length(),
                     new  Date(file.lastModified()) };
             model.addRow(row); // 添加行数据到表格模型
         }
     }
                                  
     protected  void  do_extNameField_caretUpdate(CaretEvent e) {
         listFiles();
     }
                                  
     protected  JLabel getLabel() {
         return  label;
     }
}

本文转自lixiyu 51CTO博客,原文链接:http://blog.51cto.com/lixiyu/1308355,如需转载请自行联系原作者