LoginSignup
2
3

More than 5 years have passed since last update.

SWTでToolBarに画像(アイコン)のみのToolItemを追加したときに引っかかった事。

Last updated at Posted at 2014-11-18

SWTでGUIアプリケーション作成中にツールバーに関して引っかかったのでメモ

通常のテキストのボタンなら以下の様なコードでOKです。

Toolbar.java
ToolBar toolBar = new ToolBar(shell, SWT.NONE);
ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
toolItem.setText("開く");

せっかくなので画像にしてそれっぽくしようとします。

Toolbar.java
ToolBar toolBar = new ToolBar(shell, SWT.NONE);
ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
//toolItem.setText("開く");
toolItem.setImage(new Image(display,"icon.png");

これで、よくあるアプリケーションのようにアイコンのみのボタンができるはず。。。

Mac(OS X 10.10)では、意図したとおりになります。
しかし、Windows 7環境では、左右に大きなマージン付きの横長ボタンになってしまいます。

ToolItem.setWidth()もセパレーター用みたいなのでどうしたものかと試行錯誤してると、

Toolbar.java
ToolBar toolBar = new ToolBar(shell, SWT.NONE | SWT.RIGHT);
ToolItem toolItem = new ToolItem(toolBar, SWT.PUSH);
//toolItem.setText("開く");
toolItem.setImage(new Image(display,"icon.png");

ToolBarのスタイルにSWT.RIGHTを指定してあげるとうまくいきました。

ちなみに画像+テキストなボタンのときは普通にうまくいきます。

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3