2
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");

I have an swt.TabFolder and there are some swt.TabItems on it. i want to have a close button with those TabItems so i may close the tabs i want on runtime. and i dont want to use CTabFolder or CTabItem

can someone tell me what can i do for this purpose?

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");
                }
            }

        });
}
Asad Ullah
  • 117
  • 1
  • 3
  • 11
  • Are you using Eclipse RCP? In that case there may be other alternatives. – Adam Arold Aug 07 '12 at 10:49
  • Ok, managed to get a workaround working (see my answer). However, the close "image" is on the left. If you want it easier and prettier, use `CTabItem`. – Baz Aug 07 '12 at 11:22

1 Answers1

6

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

Baz
  • 36,440
  • 11
  • 68
  • 94
  • @AsadUllah Well, what did not work? Is there an Exception or some error? You have to add more information. It works for me and for "Favonius" in your other question. – Baz Aug 29 '12 at 10:26
  • i accepted you question because i wanted reputation for adding image to another question sorry for misleading you sir – Asad Ullah Aug 29 '12 at 10:27
  • @AsadUllah Are you ignoring my previous comment? – Baz Aug 29 '12 at 10:28
  • i think the problem is that: i have another composite on this tab folder. and when i click on image, it does not perform action on tab folder but on that composite. – Asad Ullah Aug 29 '12 at 10:29
  • @AsadUllah I can't reproduce your problem. Maybe add your code to your question. – Baz Aug 29 '12 at 10:37
  • here sir, code is uploaded. please let me know what is my issue – Asad Ullah Aug 29 '12 at 11:22
  • @AsadUllah Edited the code in my answer. Try it in a new class. It contains your labels and texts and works as expected. – Baz Aug 29 '12 at 11:29
  • sir, it still did not get where i wanted action listener. i have a breakpoint inside the mouse Listener and during debugging it does not get to the breakpoint – Asad Ullah Aug 29 '12 at 12:29
  • @AsadUllah In my code or your code? If it's in yours, does it work with mine? – Baz Aug 29 '12 at 13:16
  • @AsadUllah Can you upload the code of your whole class to [PasteBin](http://pastebin.com/) and post the link here? – Baz Aug 29 '12 at 13:25
  • i have no access to pastebin, but i can email you if you dont mind – Asad Ullah Aug 29 '12 at 13:44
  • @AsadUllah I bet you can find some other website to paste your code to. – Baz Aug 29 '12 at 13:46
  • @AsadUllah Your code works for me. I have no idea, why it does not work for you... – Baz Aug 29 '12 at 14:00