how to add close button with swt.TabItem?

前端 未结 1 1109
情话喂你
情话喂你 2021-01-22 04:53
TabFolder tabFolder = new TabFolder(composite, SWT.CLOSE);      

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

TabItem tab2 = new TabItem(         


        
1条回答
  •  清酒与你
    2021-01-22 05:35

    The TabItem doesn't have this functionality (it will ignore the SWT.CLOSE style you use). There is no other way (I know of) than using CTabItem instead and use the style SWT.CLOSE. You will have to replace TabFolder with CTabFolder as well.

    See this page or this page for a good example.

    Alternatively, if you cannot step away from TabItem, you could add an x image to each tab by using item.setImage(xImage); and adding a Listener to the folder, handling the "closing stuff". Of course, the x item will then be on the left, instead of the right...

    Managed to get it working. Just replace the img/x.gif with your close image (for testing, you can use: 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();
        }
    }
    

    The result looks like this:

    Before closing:

    Before closing

    After closing:

    After closing

    0 讨论(0)
提交回复
热议问题