i wanna verify input in Text.make sure the value is between 0 and 100. but because of some reason i can't use VerifyEvent
to do it. as follows, i want to get "check" value in public void handleEvent(Event event)
how can i do it
我想验证Text.make中的输入确定值介于0和100之间。但由于某种原因我无法使用VerifyEvent来执行此操作。如下,我想在public void handleEvent(事件事件)中得到“check”值我该怎么做
control.addListener (SWT.Verify, new Listener () {
public void handleEvent(VerifyEvent e) {
// TODO Auto-generated method stub
Text text = (Text)e.getSource();
String t = text.getText();
String check = t.substring(0, e.start) + e.text + t.substring(e.end);
e.doit = validateInput(check);
}
@Override
public void handleEvent(Event event) {
// TODO Auto-generated method stub
}
});
1 个解决方案
#1
0
Ok, so you want to add a SWT.Verify
, but you cannot use a VerifyListener
with VerifyEvent
and consequently, cannot access event.getSource()
. However, since you add the Listener
manually, you know the source:
好的,所以你想要添加一个SWT.Verify,但你不能将VerifyListener与VerifyEvent一起使用,因此无法访问event.getSource()。但是,由于您手动添加了监听器,因此您知道源:
final Text textField = new Text(shell, SWT.BORDER);
textField.addListener(SWT.Verify, new Listener() {
@Override
public void handleEvent(Event e) {
// You can access textField, since it is final
final String oldS = textField.getText();
String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
boolean isValid = true;
if("".equals(newS))
{
}
else
{
try
{
// first check, if text really is a number
float value = Float.parseFloat(newS);
// then check if it's between 0 and 100
if(value < 0 || 100 < value)
isValid = false;
}
catch(NumberFormatException ex)
{
isValid = false;
}
}
// if text is not a valid number, prevent user input
if(!isValid)
e.doit = false;
}
});
So you add the Listener
to textField
. Hence, you don't need the getSource()
method, because textField
is the source...
因此,您将监听器添加到textField。因此,您不需要getSource()方法,因为textField是源...
#1
0
Ok, so you want to add a SWT.Verify
, but you cannot use a VerifyListener
with VerifyEvent
and consequently, cannot access event.getSource()
. However, since you add the Listener
manually, you know the source:
好的,所以你想要添加一个SWT.Verify,但你不能将VerifyListener与VerifyEvent一起使用,因此无法访问event.getSource()。但是,由于您手动添加了监听器,因此您知道源:
final Text textField = new Text(shell, SWT.BORDER);
textField.addListener(SWT.Verify, new Listener() {
@Override
public void handleEvent(Event e) {
// You can access textField, since it is final
final String oldS = textField.getText();
String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end);
boolean isValid = true;
if("".equals(newS))
{
}
else
{
try
{
// first check, if text really is a number
float value = Float.parseFloat(newS);
// then check if it's between 0 and 100
if(value < 0 || 100 < value)
isValid = false;
}
catch(NumberFormatException ex)
{
isValid = false;
}
}
// if text is not a valid number, prevent user input
if(!isValid)
e.doit = false;
}
});
So you add the Listener
to textField
. Hence, you don't need the getSource()
method, because textField
is the source...
因此,您将监听器添加到textField。因此,您不需要getSource()方法,因为textField是源...