How to add a close button with swt.TabItem?
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 swt.TabFolder and it has swt.TabItems on it. I want to have a closed button with these TabItems, so I can close the tabs I want at runtime. and i dont want to use CTabFolder or CTabItem
can someone tell me what i can 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");
}
}
});
}
TabItem
does not have this feature (it will ignore the style SWT.CLOSE
you are using). There is no other way (I know) than to use CTabItem
and use style SWT.CLOSE
. You will need to replace TabFolder
with CTabFolder
.
See this page or this page for a good example.
Alternatively , if you can't go away from TabItem
, you can add an image x
to each tab using item.setImage(xImage);
and add Listener
to the folder, handling the "close". Of course the element x
will then be on the left and not on the right ...
Managed to make it work. Just replace with img/x.gif
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:
After closing: