2

选项卡的内容在应用程序加载时形成并显示。稍后,选项卡的内容可能会被其他操作更改。我想在每次操作后显示更新的内容。每次单击标签页时,内容都应该刷新/更新。但我失败了。

    //the content of the tab from the "reprintsTab" class
    //in the "reprintsTab" it query data from database and print out
    //later I update the data in the database from somewhere else, and I want the tab shows the new content
    //I want to click the tab sheet to reload the "reprintTab" class and print out the new content

    //here is what I did:

    public TabSheet sheet;

    //add tab and add the content from "reprintTab" into this tab
    sheet.addTab(new reprintsTab());

    //add the listener 
    sheet.addListener(new TabSheet.SelectedTabChangeListener() {

        @Override
        public void selectedTabChange(SelectedTabChangeEvent event) {

        //I know it does not work, because it only reload the class. but not put the content under the tab I want
        new reprintsTab();

        }
    });

我该怎么办?请帮助我,谢谢。

4

1 回答 1

3

您可以使用TabSheet.replaceComponent方法来执行此操作:

//Field to store current component
private reprintsTab currentComponent; 

//during initialization
currentComponent = new reprintsTab();
sheet.addTab(currentComponent);

sheet.addListener(new TabSheet.SelectedTabChangeListener() {
    @Override
    public void selectedTabChange(SelectedTabChangeEvent event) {
        reprintsTab newComponent = new reprintsTab();
        sheet.replaceComponent(currentComponent, newComponent);
        currentComponent = newComponent;
    }
});

此外,您可能希望仅在显示此选项卡时才重新加载此选项卡:

sheet.addListener(new TabSheet.SelectedTabChangeListener() {
        @Override
        public void selectedTabChange(SelectedTabChangeEvent event) {
            if (event.getTabSheet().getSelectedTab() == currentComponent) {
                //here goes the code
            }
        }
});

这应该对你有用,但我会建议一种更简洁的方法:实现reprintsTab为组件的容器,创建方法reloadbuildInterface方法来刷新它的状态,所以你可以调用:

currentComponent.reload();

当您需要更新界面时。

Also, I hope reprintsTab is just an example name, java class names starting with lowercase letter look ugly.

于 2013-03-13T07:56:55.043 回答