Windows 资源管理器中修改文件名的话如果出错会弹出类似的提示, 用SWT实现了这个功能, 并编写了一个工具方法来完成此任务
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolTip;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.SWT;
/**
* 输入时验证文件名并显示提示的例子.
* @author BeanSoft@126.com
*
*/
public class NewComposite extends org.eclipse.swt.widgets.Composite {
private Text text1;
/**
* Auto-generated main method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void main(String[] args) {
showGUI();
}
/**
* Auto-generated method to display this
* org.eclipse.swt.widgets.Composite inside a new Shell.
*/
public static void showGUI() {
Display display = Display.getDefault();
Shell shell = new Shell(display);
NewComposite inst = new NewComposite(shell, SWT.NULL);
Point size = inst.getSize();
shell.setLayout(new FillLayout());
shell.setText("File name test");
shell.layout();
if(size.x == 0 && size.y == 0) {
inst.pack();
shell.pack();
} else {
Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
shell.setSize(shellBounds.width, shellBounds.height);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
public NewComposite(org.eclipse.swt.widgets.Composite parent, int style) {
super(parent, style);
initGUI();
addFileNameValidateToolTip(text1);
}
private void initGUI() {
try {
this.setLayout(new GridLayout());
this.setSize(255, 29);
{
text1 = new Text(this, SWT.BORDER);
GridData text1LData = new GridData();
text1LData.heightHint = 13;
text1LData.grabExcessHorizontalSpace = true;
text1LData.horizontalAlignment = GridData.FILL;
text1.setLayoutData(text1LData);
text1.setText("Please input a file name here");
}
this.layout();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Add file name validation to the text control, if the text's value
* contains invlid file name char(s), a tooltip will be displayed to notice
* the user. 添加文本控件文件名验证提示功能, 如果文件名无效, 就显示出错提示的 ToolTip.
*
* @author BeanSoft
* @version 1.0 2007-03-25
* @param text -
* the text that need to be validated
*/
public static void addFileNameValidateToolTip(final Text text) {
text.addKeyListener(new KeyListener() {
/**
* Display a tip about detail info of this picture when mouse hover
* on.
*/
final ToolTip errorInfoTip = new ToolTip(text.getShell(),
SWT.BALLOON | SWT.ICON_ERROR);
{
errorInfoTip.setText("Invalid file name");
errorInfoTip.setAutoHide(true);
}
public void keyPressed(KeyEvent e) {
}
/**
* When key released, will check for filename.
*/
public void keyReleased(KeyEvent e) {
if (!isValidFileName(text.getText())) {
errorInfoTip
.setMessage("The file name should not contain char(s) of below:/n// / : * ? /" < > |");
errorInfoTip.setVisible(true);
e.doit = false;
}
}
});
}
/**
* 检查文件名是否合法.文件名字不能包含字符//:*?"<>|
* @param fileName文件名,不包含路径
* @return boolean is valid file name
*/
public static boolean isValidFileName(String fileName)
{
boolean isValid = true;
String errChar = "///:*?/"<>|" //
if (fileName == null || fileName.length() == 0)
{
isValid = false;
}
else
{
for (int i = 0; i < errChar.length(); i++)
{
if (fileName.indexOf(errChar.charAt(i)) != -1)
{
isValid = false;
break;
}
}
}
return isValid;
}
}