SWT输入时验证并提示文件名非法的小例子

本文介绍了一个使用Eclipse SWT实现的文件名验证工具,该工具能够在用户输入非法字符时显示错误提示,确保文件名符合Windows资源管理器的要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Windows 资源管理器中修改文件名的话如果出错会弹出类似的提示, 用SWT实现了这个功能, 并编写了一个工具方法来完成此任务

https://i-blog.csdnimg.cn/blog_migrate/3e9514e22277d94346dc57a846b4097c.png

 

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;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值