CoolBars
are just like tool bars, and these can be dynamically repositioned by the user.
You
can create cool bar using CoolBar instance.
CoolBar
coolBar = new CoolBar(shell, SWT.BORDER);
You
can add items to cool bar using CoolItem.
CoolItem
coolItem1 = new CoolItem(coolBar, SWT.NONE);
Button
button1 = new Button(coolBar, SWT.FLAT | SWT.BORDER);
button1.setText("Button");
button1.pack();
Point
size = button1.getSize();
coolItem1.setControl(button1);
coolItem1.setSize(coolItem1.computeSize(size.x,
size.y));
CoolBar
provides 'setLocked' method, to lock the coolbar. When a coolbar is locked, its
items cannot be repositioned. Whenever you click the button1, logic toggles the
locking behavior of cool bar.
button1.addSelectionListener(new
SelectionAdapter() {
public void
widgetSelected(SelectionEvent e) {
coolBar.setLocked(!coolBar.getLocked());
}
});
Following
is the complete working application.
package swt_app; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.CoolBar; import org.eclipse.swt.widgets.CoolItem; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.swt.widgets.ToolItem; public class Test { private static int shellWidth = 1000; private static int shellHeight = 700; private static void addWidgetsToShell(Display display, Shell shell) { CoolBar coolBar = new CoolBar(shell, SWT.BORDER); CoolItem coolItem1 = new CoolItem(coolBar, SWT.NONE); CoolItem coolItem2 = new CoolItem(coolBar, SWT.NONE); CoolItem coolItem3 = new CoolItem(coolBar, SWT.NONE); Button button1 = new Button(coolBar, SWT.FLAT | SWT.BORDER); button1.setText("Button"); button1.pack(); Button button2 = new Button(coolBar, SWT.PUSH); button2.setText("Another button"); button2.pack(); ToolBar tools = new ToolBar(coolBar, SWT.NONE); ToolItem b1 = new ToolItem(tools, SWT.FLAT); b1.setText("Tool"); ToolItem b2 = new ToolItem(tools, SWT.FLAT); b2.setText("Bar"); tools.pack(); Point size = button1.getSize(); coolItem1.setControl(button1); coolItem1.setSize(coolItem1.computeSize(size.x, size.y)); size = button2.getSize(); coolItem2.setControl(button2); coolItem2.setSize(coolItem2.computeSize(size.x, size.y)); size = tools.getSize(); coolItem3.setControl(tools); coolItem3.setSize(coolItem3.computeSize(size.x, size.y)); coolItem3.setMinimumSize(size); coolBar.setWrapIndices(new int[] { 2 }); coolBar.setSize(300, 120); button1.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { coolBar.setLocked(!coolBar.getLocked()); } }); } public static void main(String[] args) { /* Instantiate Display object, it represents SWT session */ Display display = new Display(); /* * Define Shell, it represent a window, You can add more than one shell * to Display */ Shell shell = new Shell(display); shell.setSize(shellWidth, shellHeight); shell.setText("SWT Tutorial"); addWidgetsToShell(display, shell); /* Open shell window */ shell.open(); /* * Run the event dispatching loop until an exit condition occurs, which * is typically when the main shell window is closed by the user. */ while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } /* Dispose the display */ display.dispose(); } }
No comments:
Post a Comment