我有一个包含有列的表的复合。这些列具有特定的验证,如果字段值没有通过验证,则将其着色为红色,并设置错误消息。我希望有一个侦听器来检查这些列是否设置了任何错误消息。我向复合中添加了一个侦听器,但它从未被调用。我认为,每当我单击从复合到其他组合或在shell中的其他地方时,都应该调用focusLost()方法。但它从来没有被称为。
myComposite = new MyComposite(myGroup, myEditor, getModel(), SWT.NONE,
calculator, this, application, modelService, partService, progressService, shell);
myComposite.setLayout(new GridLayout(2, false));
GridDataFactory.swtDefaults().span(21, 1).grab(true, true).align(SWT.FILL, SWT.TOP).applyTo(myComposite);
FocusListener focusListener = new FocusListener() {
@Override
public void focusLost(FocusEvent e)
{
if (myComposite.getErrorMessage() != null) {
getValidator().getDecorator().setError(myComposite, myComposite.getErrorMessage());
}
}
@Override
public void focusGained(FocusEvent e) { /** nothing */ }
};
myComposite.addFocusListener(focusListener);发布于 2022-07-28 16:55:24
虽然这个问题很老,但我会尝试提出什么可能出了问题。
我在这个问题中没有足够的代码来准确地确定原因,但常见的情况是复合的子元素有焦点,而不是父元素。这就是为什么这个外部组合可能不会产生焦点事件--它根本不会在事件周期中运行。因此,我可以建议在内部元素上使用焦点放松检查,例如:
entireElement.addListener(SWT.FocusOut, event -> {
// put element validation mechanics here
});https://stackoverflow.com/questions/65306147
复制相似问题