Tuesday, 3 January 2017

SWT: ToolBars

You can define a tool bar using ToolBar widget.

ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL);
toolBar.setSize(shellWidth, 150);
toolBar.setLocation(toolBarLocationX, toolBarLocationY);

How to add an item to toolbar?
By using 'ToolItem' class, you can add an item to toolbar.

ToolItem newFile = new ToolItem(toolBar, SWT.PUSH);
newFile.setText("New");

You can apply any one of the following styles to ToolItem.

Style
Description
CHECK
Style constant for check box behavior
PUSH
Style constant for push button behavior
RADIO
Style constant for radio button behavior
SEPARATOR
Style constant for line separator behavior
DROP_DOWN
Style constant for drop down menu/list behavior

Following statements create check box.
ToolItem checkItem = new ToolItem(toolBar, SWT.CHECK);
checkItem.setText("Check");

Following statements create push button.
ToolItem newFile = new ToolItem(toolBar, SWT.PUSH);
newFile.setText("New");
                
Following statement creates a separator.
ToolItem sep = new ToolItem(toolBar, SWT.SEPARATOR);

Following statements create radio button.
ToolItem radioItem1 = new ToolItem(toolBar, SWT.RADIO);
radioItem1.setText("Radio 1");

Following statements create drop down.
ToolItem dropdown = new ToolItem(toolBar, SWT.DROP_DOWN);
dropdown.setText("Drop-down");

Following logic adds a menu to drop down box.

Menu menu = new Menu(shell, SWT.POP_UP);
MenuItem choice = new MenuItem(menu, SWT.PUSH);
choice.setText("Choices");
MenuItem options = new MenuItem(menu, SWT.PUSH);
options.setText("options");

dropdown.addListener(SWT.Selection, new Listener() {
         @Override
         public void handleEvent(Event event) {
                 if (event.detail == SWT.ARROW) {
                          Rectangle rect = dropdown.getBounds();
                          Point pt = new Point(rect.x, rect.y + rect.height);
                          pt = toolBar.toDisplay(pt);
                          menu.setLocation(pt.x, pt.y);
                          menu.setVisible(true);
                 }
         }
});


Following is the complete working application.
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
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 int toolBarLocationX = 10;
 private static int toolBarLocationY = 10;

 private static void addWidgetsToShell(Display display, Shell shell) {
  ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL);
  toolBar.setSize(shellWidth, 150);
  toolBar.setLocation(toolBarLocationX, toolBarLocationY);

  ToolItem newFile = new ToolItem(toolBar, SWT.PUSH);
  newFile.setText("New");

  ToolItem openFile = new ToolItem(toolBar, SWT.PUSH);
  openFile.setText("Open");

  ToolItem save = new ToolItem(toolBar, SWT.PUSH);
  save.setText("Save");

  ToolItem sep = new ToolItem(toolBar, SWT.SEPARATOR);

  ToolItem checkItem = new ToolItem(toolBar, SWT.CHECK);
  checkItem.setText("Check");

  ToolItem sep2 = new ToolItem(toolBar, SWT.SEPARATOR);

  ToolItem radioItem1 = new ToolItem(toolBar, SWT.RADIO);
  radioItem1.setText("Radio 1");
  ToolItem radioItem2 = new ToolItem(toolBar, SWT.RADIO);
  radioItem2.setText("Radio 2");

  ToolItem sep3 = new ToolItem(toolBar, SWT.SEPARATOR);

  ToolItem radioItem3 = new ToolItem(toolBar, SWT.RADIO);
  radioItem3.setText("Radio 3");
  ToolItem radioItem4 = new ToolItem(toolBar, SWT.RADIO);
  radioItem4.setText("Radio 4");

  ToolItem dropdown = new ToolItem(toolBar, SWT.DROP_DOWN);
  dropdown.setText("Drop-down");

  Menu menu = new Menu(shell, SWT.POP_UP);
  MenuItem choice = new MenuItem(menu, SWT.PUSH);
  choice.setText("Choices");
  MenuItem options = new MenuItem(menu, SWT.PUSH);
  options.setText("options");

  dropdown.addListener(SWT.Selection, new Listener() {
   @Override
   public void handleEvent(Event event) {
    if (event.detail == SWT.ARROW) {
     Rectangle rect = dropdown.getBounds();
     Point pt = new Point(rect.x, rect.y + rect.height);
     pt = toolBar.toDisplay(pt);
     menu.setLocation(pt.x, pt.y);
     menu.setVisible(true);
    }
   }
  });

 }

 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();

 }
}


How radio buttons grouped together?
ToolItem radioItem1 = new ToolItem(toolBar, SWT.RADIO);
radioItem1.setText("Radio 1");
ToolItem radioItem2 = new ToolItem(toolBar, SWT.RADIO);
radioItem2.setText("Radio 2");


In case of toolbars, radio button behavior is done automatically. This automatic behavior happens only if radio buttons that are adjacent to one another. If any other ToolItem occurred between the radio buttons, then radio buttons don’t behave like group.


Previous                                                 Next                                                 Home

No comments:

Post a Comment