简体   繁体   English

如何在一个CTabItem中添加多个项目?

[英]how to add more than one items in one CTabItem?

CTabItem tabItem1 = new CTabItem(newTabFolder, SWT.CLOSE);
tabItem1.setText("Tab 1");
Label lab2 = new Label(newTabFolder, 0);
lab2.setText("Hello World");
tabItem1.setControl(table);
tabItem1.setControl(lab2);

I am trying to have a CTabItem which has a table and some labels and textfields 我正在尝试使用具有表以及一些标签和文本字段的CTabItem

But the problem arising is that any item/widget is added by use of setControl(Control control) function. 但是出现的问题是通过使用setControl(Control control)函数添加了任何项目/小部件。 And I cannot pass more than two controls. 而且我不能通过两个以上的控件。 The code above adds a table first and then over writes it with the label, hence showing only one item at a time. 上面的代码首先添加了一个表,然后用标签将其覆盖,因此一次仅显示一个项目。

And I want both things at one time. 我一次想要两件事。

I have also tried to see if I might add things to an object of Control and then pass that control to setControl() function but I could not find any function by which I can add items/widgets to control please tell me how to do this. 我也尝试过查看是否可以将内容添加到Control的对象中,然后将该控件传递给setControl()函数,但是找不到任何可以添加控件的函数,请告诉我如何执行此操作。

final Composite compositeInTab = new Composite(newTabFolder, SWT.NONE);
        compositeInTab.setLayout(new FillLayout());
        table = new Table(compositeInTab, SWT.V_SCROLL);

        btn1.addMouseListener(new MouseAdapter()
        {
            public void mouseDown(MouseEvent e)
            {
                /*JUST CREATING A TABLE AND MANAGING IT*/
                String[] titles = {"System Code","Domain Name","Organizational Unit","Organization Name"};
                for(int i=0; i<titles.length; i++)
                {
                    TableColumn column = new TableColumn(table, SWT.CENTER, i);
                    column.setText(titles[i]);
                    column.setWidth(150);
                    column.setMoveable(true);
                    column.setResizable(true);
                }
                for(int i=0; i<50; i++)
                {
                    TableItem item = new TableItem(table, 0);
                    item.setText(0, ""+i);
                    item.setText(1, ""+i);
                    item.setText(2, ""+i);
                    item.setText(3, ""+i);
                }
                for (int i = 0; i < titles.length; i++) 
                {
                    table.getColumn(i).pack();
                }
                table.setHeaderVisible(true);
                table.setSize(table.computeSize(SWT.DEFAULT, 200));
                table.setLinesVisible(true);
                /*CREATING OF TABLE COMPLETE*/

                compositeForTabFolder.setLayout(new GridLayout());
                compositeForTabFolder.setBounds(280, 0, 500, 450);
                newTabFolder.setUnselectedCloseVisible(false);

                CTabItem tabItem1 = new CTabItem(newTabFolder, SWT.CLOSE);
                tabItem1.setText("Tab 1");
                Label lab2 = new Label(compositeInTab, 0);
                lab2.setText("Hello World");
                Label lab3 = new Label(compositeInTab, 0);
                lab3.setText("Bye Bye World");
                tabItem1.setControl(compositeInTab);

                newTabFolder.setBounds(0, 0, 500, 300);
            }
        });

Image 图片

You could use a Composite , then add everything you want to this Composite and afterwards use setControl(composite) . 您可以使用Composite ,然后将所需的所有内容添加到此Composite ,然后使用setControl(composite)

This way, you will only have to add ONE widget via setControl() but this widget can have multiple children. 这样,您只需要通过setControl()添加一个小部件,但是此小部件可以有多setControl()

There is a good example here . 有一个很好的例子在这里

Here is the sample code from this site (slightly modified): 这是此站点的示例代码(稍作修改):

public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 6; i++) {
      TabItem item = new TabItem(tabFolder, SWT.NONE);
      item.setText("TabItem " + i);
      item.setToolTipText("This is my tab" + i);

      Composite composite = new Composite(tabFolder, SWT.NONE);
      composite.setLayout(new FillLayout());
      new Button(composite, SWT.PUSH).setText("Button");
      new Text(composite, SWT.BORDER).setText("TextField");
      new Label(composite, SWT.NONE).setText("Label");

      Table table = new Table(composite, SWT.NONE);
      table.setHeaderVisible(true);

      new TableItem(table, SWT.NONE).setText("TableItem");

      item.setControl(composite);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM