-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFileFilter.java
More file actions
106 lines (95 loc) · 2.27 KB
/
MyFileFilter.java
File metadata and controls
106 lines (95 loc) · 2.27 KB
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
package com.javatpoint;
import java.io.File;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
/***************************************************/
class FileFilterDemo extends JFrame
{
JLabel myLabel;
JButton myButton;
JFileChooser chooser;
FileFilterDemo()
{
super("File Filter Demo");
myLabel=new JLabel("No file is choosed yet");
myButton=new JButton("Choose file");
ActionListener listener=
new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
if (FileFilterDemo.this.chooser==null)
chooser=new JFileChooser();
chooser.addChoosableFileFilter(new MyFileFilter(".java","Java Source Files(*.java)"));
chooser.addChoosableFileFilter(new MyFileFilter(".txt","Text Files(*.txt)"));
//filter=new MyFilter(); then filter is equivalent to select all files
if(chooser.showDialog(FileFilterDemo.this,"Select this")==JFileChooser.APPROVE_OPTION)
FileFilterDemo.this.myLabel.setText(chooser.getSelectedFile().getPath());
}
};
myButton.addActionListener(listener);
add(myLabel,"Center");
add(myButton,"South");
setSize(300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
FileFilterDemo ffd=new FileFilterDemo();
ffd.setVisible(true);
}
}
/***************************************************/
public class MyFileFilter extends FileFilter
{
private String extension;
private String description;
////////////////
public MyFileFilter()
{
setExtension(null);
setDescription(null);
}
////////////////
public MyFileFilter(final String ext, final String desc)
{
setExtension(ext);
setDescription(desc);
}
////////////////
public boolean accept(File f)
{
final String filename=f.getName();
if( f.isDirectory() ||
extension==null ||
filename.toUpperCase()
.endsWith(extension.toUpperCase()))
return true;
return false;
}
////////////////
public String getDescription()
{
return description;
}
////////////////
public void setDescription(String desc)
{
if(desc==null)
description=new String("All Files(*.*)");
else
description=new String(desc);
}
////////////////
public void setExtension(String ext)
{
if(ext==null)
{extension=null; return;}
extension=new String(ext).toLowerCase();
if(!ext.startsWith("."))
extension="."+extension;
}
////////////////
}
/***************************************************/