以下是我的代码,当按下按钮时我使用 item1.setcontrol 更改item1列表取决于我的条件。 复合不刷新,但当我点击Item2选项卡并返回Item1选项卡时...列表更新取决于条件。 请告诉我如何在不移动到其他标签项目的情况下刷新布局。
final Composite RightComposite = new Composite(paComposite, SWT.NONE);
RightComposite.setLayout(new GridLayout(1, false));
RightComposite.setLayoutData(new GridData(SWT.FILL, GridData.FILL, true, true));
Composite findComposite = new Composite(RightComposite, SWT.NONE);
findComposite.setLayout(new GridLayout(2, true));
findComposite.setLayoutData(new GridData(SWT.FILL, GridData.FILL, true, false));
txt = new Text(findComposite, SWT.BORDER | SWT.WRAP | SWT.SINGLE);
txt.setLayoutData(new GridData(SWT.FILL, GridData.FILL, true, false));
txt.addListener(SWT.Verify, new Listener()
{
@Override
public void handleEvent(final Event e)
{
newString = ((Text) e.widget).getText();
}
});
btn = new Button(findTCComposite, SWT.NONE);
btn.setLayoutData(new GridData(SWT.BEGINNING, GridData.BEGINNING, false, false));
btn.setText("Find button");
final TabFolder tabFolder = new TabFolder(RightComposite, SWT.NONE);
tabFolder.setLayoutData(new GridData(SWT.FILL, GridData.FILL, true, true));
final TabItem item1 = new TabItem(tabFolder, SWT.NONE);
item1.setText("Tab 1 ");
btn.addListener (SWT.Selection, new Listener()
{
public void handleEvent(Event e) {
if(!newString.isEmpty()){
item1.setControl(list1);
}
else
{
item1.setControl(list);
}
}
});
TabItem item2 = new TabItem(tabFolder, SWT.NONE);
Item2.setText("Tab 2");
RightComposite.layout();
答案 0 :(得分:2)
您的问题出在SWT的TabItem类中,在setControl()函数中。在这个函数的最后它创建:oldControl.setVisible(false);
因此,在您的情况下,oldControl将与您设置的控件相同(它设置了两次)并且它将被隐藏。要解决此问题,您可以将代码修改为:
btn.addListener (SWT.Selection, new Listener() {
public void handleEvent(Event e) {
if (!newString.isEmpty()) {
item1.setControl(list1);
list1.setVisible(true);
} else {
item1.setControl(list);
list.setVisible(true);
}
}
});
或另一种方法:
btn.addListener (SWT.Selection, new Listener() {
public void handleEvent(Event e) {
if (newString != null && !newString.isEmpty()) {
if (item1.getControl() != list1) {
item1.setControl(list1);
}
} else {
if (item1.getControl() != list) {
item1.setControl(list);
}
}
}
});
我希望这符合您的需求。