如何将滚动条添加到TabItem?

时间:2015-12-29 10:11:35

标签: java user-interface swt

我有以下方法,在其中创建一个TabFolder,其中包含两个标签(TabItem):

protected Control createContents(Composite parent) {
    Control control = super.createContents(parent);

    // 1. Create a TabFolder (dialogArea is a Control)
    TabFolder folder = new TabFolder((Composite) dialogArea, SWT.TOP);

    // 2. Create tab 1
    TabItem firstTab = new TabItem(folder, SWT.NONE);
    firstTab.setText("Tab One");
    firstTab.setControl(createMyFirstComposite(folder));

    // 3. Create tab 2
    TabItem secondTab = new TabItem(folder, SWT.NONE);
    secondTab.setText("Tab Two");
    secondTab.setControl(createMySecondComposite(folder));

    // TODO: Make the tab content scrollable

    return control;
}

由于标签的内容包含很多元素(在createMyFirstComposite(folder)createMySecondComposite(folder)中创建),我想向其中添加滚动条。

我该怎么做?

更新:尝试按照Greg的建议实施ScrolledComposite

protected Control createContents(Composite parent) {
    Control control = super.createContents(parent);

    TabFolder folder = new TabFolder((Composite) dialogArea, SWT.TOP);

    TabItem firstTab = new TabItem(folder, SWT.NONE);
    firstTab.setText("Tab One");

    ScrolledComposite sc = new ScrolledComposite(folder, SWT.V_SCROLL | SWT.H_SCROLL);
    sc.setExpandHorizontal(true);

    // createMyFirstComposite() returns composite with controlls
    Composite body = createMyFirstComposite(folder);

    sc.setContent(body);
    sc.setMinSize(body.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    firstTab.setControl(sc);

    // second tab...

    return control;
}

很遗憾,标签中没有内容。我忽略了什么吗?

1 个答案:

答案 0 :(得分:1)

使用ScrolledCompositeTabItem的控件。类似的东西:

TabItem firstTab = new TabItem(folder, SWT.NONE);

ScrolledComposite sc = new ScrolledComposite(folder, SWT.V_SCROLL | SWT.H_SCROLL);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);

Composite body = // TODO create all body controls of the tab

sc.setContent(body);
sc.setMinSize(body.computeSize(SWT.DEFAULT, SWT.DEFAULT));

firstTab.setControl(sc);