java中文件浏览对话框中怎么过滤文件类型
1个回答
展开全部
代码如下:
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 李钟尉
*/
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("显示指定类型的文件");
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("选择文件夹");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
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 李钟尉
*/
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("显示指定类型的文件");
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("选择文件夹");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
do_button_actionPerformed(e);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询