【问题标题】:how to add close button with swt.TabItem?如何使用 swt.TabItem 添加关闭按钮?
【发布时间】:2012-08-04 08:36:55
【问题描述】:
TabFolder tabFolder = new TabFolder(composite, SWT.CLOSE);      

TabItem tab1 = new TabItem(tabFolder, SWT.CLOSE);
tab1.setText("Tab 1");

TabItem tab2 = new TabItem(tabFolder, SWT.CLOSE);
tab2.setText("Tab 2");

我有一个 swt.TabFolder,上面有一些 swt.TabItems。 我想要一个带有这些 TabItems 的关闭按钮,所以我可以在运行时关闭我想要的选项卡。 我不想使用 CTabFolder 或 CTabItem

谁能告诉我为此我能做些什么?

public DomainUI(Composite composite, TabFolder newTabFolder, boolean comingFromSelf)
    {       
        boolean itemsDisposed = false;
        TabItem[] itemsOnTabFolder = newTabFolder.getItems();
        String[] namesOfItemsOnTabFolder = new String[itemsOnTabFolder.length];
        if(comingFromSelf) // checking when to dispose other tabs
        {
            if(itemsOnTabFolder.length != 0)
            {
                for(int i=0; i<itemsOnTabFolder.length; i++)
                {
                    namesOfItemsOnTabFolder[i] = itemsOnTabFolder[i].getText();
                    itemsOnTabFolder[i].dispose();
                }
                itemsDisposed = true;
            }
        }
        final Composite compositeInTab = new Composite(newTabFolder, SWT.NONE);
        compositeInTab.setLayout(null);

        // CREATIION OF LABELS AND TEXTFIELDS
        systemCodeLabel = new Label(compositeInTab, 0);
        systemCodeText = new Text(compositeInTab, SWT.BORDER);

        domainNameLabel = new Label(compositeInTab, 0);
        domainNameText = new Text(compositeInTab, SWT.BORDER);

        organizationalUnitLabel = new Label(compositeInTab, 0);
        organizationalUnitText = new Text(compositeInTab, SWT.BORDER);

        organizationNameLabel = new Label(compositeInTab, 0);
        organizationNameText = new Text(compositeInTab, SWT.BORDER);

        systemCodeLabel.setText("System Code");
        domainNameLabel.setText("Domain Name");
        organizationalUnitLabel.setText("Organizational Unit");
        organizationNameLabel.setText("Organization Name");
newTabFolder.addMouseListener(new MouseAdapter() 
        {
            public void mouseUp(MouseEvent arg0)
            {
                TabFolder curFolder = (TabFolder)arg0.widget;
                Point eventLocation = new Point(arg0.x, arg0.y);
                TabItem item = curFolder.getItem(eventLocation);
                if(item == null)
                    return;

                Image image = item.getImage();

                // check if click is on image
                if(        eventLocation.x >= item.getBounds().x + image.getBounds().x && eventLocation.x <= item.getBounds().x + image.getBounds().x + image.getBounds().width
                        && eventLocation.y >= item.getBounds().y + image.getBounds().y && eventLocation.y <= item.getBounds().y + image.getBounds().y + image.getBounds().height)
                {
                    System.out.println("Close tab");
                    item.dispose();
                }
                else
                {
                    System.out.println("Don't close tab");
                }
            }

        });
}

【问题讨论】:

  • 您在使用 Eclipse RCP 吗?在这种情况下,可能还有其他选择。
  • 好的,设法得到一个解决方法(见我的回答)。但是,关闭的“图像”在左侧。如果你想让它更简单更漂亮,请使用CTabItem

标签: java swt tabitem


【解决方案1】:

TabItem 没有此功能(它将忽略您使用的SWT.CLOSE 样式)。除了使用CTabItem 并使用样式SWT.CLOSE 之外,没有其他方法(我知道)。您还必须将 TabFolder 替换为 CTabFolder

请参阅this 页面或this 页面以获取一个很好的示例。

或者,如果您无法离开TabItem,您可以使用item.setImage(xImage);x 图像添加到每个选项卡,并将Listener 添加到文件夹,处理“关闭的东西”。当然,x 项目将在左侧,而不是右侧...

设法让它工作。只需将img/x.gif 替换为您的关闭图像(用于测试,您可以使用:display.getSystemImage(SWT.ICON_ERROR)):

public static void main(String[] args) {
    Display display = Display.getDefault();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final TabFolder folder = new TabFolder(shell, SWT.NONE);

    TabItem item = new TabItem(folder, SWT.NONE);
    item.setImage(Images.loadImage("img/x.gif"));
    item.setText("Text");

    TabItem item2 = new TabItem(folder, SWT.NONE);
    item2.setImage(Images.loadImage("img/x.gif"));
    item2.setText("Text2");

    folder.addMouseListener(new MouseListener() {

        @Override
        public void mouseUp(MouseEvent arg0) {
            TabFolder curFolder = (TabFolder)arg0.widget;
            Point eventLocation = new Point(arg0.x, arg0.y);
            TabItem item = curFolder.getItem(eventLocation);
            if(item == null)
                return;

            Image image = item.getImage();

            // check if click is on image
            if(        eventLocation.x >= item.getBounds().x + image.getBounds().x && eventLocation.x <= item.getBounds().x + image.getBounds().x + image.getBounds().width
                    && eventLocation.y >= item.getBounds().y + image.getBounds().y && eventLocation.y <= item.getBounds().y + image.getBounds().y + image.getBounds().height)
            {
                System.out.println("Close tab");
                item.dispose();
            }
            else
            {
                System.out.println("Don't close tab");
            }
        }

        @Override
        public void mouseDown(MouseEvent arg0) {
        }

        @Override
        public void mouseDoubleClick(MouseEvent arg0) {
        }
    });

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

结果如下:

关闭前:

关闭后:

【讨论】:

  • @AsadUllah 那么,什么没用?是否有异常或某些错误?您必须添加更多信息。它适用于我和您的其他问题中的“Favonius”。
  • 我接受了你的问题,因为我想要在另一个问题中添加图像的声誉,对不起,先生误导了你
  • @AsadUllah 你是否忽略了我之前的评论?
  • 我认为问题在于:我在此选项卡文件夹中有另一个复合材料。当我单击图像时,它不会对选项卡文件夹执行操作,而是在该合成上执行操作。
  • @AsadUllah 我无法重现您的问题。也许将您的代码添加到您的问题中。
猜你喜欢
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2012-07-28
  • 2013-02-25
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多