Java 在SWT中更改TabItem列表时,TabItem不会刷新

Java 在SWT中更改TabItem列表时,TabItem不会刷新,java,swt,eclipse-rcp,Java,Swt,Eclipse Rcp,下面是我的代码,当按下按钮时,我正在使用item1更改item1列表。setcontrol取决于我的条件。 复合并没有刷新,但当我单击Item2选项卡并返回Item1选项卡时。。。列表是否更新取决于条件。 请让我知道如何在不移动到其他选项卡项的情况下刷新布局 final Composite RightComposite = new Composite(paComposite, SWT.NONE); RightComposite.setLayout(new GridLayout(1

下面是我的代码,当按下按钮时,我正在使用item1更改item1列表。setcontrol取决于我的条件。 复合并没有刷新,但当我单击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();

问题出在SWT的TabItem类的setControl()函数中。在该函数结束时,它使:oldControl.setVisible(false)

因此,在您的情况下,旧控件将与您设置的控件相同(如果您设置了两次),并且它将被隐藏。要解决此问题,可以将代码修改为:

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);
                }
            }
    }
});
我希望这符合你的需要