如何在SWT中为一个TabItem设置scrollcomposite?

时间:2016-05-19 04:36:53

标签: java layout swt scrolledcomposite

我正在尝试以下代码为TabItem“item2”设置scrollcomposite。但无法获得滚动条。

这里我创建了两个tabItem,我需要为item2设置scrollcomposite / scrollbar而不是item1

Display display = new Display();
final Shell shell = new Shell(display);
final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
Rectangle clientArea = shell.getClientArea();
tabFolder.setLocation(clientArea.x, clientArea.y);
// First Tab Item
TabItem item = new TabItem(tabFolder, SWT.NONE);
item.setText("TabItem " + 1);
Composite comp = new Composite(tabFolder, SWT.NONE);
GridLayout gl = new GridLayout();
GridData wgd = new GridData(GridData.FILL_BOTH);
comp.setLayout(gl);
comp.setLayoutData(wgd);
Button button = new Button(comp, SWT.PUSH);
button.setText("Page " + 1);
Button button2 = new Button(comp, SWT.PUSH);
button2.setText("Page " + 1);
Button button3 = new Button(comp, SWT.PUSH);
button3.setText("Page " + 1);
Button button4 = new Button(comp, SWT.PUSH);
button4.setText("Page " + 1);
item.setControl(comp);

ScrolledComposite sc = new ScrolledComposite(tabFolder, SWT.BORDER
            | SWT.H_SCROLL | SWT.V_SCROLL);

// second tab item
TabItem item2 = new TabItem(tabFolder, SWT.NONE);
item2.setText("TabItem " + 1);
Composite comp2 = new Composite(tabFolder, SWT.NONE);
GridLayout gl2 = new GridLayout();
GridData wgd2 = new GridData(GridData.FILL_BOTH);
comp2.setLayout(gl2);
comp2.setLayoutData(wgd2);
Button buttonq = new Button(comp2, SWT.PUSH);
buttonq.setText("Page " + 1);
Button button2q = new Button(comp2, SWT.PUSH);
button2q.setText("Page " + 1);

sc.setContent(comp2);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinSize(comp2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
sc.setShowFocusedControl(true);
item2.setControl(comp2);

tabFolder.pack();
shell.pack();
shell.open();
while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
        display.sleep();
}
display.dispose();

当我添加以下代码时,tabItem2为空:

item2.setControl(comp2);

请帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

这里有几件事。

首先使用所有内容的布局。 tabFolder.setLocation导致混淆,请改为使用FillLayout

所以替换

Rectangle clientArea = shell.getClientArea();
tabFolder.setLocation(clientArea.x, clientArea.y);

shell.setLayout(new FillLayout());

其次,第二个标签的Composite必须归ScrolledComposite所有。

所以改变

Composite comp2 = new Composite(tabFolder, SWT.NONE);

Composite comp2 = new Composite(sc, SWT.NONE);

最后,ScrolledComposite必须是第二个标签的控件,因此请更改

item2.setControl(comp2);

item2.setControl(sc);