Set header-width of CTabItem

789 Views Asked by At

Can the width of the tab-header of a CTabItem be set arbitrary?

Thnx in advance!

Kind regard, Marcus

3

There are 3 best solutions below

1
On BEST ANSWER

No you can't do that. Not at least by any exposed API (read public methods). A possible solution is to extend the code of CTabFolder and CTabItem.

1
On

You can try to override CTabItem, but this solution doesn't look as nice solution (nevertheless I use it):

CTabItem tabItem;

final int tabWidth = 90;

tabItem = new CTabItem( tabFolder, SWT.NONE ) {
    @Override
    public Rectangle getBounds( ) {
        Rectangle bounds = super.getBounds();
        bounds.x = 0 * tabWidth;
        bounds.width = tabWidth;
        return bounds;
    }
};
tabItem.setText( __lang.str( this, "Tab Item I" ) );

tabItem = new CTabItem( tabFolder, SWT.NONE ) {
    @Override
    public Rectangle getBounds( ) {
        Rectangle bounds = super.getBounds();
        bounds.x = 1 * tabWidth;
        bounds.width = tabWidth;
        return bounds;
    }
};
tabItem.setText( __lang.str( this, "Tab Item II" ) );

tabItem = new CTabItem( tabFolder, SWT.NONE ) {
    @Override
    public Rectangle getBounds( ) {
        Rectangle bounds = super.getBounds();
        bounds.x = 2 * tabWidth;
        bounds.width = tabWidth;
        return bounds;
    }
};
tabItem.setText( __lang.str( this, "Tab Item III" ) );
0
On

As a workaround, you can pad the title with extra spaces using String.format like:

cTabItem.setText(String.format("%-15s", orgTitle));

This will add extra spaces to the end of the string if its length is less than 15 characters.

Check this for more details: https://docs.oracle.com/javase/tutorial/essential/io/formatting.html