1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell();
shell.setLayout(new GridLayout());
final Image image = new Image(display, 16, 16);
final Color color = display.getSystemColor(SWT.COLOR_RED);
final GC gc = new GC(image);
gc.setBackground(color);
gc.fillRectangle(image.getBounds());
gc.dispose();
final ToolBar bar = new ToolBar(shell, SWT.BORDER);
bar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
ToolItem item;
for (int i = 1; i < 10; i++) {
if (i % 3 == 0) {
item = new ToolItem(bar, SWT.SEPARATOR);
} else {
item = new ToolItem(bar, SWT.PUSH);
}
item.setText("Item " + i);
item.setImage(image);
}
final Rectangle clientArea = shell.getClientArea();
bar.setLocation(clientArea.x, clientArea.y);
bar.pack();
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(final PaintEvent e) {
final int width = (int) ((float) e.width / (float) bar.getItemCount());
for (final ToolItem item : bar.getItems()) {
if ((item.getStyle() & SWT.SEPARATOR) == SWT.SEPARATOR) {
item.setWidth(width);
}
}
}
});
shell.open();
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
} |
Partager