Friday 13 January 2017

SWT: RowLayout

RowLayout arrange the widgets in a row. You can appy horizontal (or) vertical styles to layout. RowLayout provides additional fields to control the user interface.

Ex:
RowLayout rowLayout = new RowLayout();
rowLayout.pack = false;
rowLayout.wrap = true;
rowLayout.justify = true;
rowLayout.type = SWT.VERTICAL;
rowLayout.marginLeft = 5;
rowLayout.marginTop = 5;
rowLayout.marginRight = 5;
rowLayout.marginBottom = 5;
rowLayout.spacing = 0;

Pack field makes sure that all the fields are in same size,  and they will be placed as far to the left (or the top) as possible.

If you set wrap feature to true, long rows (Which are beyond window size) will wrap around to form two rows.

Following is the complete working application.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

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

  RowLayout rowLayout = new RowLayout();
  // rowLayout.pack = true;
  // rowLayout.wrap = true;
  // rowLayout.justify = true;
  // rowLayout.type = SWT.VERTICAL;
  // rowLayout.marginLeft = 5;
  // rowLayout.marginTop = 5;
  // rowLayout.marginRight = 5;
  // rowLayout.marginBottom = 5;
  // rowLayout.spacing = 0;

  shell.setLayout(rowLayout);

  Button b1 = new Button(shell, SWT.PUSH);
  b1.setText("File");
  Button b2 = new Button(shell, SWT.PUSH);
  b2.setText("Edit");
  Button b3 = new Button(shell, SWT.PUSH);
  b3.setText("Source");
  Button b4 = new Button(shell, SWT.PUSH);
  b4.setText("Refactor");
  Button b5 = new Button(shell, SWT.PUSH);
  b5.setText("Navigate");
  Button b6 = new Button(shell, SWT.PUSH);
  b6.setText("Search");
  Button b7 = new Button(shell, SWT.PUSH);
  b7.setText("Search");
  Button b8 = new Button(shell, SWT.PUSH);
  b8.setText("Search");
  Button b9 = new Button(shell, SWT.PUSH);
  b9.setText("Search");
  Button b10 = new Button(shell, SWT.PUSH);
  b10.setText("Search");

  shell.open();
 }

 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(700, 300);
  addWidgetsToShell(display, shell);

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

 }
}

Run above application, you can able to see following screens.
Try to minimize the window size, you can able to see screens like below.





Previous                                                 Next                                                 Home

No comments:

Post a Comment