eclipse 如何验证 SWT 小部件的输入(文本)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12736421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do validation of input of a SWT widget (Text)
提问by Cratylus
How can add validation to an SWT
widget? E.g. Text
?
I tried both of the following (found online):
如何向SWT
小部件添加验证?例如Text
?
我尝试了以下两种方法(在网上找到):
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
?
In this case I only want to accept a number serving as port.
如果我添加一个字符,光标会卡住,我什至无法删除它。
即使我第一次只是删除所有内容,光标也会卡住,我无法写任何其他内容。
我在这里搞什么鬼?我应该如何验证 a Text
?
在这种情况下,我只想接受一个数字作为端口。
回答by Mike K
The VerifyListener
you are creating will be called before any text has actually been entered. 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.
在VerifyListener
实际上已输入的任何文字之前要创建将被调用。您当前正在检查已输入的文本以查看该值是否有效,但它永远不会有效,因为尚未输入任何文本。
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;
}
}
});
回答by Alexander Hansen
I want to suggest JFace DataBinding. You can easily add a ControlDecorationSupport
to your text field and give the user some indication about what′s going wrong.
我想建议 JFace DataBinding。您可以轻松地将 a 添加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);
回答by Chetan Bhagat
(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);
}
});