分类:
2008-09-09 15:19:30
StringTokenizer st = new StringTokenizer(strName.substring(pos + 1,
strName.length()), "\\");
while (st.hasMoreTokens()) {
String strTemp = st.nextToken();
if (!isNormallyNameString(strTemp)) {
return false;
}
}
}
return true;
}
/**
*
方法说明:判断文件名是否合法
*
输入参数:strName 文件名
*
返回类型:符合语法规则的文件名
*/
public static boolean isNormallyNameString(String strName) {
int pos = strName.indexOf(":\\");
if (pos == -1) {
}
String strText = "\t\r\n\\/:*?\"<>|^___FCKpd___0quot;;
for (int i = 0; i < strName.length(); ++i) {
String ch = String.valueOf(strName.charAt(i));
if (strText.indexOf(ch) != -1) {
return false;
}
}
return true;
}
}
package com.browser;
import java.awt.Toolkit;
import java.io.File;
import java.util.StringTokenizer;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
/**
* 只能输入文件名的文本框
* @author dl
*/
public class JFileNameTextField extends javax.swing.JTextField{
public static void main(String[] args) {
// TODO Auto-generated method stub
//测试代码
/*JFrame frame = new JFrame("文本框的内容限制测试");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JFileNameTextField(true), BorderLayout.CENTER);
frame.add(new JNumTextField(1, 999), BorderLayout.NORTH);
frame.setSize(300, 150);
frame.setVisible(true);*/
}
/**
*
方法说明:初始化
*
输入参数:isPath 是否可以输入带目录的文件名
*
返回类型:
*/
public JFileNameTextField(boolean isPath) {
super();
FileNameDocument fd = (FileNameDocument)this.getDocument();
fd.setIsPath(isPath);
}
protected Document createDefaultModel() {
return new FileNameDocument(this);
}
}
/**
* 判断输入字符是否符合文件名规范的过滤实现
* @author dl
*/
class FileNameDocument extends PlainDocument {
private boolean isPath = false; /**是否可以输入带目录的文件名*/
private JTextField parent = null; /**PlainDocument所在的文本框*/
private static Toolkit toolkit = Toolkit.getDefaultToolkit();
public FileNameDocument(JTextField field) {
super();
this.parent = field;
}
public void setIsPath(boolean isPath) {
this.isPath = isPath;
}
public void insertString(int offset, String text, AttributeSet attributes)
throws BadLocationException {
//得到当前文本框的内容
String strValue = parent.getText();
strValue = strValue.substring(0, offset) + text
+ strValue.substring(offset, strValue.length());
//判断得到的文本是否符合文件名的语法规范
if (isPath) {
if (!isNormallyPathString(strValue)) {
toolkit.beep();
return;
}
} else {
if (!isNormallyNameString(strValue)) {
toolkit.beep();
return;
}
}
super.insertString(offset, text, attributes);
}
/**
*
方法说明:判断文件路径是否合法
*
输入参数:strName 文件路径
*
返回类型:符合语法规则的文件路径
*/
public static boolean isNormallyPathString(String strName) {
int pos = strName.indexOf(":");
if (strName.indexOf("") != -1)
return false;
if (pos == -1) {
StringTokenizer st = new StringTokenizer(strName, "\\");
while (st.hasMoreTokens()) {
String strTemp = st.nextToken();
if (!isNormallyNameString(strTemp)) {
return false;
}
}
} else {
String strPath = strName.substring(0, pos);
if (strPath.length() == 1) {
java.lang.Character fq = strPath.toLowerCase().charAt(0);
java.lang.Character fq1 = strName.toLowerCase().charAt(pos);
if (fq1 != ':')
return false;
if (strName.length() > pos + 1) {
java.lang.Character fq2 = strName.toLowerCase().charAt(
pos + 1);
if (fq2 != '\\')
return false;
}
[2]