Thursday, 12 January 2017

SWT: FillLayout

FillLayout arranges the swt widgets horizontally in a row (or) vertically in a column. It forces all the widgets to be in same size. Horizontal is the default layout.

Following statements define vertical layout.

FillLayout fillLayout = new FillLayout();
fillLayout.type = SWT.VERTICAL;

You can set the layout of a shell using setLayout method.
shell.setLayout(fillLayout);

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

public class Test {

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

  FillLayout fillLayout = new FillLayout();
  // fillLayout.type = SWT.VERTICAL;

  shell.setLayout(fillLayout);
  Label label0 = new Label(shell, SWT.NONE);
  label0.setText("File");
  Label label1 = new Label(shell, SWT.NONE);
  label1.setText("Edit");
  Label label2 = new Label(shell, SWT.NONE);
  label2.setText("Source");
  Label label3 = new Label(shell, SWT.NONE);
  label3.setText("Refactor");
  Label label4 = new Label(shell, SWT.NONE);
  label4.setText("Navigate");
  Label label5 = new Label(shell, SWT.NONE);
  label5.setText("Search");
  Label label6 = new Label(shell, SWT.NONE);
  label6.setText("Project");
  Label label7 = new Label(shell, SWT.NONE);
  label7.setText("Run");
  Label label8 = new Label(shell, SWT.NONE);
  label8.setText("Window");
  Label label9 = new Label(shell, SWT.NONE);
  label9.setText("Help");
  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(800, 500);
  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 screen.




Previous                                                 Next                                                 Home

No comments:

Post a Comment