Thursday 1 December 2016

SWT: Group widget

Group widget is used to group SWT widgets. A group is surrounded by a border and may, optionally, contain a title. The position of widgets inside the group are relative to the group.

How to add group to the screen?
Group group1 = new Group(shell, SWT.BORDER);

How to place widgets inside a group?
The way how you place the widgets inside a shell, you can place other widgets inside the group.

Button button = new Button(group1, SWT.PUSH);
package swt_app;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static int xPosition = 30;
 private static int yPosition = 50;

 private static int shellWidth = 600;
 private static int shellHeight = 600;

 private static int groupWidth = 250;
 private static int groupHeight = 250;

 private static String[] countries = { "India", "Germany", "Canada", "Japan", "Sri Lanka", "Singpore", "Russia" };
 private static String[] hobbies = { "Trekking", "Movies", "Games", "Painting", "Singing", "Dancing" };

 private static void addWidgetsToShell(Display display, Shell shell) {

  Group group = new Group(shell, SWT.BORDER);
  group.setBounds(10, 10, groupWidth, groupHeight);
  group.setText("Personal Information");

  Combo countryCombo = new Combo(group, SWT.DROP_DOWN);
  countryCombo.setItems(countries);
  countryCombo.setBounds(xPosition, yPosition, 150, 100);
  countryCombo.setText("Select Country");

  yPosition += 50;

  Combo hobbyCombo = new Combo(group, SWT.DROP_DOWN);
  hobbyCombo.setItems(hobbies);
  hobbyCombo.setBounds(xPosition, yPosition, 150, 100);
  hobbyCombo.setText("Select hobbies");

  yPosition += 50;

  Button button1 = new Button(group, SWT.PUSH);
  button1.setText("Submit");
  button1.setLocation(xPosition + 100, yPosition);
  button1.setSize(100, 20);

 }

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

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





Previous                                                 Next                                                 Home

No comments:

Post a Comment