Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Fill ToolItems in Vertical ToolBar to Fit Width (Windows only)
Fill ToolItems in Vertical ToolBar to Fit Width (Windows only) [message #446885] Sun, 05 December 2004 09:31
Marcelo Fernandez is currently offline Marcelo FernandezFriend
Messages: 10
Registered: July 2009
Junior Member
Hello, I'm posting this because I've made a 'dirty' hack to solution (at
least momentaneusly) the (previously reported) bug No.71207:

Add option to fill ToolItems in vertical ToolBar to fit width

"When you create a ToolBar with the vertical style, the ToolItems will not
span the entire width of the toolbar, creating a jagged and an unpolished
look (IMO). You can see this behavior in Eclipse itself if you dock the
perspective toolbar on the left. Can we please have an option to size the
toolitems all to the maximum width?" (This happends only in Windows,

Well, I realize that Windows adjusts the width of a ToolItem (inside a
Vertical ToolBar) depending on the length of the string's
toolItem.getText(). This approach I did fills with spaces ToolItem's text.
If this is useful for anybody, I'll be happy. :D

These are the two functions:

private void adjustButton(ToolItem toolItem, int width) {
final int THRESHOLD = 1; // 1 pix
boolean adjusted = false;
StringBuffer spaceBuffer = new StringBuffer(toolItem.getText());
boolean insertLeft = true;
while (!adjusted) {
if (isSameValue(THRESHOLD, toolItem.getWidth(), width))
adjusted = true;
else {
if (insertLeft)
spaceBuffer.insert(0,' ');
spaceBuffer.append(' ');

toolItem.setText(spaceBuffer.toString()); // This is to update the
toolItem.getWidth() call in the next loop
insertLeft = !insertLeft;
private boolean isSameValue(int threshold, int a, int b) {
if ((Math.abs(a - b)) < threshold)
return true;
return false;

And this is how to call the adjustButton() function, works only after the (because toolBar's field values are initialized).

class SomeClass {
private ToolBar toolBar = null;

public SomeWindow() {
Shell shell = new Shell();
initializeShell(); // Creates the toolBar variable;

if (System.getProperty("").toLowerCase().indexOf("windows ") >=0) {
for (int i=0; i < toolBar.getItemCount(); i++)
adjustButton(toolBar.getItems()[i], toolBar.getClientArea().width);

while (!shell.isDisposed())
if (!MainApp.appDisplay.readAndDispatch())

I know, this is a very ugly hack, but I'm a newbie with SWT (and with Java
too). Maybe somebody could help me improve this code, or to solution this
issue in a better way. :D
The last thing to make it perfect would be to run this adjustment every time
the shell is resized. I tried to call the for() loop inside a Resize event,
but the whole app hangs!!

Previous Topic:About the thread of swt !
Next Topic:Debugging
Goto Forum:

Current Time: Tue Jun 15 08:57:07 GMT 2021

Powered by FUDForum. Page generated in 0.01721 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top