[英]How to do validation of input of a SWT widget (Text)
How can add validation to an SWT
widget? 如何为SWT
小部件添加验证? Eg Text
? Text
?
I tried both of the following (found online): 我尝试了以下两种方法(在网上找到):
txtPort.addListener(SWT.Verify,new Listener() {
@Override
public void handleEvent(Event event) {
String port = ((Text)event.widget).getText();
try{
int portNum = Integer.valueOf(port);
if(portNum <0 || portNum > 65535){
event.doit = false;
}
}
catch(Exception ex){
event.doit = false;
}
}
});
Also: 也:
txtPort.addVerifyListener(new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
String port = ((Text)e.widget).getText();
try{
int portNum = Integer.valueOf(port);
if(portNum <0 || portNum > 65535){
e.doit = false;
}
}
catch(Exception ex){
e.doit = false;
}
}
});
If I add a character, the cursor stucks and I can not even delete it. 如果我添加一个字符,光标卡住了,我甚至无法删除它。
Even if I just delete everything the first time, the cursor also stucks and I can not write anything else. 即使我第一次删除所有内容,光标也会卡住,我无法写任何其他内容。
What am I messing up here? 我在这里搞砸了什么? How am I supposed to do the validation of a Text
? 我该如何进行Text
验证?
In this case I only want to accept a number serving as port. 在这种情况下,我只想接受一个作为端口的号码。
The VerifyListener
you are creating will be called before any text has actually been entered. 在实际输入任何文本之前,将调用您正在创建的VerifyListener
。 You a currently checking the text that has already been entered to see if the value is valid, but it will never be valid because no text has yet been entered. 您当前正在检查已输入的文本以查看该值是否有效,但它永远无效,因为尚未输入任何文本。
Try reading the value of e.text
to see if it's an integer and use the e.start
and e.end
properties along with the the getText()
you have now to see if the overall new value will be between 0 and 65535. 尝试读取e.text
的值以查看它是否为整数并使用e.start
和e.end
属性以及现在的getText()
来查看整体新值是否介于0和65535之间。
An easier solution may be to create a ModifyListener
that only enables a submit button when the Text widget's text contains a valid port number. 一个更简单的解决方案可能是创建一个ModifyListener
,它只在Text小部件的文本包含有效端口号时启用提交按钮。
You can try something similar to this: 你可以尝试类似的东西:
txtPort.addVerifyListener(new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
/* Notice how we combine the old and new below */
String currentText = ((Text)e.widget).getText();
String port = currentText.substring(0, e.start) + e.text + currentText.substring(e.end);
try{
int portNum = Integer.valueOf(port);
if(portNum <0 || portNum > 65535){
e.doit = false;
}
}
catch(NumberFormatException ex){
if(!port.equals(""))
e.doit = false;
}
}
});
I want to suggest JFace DataBinding. 我想建议JFace DataBinding。 You can easily add a ControlDecorationSupport
to your text field and give the user some indication about what´s going wrong. 您可以轻松地在文本字段中添加ControlDecorationSupport
,并向用户提供有关出错的指示。
/* with portText being your SWT text control */
IObservableValue textObservable = WidgetProperties.text().observe(portText);
UpdateValueStrategy strategy = new UpdateValueStrategy();
strategy.setBeforeSetValidator(new IValidator() {
@Override
public IStatus validate(Object value) {
Integer portNumber = null;
try {
portNumber = Integer.valueOf((String) value);
} catch (NumberFormatException e) {
return error(e.getMessage() + " is not a number");
}
if (portNumber < 0 || portNumber > 65535) {
return error("Number is out of range");
}
return ok();
}
});
/* with text being the port value in your model */
Binding binding = new DataBindingContext().bindValue(
textObservable,
PojoProperties.value(Model.class, "text").observe(this.model),
strategy, null);
ControlDecorationSupport.create(binding, SWT.TOP | SWT.LEFT);
(Simply) Text field input or enter only digit using VerifyEvent in java rcp/swt Eclipse (简单)使用java rcp / swt Eclipse中的VerifyEvent输入文本字段或仅输入数字
Text txt_mobile = new Text(grpBasicDetails, SWT.BORDER);
txt_mobile.addVerifyListener(new VerifyListener() {
public void verifyText(VerifyEvent e) {
///// Keycode : backspace==8 (Enable backspace)
if(!(e.character>='0'&&e.character<='9'||e.keyCode==8))
{
txt_mobile.setText("");
//OR
//e.doit=false;
}
btnCancel.setEnabled(true);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.