如何为SWT对话框合并和验证两个文本字段? [英] How to combine and validate two text fields for a swt dialog?
问题描述
我还有一个问题。我使用ModifyListener一个文本框来激活,并在SWT对话框停用OK-按钮。它的伟大工程。
i have another question. I use a ModifyListener for one textfield to activate and deactivate the OK-Button in a swt dialog. It works great.
现在我想添加一个ModifyListener另一个文本框。我想,如果在这两个文本字段是分一个字符的OK-按键仅被激活。
Now I want to add a ModifyListener for another textfield. I want that the OK-Button only is activated if in both text fields is min one char.
这是两个领域的code:
This is the code of the two fields:
descriptionText.addModifyListener(new ModifyListener(){
public void modifyText(ModifyEvent e) {
Text text = (Text) e.widget;
if (text.getText().length() == 0) {
getButton(IDialogConstants.OK_ID).setEnabled(false);
}
if (text.getText().length() >= 1) {
getButton(IDialogConstants.OK_ID).setEnabled(true);
}
}
});
}
第二个字段:
ccidText.addModifyListener(new ModifyListener(){
public void modifyText(ModifyEvent e) {
Text text = (Text) e.widget;
if (text.getText().length() == 0) {
getButton(IDialogConstants.OK_ID).setEnabled(false);
}
if (text.getText().length() >= 1){
getButton(IDialogConstants.OK_ID).setEnabled(true);
}
}
});
}
我知道这doesn't工作,因为有两个按键之间没有相关性。
我怎样才能结合起来?
I know that it doesn´t work because there are no dependencies between the two buttons. How can i combine it?
我想设置OK按钮假,而这两个modifylistener检测字符。
如果我在一个testfield删除所有字符的按钮必须重新激活。
I want to set the ok-button false while both modifylistener detect a char. If i delete all chars in one testfield the button must be deactivated again.
感谢ü。
推荐答案
您可以使用相同的监听器
为文本
字段,添加了 SWT.KeyUp
:
You can use the same Listener
for both Text
fields and add it for SWT.KeyUp
:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout(SWT.VERTICAL));
final Text first = new Text(shell, SWT.BORDER);
final Text second = new Text(shell, SWT.BORDER);
final Button button = new Button(shell, SWT.PUSH);
button.setText("disabled");
button.setEnabled(false);
Listener listener = new Listener()
{
@Override
public void handleEvent(Event e)
{
String firstString = first.getText();
String secondString = second.getText();
button.setEnabled(!isEmpty(firstString) && !isEmpty(secondString));
button.setText(button.isEnabled() ? "enabled" : "disabled");
}
};
first.addListener(SWT.KeyUp, listener);
second.addListener(SWT.KeyUp, listener);
shell.pack();
shell.setSize(300, shell.getSize().y);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static boolean isEmpty(String input)
{
if(input == null)
return true;
else
return input.trim().isEmpty();
}
是这样的:
在code基本上都会(在每个键中风)检查,如果这两个文本
是空的。如果是这样,禁用按钮
,否则启用它。
The code will basically (on each key stroke) check if both Text
s are empty. If so, disable the Button
, else enable it.
这篇关于如何为SWT对话框合并和验证两个文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!