SWT Tab Problem (on the fly creation /deletion)

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

SWT Tab Problem (on the fly creation /deletion)

Postby ChristianZimmer » Thu Mar 02, 2006 7:06 am


First let me describe our application:

We have a MainGUI Window with a treeviewer. Whenever the user double-clicks an tree item, a dialog is opened containing the relevant data for that item.

The Dialog is implemented as singleton with several tabs and remains open until the user explicitly closes it. So a refreshing mechanism has to update the data for every tab when the selected item changes.
(Plus the count of tabs is not constant for every item.)

Due to the fact, that for some items a certain tab has to be shown and for other items a certain tab is not needed, we are doing a check, every time the item changes.

If tab is needed that is actually disposed we're creating it, otherwise we're disposing the tab.

Is there a better approach to archieve that ?
How can we keep the order of our tabs ( if for example the 2nd tab is disposed and then created again, how do we archieve that the tab is displayed as second ) ?

Any help would be appreciated.

Thanks in advance!
Posts: 2
Joined: Thu Mar 02, 2006 6:42 am

Postby Kelly » Fri Mar 03, 2006 5:54 am

If I understand, you want to be able to dispose of the second tab in a tabfolder and then add a new tab back into that same position.

Use the TabItem(TabFolder parent, int style, int index)

The index argument controls where the tab will be placed in the folder.

I have tabs in my application that need updating also, but I only update the tab when the user clicks on the tab.

tabFolder.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
CTabItem cti = null;
int n = tabFolder.getSelectionIndex();
for (int i = 0; i < tabFolder.getItemCount(); i++) {
cti = tabFolder.getItem(i);
if (i == n) {
cti.setFont(SWTResourceManager.getFont("", 11, SWT.BOLD));
} else {
I use this to bold the tab text that they have clicked and also update the widget inside of the tab.

For the tab that is being shown, if that data needs to be updated ever so many seconds, I use the main while loop in the open method.

public void open(Display display) {
while (!isDisposed()) {
if (!display.readAndDispatch())

Making sure that I test to make sure that the widget exists each time I update it.

I have also used the Runable class. in the main method as another way to do this.

final int time = 30000; // believe thats 30 secs
final Runnable updater = new Runnable() {
public void run() {
if (samAtAGlance.isDisposed())
MonthDayYear mdy = monthViewer.getMonthDayYear();
if (mdy != null) {
lastMdy.month = mdy.month;
lastMdy.day = mdy.day;
lastMdy.year = mdy.year;
samAtAGlance.fillSchedule(mdy.month, mdy.day, mdy.year);
} else {
samAtAGlance.fillSchedule(lastMdy.month, lastMdy.day, lastMdy.year);
display.timerExec(time, this);

display.timerExec(time, updater);

while (!shell.isDisposed()) {
if (display.readAndDispatch() == false)

This is a code snippet which is missing some classes but you can work around this.

I am no expert in java so my way of doing this maybe completely wrong. :)
Posts: 38
Joined: Wed Feb 02, 2005 8:22 am

Postby ChristianZimmer » Fri Mar 03, 2006 6:33 am

Thanks a lot for your answer Kelly.

Let me detail my requirements:

We have a Dialog with a TabFolder with for example 4 Tabs.

Tab 1: "General"
Tab 2: "Planning"
Tab 3: "Contact Person"
Tab 4: "History"

Sometimes we don't have a plan for our item. So the second tab will not be shown.

Let's assume we have one item that has a plan (let's name it item A) and another that has none (item B).

    1. The user selects item A, the Dialog is shown and the 2nd tab is displayed.
    2. The Dialog remains open.
    3. Then the user selects item B, the second tab has to be disposed.
    4. The Dialog remains open.
    5. The User selects item A. The second tab must be displayed.

One code snippet for this:
Code: Select all
       if (planningTabItem==null) {
            splanningTabItem = new TabItem(tabFolder, SWT.NONE);
            planningTabItem.setToolTipText("Planning purposes");
            tab2 = new ItemComposite_TabPlanning(tabFolder, SWT.NONE, context);
        } else {
           if (tab2!=null) {
              tab2 = null;

My question is: Is there a better way to do this ? What would be best practice ?
Posts: 2
Joined: Thu Mar 02, 2006 6:42 am

Postby Kelly » Fri Mar 03, 2006 9:05 am

I believe I understand and what you have thier will work fine.

I would do it a different way in that I would loop through the TabItems comparing the the tab text to 'Planning' and when found would dispose of the TabItem widget.

Code: Select all
for (int i = 0; i < tabFolder.getItemCount(); i++) {
    TabItem ti = tabFolder.getItem(i);
     if (ti.getText().compare("Planning") == 0) {

Keeps global variables to a min and may simplify your code. This does not work if your tab text changes of course.

May also help if you turn on and off the redraw as you create or destroy the tab.

I am sure thier is maybe a better way and someone may tell us.
Posts: 38
Joined: Wed Feb 02, 2005 8:22 am

Return to SWT Designer

Who is online

Users browsing this forum: No registered users and 1 guest