Java Tutorial/SWT/GridLayout — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 15:20, 31 мая 2010
Содержание
- 1 A Sample GUI Using GridLayouts
- 2 Enable more than one control to grab excess vertical space
- 3 Exclude a widget from a GridLayout
- 4 Grabbing Excess Space: grabExcessHorizontalSpace and grabExcessVerticalSpace
- 5 GridLayout 2x2
- 6 GridLayout Column 2
- 7 GridLayout Complex
- 8 GridLayout Fill Both
- 9 GridLayout: HORIZONTAL ALIGN Begin Fill
- 10 GridLayout: horizontalSpacing, verticalSpacing
- 11 GridLayout: insert widgets into a grid layout
- 12 GridLayout: makeColumnsEqualWidth
- 13 GridLayout: marginHeight, marginWidth
- 14 GridLayout: numColumns
- 15 GridLayout snippet: align widgets in a GridLayout
- 16 Horizontal Indentation
- 17 Horizontal Span and Vertical Span
- 18 Make more than one control to grab excess space in the same direction
- 19 Size Hints: widthHint and heightHint
- 20 The verticalAlignment specifies the vertical positioning.
- 21 Use GridLayout to layout a dialog
- 22 Using GridData Objects
- 23 Using GridLayout
- 24 Using GridLayout with all default values
A Sample GUI Using GridLayouts
- HORIZONTAL_ALIGN_BEGINNING, HORIZONTAL_ALIGN_CENTER, HORIZONTAL_ALIGN_END, HORIZONTAL_ALIGN_FILL: Set the horizontalAlignment field to value BEGINNING, CENTER, END, and FILL, respectively.
- VERTICAL_ALIGN_BEGINNING, VERTICAL_ALIGN_CENTER, VERTICAL_ALIGN_END, VERTICAL_ALIGN_FILL: Set the verticalAlignment field to value BEGINNING, CENTER, END, and FILL, respectively.
- GRAB_HORIZONTAL: Sets the grabExcessHorizontalSpace field to true.
- GRAB_VERTICAL: Sets the grabExcessVerticalSpace field to true.
- FILL_HORIZONTAL: Sets the horizontalAlignment field to FILL and grabExcessHorizontalSpace to true.
- FILL_VERTICAL: Sets the verticalAlignment field to FILL and grabExcessVerticalSpace to true.
- FILL_BOTH: Sets the horizontalAlignment field and the verticalAlignment field to FILL and the grabExcessHorizontalSpace field and the grabExcessVerticalSpace field to true.
(Taken from Professional Java Interfaces with SWT/JFace by Jackwind Li Guojie John Wiley & Sons 2005)
/*
All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
Created on 2004-5-2 18:55:02 by JACK $Id$
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class GridLayoutSimpleDmo {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout(4, false);
gridLayout.verticalSpacing = 8;
shell.setLayout(gridLayout);
Label label = new Label(shell, SWT.NULL);
label.setText("Title: ");
Text title = new Text(shell, SWT.SINGLE | SWT.BORDER);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 3;
title.setLayoutData(gridData);
label = new Label(shell, SWT.NULL);
label.setText("Author(s): ");
Text authors = new Text(shell, SWT.SINGLE | SWT.BORDER);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 3;
authors.setLayoutData(gridData);
label = new Label(shell, SWT.NULL);
label.setText("Cover: ");
gridData = new GridData();
gridData.verticalSpan = 3;
label.setLayoutData(gridData);
CLabel cover = new CLabel(shell, SWT.NULL);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 1;
gridData.verticalSpan = 3;
gridData.heightHint = 100;
gridData.widthHint = 100;
cover.setLayoutData(gridData);
label = new Label(shell, SWT.NULL);
label.setText("Pages");
Text pages = new Text(shell, SWT.SINGLE | SWT.BORDER);
pages.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
label = new Label(shell, SWT.NULL);
label.setText("Publisher");
Text publisher = new Text(shell, SWT.SINGLE | SWT.BORDER);
publisher.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
label = new Label(shell, SWT.NULL);
label.setText("Rating");
Combo rating = new Combo(shell, SWT.READ_ONLY);
rating.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
rating.add("5");
rating.add("4");
rating.add("3");
rating.add("2");
rating.add("1");
label = new Label(shell, SWT.NULL);
label.setText("Abstract:");
Text bookAbstract = new Text(shell, SWT.WRAP | SWT.MULTI | SWT.BORDER | SWT.H_SCROLL
| SWT.V_SCROLL);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
gridData.horizontalSpan = 3;
gridData.grabExcessVerticalSpace = true;
bookAbstract.setLayoutData(gridData);
Button enter = new Button(shell, SWT.PUSH);
enter.setText("Enter");
gridData = new GridData();
gridData.horizontalSpan = 4;
gridData.horizontalAlignment = GridData.END;
enter.setLayoutData(gridData);
title.setText("Professional Java Interfaces with SWT/JFace");
authors.setText("Jack Li Guojie");
pages.setText("500pp");
publisher.setText("John Wiley & Sons");
cover.setBackground(new Image(display, "yourFile.gif"));
bookAbstract.setText("SWT/JFace. ");
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Enable more than one control to grab excess vertical space
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutGrabExcessVertical {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1"); // Default alignment
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
list.setLayoutData(gridData);
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
GridData gridData2 = new GridData();
gridData2.grabExcessVerticalSpace = true;
gridData2.verticalAlignment = GridData.FILL;
button2.setLayoutData(gridData2);
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Exclude a widget from a GridLayout
/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: IBM Corporation - initial API and implementation
******************************************************************************/
// package org.eclipse.swt.snippets;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
/*
* Exclude a widget from a GridLayout
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.1
*/
public class GridLayoutWidgetExclude {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(3, false));
Button b = new Button(shell, SWT.PUSH);
b.setText("Button 0");
final Button bHidden = new Button(shell, SWT.PUSH);
bHidden.setText("Button 1");
GridData data = new GridData();
data.exclude = true;
data.horizontalSpan = 2;
data.horizontalAlignment = SWT.FILL;
bHidden.setLayoutData(data);
b = new Button(shell, SWT.PUSH);
b.setText("Button 2");
b = new Button(shell, SWT.PUSH);
b.setText("Button 3");
b = new Button(shell, SWT.PUSH);
b.setText("Button 4");
b = new Button(shell, SWT.CHECK);
b.setText("hide");
b.setSelection(true);
b.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
Button b = (Button) e.widget;
GridData data = (GridData) bHidden.getLayoutData();
data.exclude = b.getSelection();
bHidden.setVisible(!data.exclude);
shell.layout(false);
}
});
shell.setSize(400, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Grabbing Excess Space: grabExcessHorizontalSpace and grabExcessVerticalSpace
- Grow as their parent composite grows.
- Shrink as their parent composite shrinks.
- Usually, you enable these properties with controls such as Text, List, Tree, Table, or Canvas.
- The default value of each of the two properties is false.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutGrabExpressionSpace {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1"); // Default alignment
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
list.setLayoutData(gridData);
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
button2.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_END));
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
GridLayout 2x2
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GridLayout2x2 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.makeColumnsEqualWidth = true;
shell.setLayout(layout);
GridData data = new GridData(GridData.FILL_BOTH);
Button one = new Button(shell, SWT.PUSH);
one.setText("one");
one.setLayoutData(data);
data = new GridData(GridData.FILL_BOTH);
Button two = new Button(shell, SWT.PUSH);
two.setText("two");
two.setLayoutData(data);
data = new GridData(GridData.FILL_BOTH);
Button three = new Button(shell, SWT.PUSH);
three.setText("three");
three.setLayoutData(data);
data = new GridData(GridData.FILL_BOTH);
Button four = new Button(shell, SWT.PUSH);
four.setText("four");
four.setLayoutData(data);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
GridLayout Column 2
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutColumnNumber2 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
shell.setLayout(layout);
new Button(shell, SWT.PUSH).setText("one");
new Button(shell, SWT.PUSH).setText("two");
new Button(shell, SWT.PUSH).setText("three");
new Button(shell, SWT.PUSH).setText("four");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
GridLayout Complex
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutComplex {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
layout.makeColumnsEqualWidth = true;
shell.setLayout(layout);
// Create the big button in the upper left
GridData data = new GridData(GridData.FILL_BOTH);
data.widthHint = 200;
Button one = new Button(shell, SWT.PUSH);
one.setText("one");
one.setLayoutData(data);
// Create a composite to hold the three buttons in the upper right
Composite composite = new Composite(shell, SWT.NONE);
data = new GridData(GridData.FILL_BOTH);
data.horizontalSpan = 2;
composite.setLayoutData(data);
layout = new GridLayout();
layout.numColumns = 1;
layout.marginHeight = 15;
composite.setLayout(layout);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
GridLayout Fill Both
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutFillBoth {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
layout.makeColumnsEqualWidth = true;
shell.setLayout(layout);
// Create the big button in the upper left
GridData data = new GridData(GridData.FILL_BOTH);
data.widthHint = 200;
Button one = new Button(shell, SWT.PUSH);
one.setText("one");
one.setLayoutData(data);
// Create a composite to hold the three buttons in the upper right
Composite composite = new Composite(shell, SWT.NONE);
// Create button "two"
data = new GridData(GridData.FILL_BOTH);
Button two = new Button(composite, SWT.PUSH);
two.setText("two");
two.setLayoutData(data);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
GridLayout: HORIZONTAL ALIGN Begin Fill
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutHORIZONTALALIGNBeginFill {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
layout.makeColumnsEqualWidth = true;
shell.setLayout(layout);
// Create the big button in the upper left
GridData data = new GridData(GridData.FILL_BOTH);
data.widthHint = 200;
Button one = new Button(shell, SWT.PUSH);
one.setText("one");
one.setLayoutData(data);
// Create a composite to hold the three buttons in the upper right
Composite composite = new Composite(shell, SWT.NONE);
// Create button "four"
data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
Button four = new Button(composite, SWT.PUSH);
four.setText("four");
four.setLayoutData(data);
// Create the long button across the bottom
data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.grabExcessHorizontalSpace = true;
data.horizontalSpan = 3;
data.heightHint = 150;
Button five = new Button(shell, SWT.PUSH);
five.setText("five");
five.setLayoutData(data);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
GridLayout: horizontalSpacing, verticalSpacing
The default value for each of them is 5.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutSpacing {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.horizontalSpacing = 20;
gridLayout.verticalSpacing = 30;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("button #3");
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
GridLayout: insert widgets into a grid layout
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
//package org.eclipse.swt.snippets;
/*
* GridLayout example snippet: insert widgets into a grid layout
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.1
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutInsertWidgets {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final Composite c = new Composite(shell, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
c.setLayout(layout);
for (int i = 0; i < 10; i++) {
Button b = new Button(c, SWT.PUSH);
b.setText("Button " + i);
}
Button b = new Button(shell, SWT.PUSH);
b.setText("add a new button at row 2 column 1");
final int[] index = new int[1];
b.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
Button s = new Button(c, SWT.PUSH);
s.setText("Special " + index[0]);
index[0]++;
Control[] children = c.getChildren();
s.moveAbove(children[3]);
shell.layout(new Control[] { s });
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
GridLayout: makeColumnsEqualWidth
Specifies whether all columns should be forced to have the same width as the widest cell. The default value is false.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutMakeColumnWidth {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("button #3");
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
GridLayout: marginHeight, marginWidth
The default value for each of them is 5.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutMargin {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.marginHeight = 20;
gridLayout.marginWidth = 30;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("button #3");
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
GridLayout: numColumns
Specifies the number of cell columns in the layout. The default value is 1.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutNumColumns {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("button #3");
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
GridLayout snippet: align widgets in a GridLayout
/*******************************************************************************
* Copyright (c) 2000, 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
//package org.eclipse.swt.snippets;
/*
* GridLayout snippet: align widgets in a GridLayout
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.0
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutWidgetsAlign {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayout layout = new GridLayout(4, false);
shell.setLayout(layout);
Button b = new Button(shell, SWT.PUSH);
b.setText("LEFT, TOP");
b.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("LEFT, CENTER");
b.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("LEFT, BOTTOM");
b.setLayoutData(new GridData(SWT.LEFT, SWT.BOTTOM, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("LEFT, FILL");
b.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("CENTER, TOP");
b.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("CENTER, CENTER");
b.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("CENTER, BOTTOM");
b.setLayoutData(new GridData(SWT.CENTER, SWT.BOTTOM, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("CENTER, FILL");
b.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("RIGHT, TOP");
b.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("RIGHT, CENTER");
b.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("RIGHT, BOTTOM");
b.setLayoutData(new GridData(SWT.RIGHT, SWT.BOTTOM, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("RIGHT, FILL");
b.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("FILL, TOP");
b.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("FILL, CENTER");
b.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("FILL, BOTTOM");
b.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, true, 1, 1));
b = new Button(shell, SWT.PUSH);
b.setText("FILL, FILL");
b.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Horizontal Indentation
Specifies the number of pixels of indentation that is placed along the left side of the cell. The default value is 0.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutIndentation {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1"); // Default alignment
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
list.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
button2.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_END));
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
GridData gridData = new GridData(GridData.VERTICAL_ALIGN_END);
gridData.horizontalIndent = 50;
button2.setLayoutData(gridData);
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Horizontal Span and Vertical Span
Specify the number of column cells and the number of row cells that a control takes up. The default value for each of them is 1.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutSpanVeriticalHorizontal {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1"); // Default alignment
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
list.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
button2.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_END));
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
GridData gridData = new GridData(GridData.VERTICAL_ALIGN_END);
gridData.horizontalSpan = 2;
gridData.horizontalAlignment = GridData.FILL;
button2.setLayoutData(gridData);
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Make more than one control to grab excess space in the same direction
If more than one control are grabbing excess space, the excess space is distributed to them evenly.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutGrabExcess {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1"); // Default alignment
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
gridData.verticalAlignment = GridData.FILL;
list.setLayoutData(gridData);
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
GridData gridData2 = new GridData();
gridData2.grabExcessVerticalSpace = true;
gridData2.grabExcessHorizontalSpace = true;
gridData2.verticalAlignment = GridData.FILL;
gridData2.horizontalAlignment = GridData.FILL;
button2.setLayoutData(gridData2);
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Size Hints: widthHint and heightHint
Specify the minimum width and height of a control. The default value for them is SWT.DEFAULT, indicating that no minimum width or height is specified.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutSizeHint {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1"); // Default alignment
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
GridData gridData = new GridData();
gridData.widthHint = 200;
gridData.heightHint = 200;
list.setLayoutData(gridData);
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
The verticalAlignment specifies the vertical positioning.
Possible values for verticalAlignment include the following:
- BEGINNING: Top-aligned
- CENTER: Center-aligned (the default value)
- END: Bottom-aligned
- FILL: Filling all the available vertical space
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutVerticalAlignment {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1"); // Default alignment
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
list.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
button2.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_END));
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
button3.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL));
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Use GridLayout to layout a dialog
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class GridLayoutDialog {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
Label labelUser;
Label labelFile;
final Text textUser;
final Text textFile;
Button buttonBrowseFile;
Button buttonUpload;
GridLayout gridLayout = new GridLayout(3, false);
shell.setLayout(gridLayout);
labelUser = new Label(shell, SWT.NULL);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.grabExcessHorizontalSpace = true;
textUser = new Text(shell, SWT.SINGLE | SWT.BORDER);
textUser.setLayoutData(gridData);
new Label(shell, SWT.NULL);
// 2nd row.
labelFile = new Label(shell, SWT.NULL);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.grabExcessHorizontalSpace = true;
textFile = new Text(shell, SWT.SINGLE | SWT.BORDER);
textFile.setLayoutData(gridData);
buttonBrowseFile = new Button(shell, SWT.PUSH);
// last row.
gridData = new GridData();
gridData.horizontalSpan = 3;
gridData.horizontalAlignment = GridData.CENTER;
buttonUpload = new Button(shell, SWT.PUSH);
buttonUpload.setLayoutData(gridData);
labelUser.setText("User name: ");
labelFile.setText("Photo: ");
buttonBrowseFile.setText("Browse");
buttonUpload.setText("Upload");
buttonBrowseFile.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
String file = dialog.open();
if (file != null) {
textFile.setText(file);
}
}
});
buttonUpload.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println(textUser.getText());
System.out.println(textFile.getText());
shell.dispose();
}
});
shell.pack();
shell.open();
textUser.forceFocus();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Using GridData Objects
The GridData class is the layout data associated with the GridLayout class.
- Horizontal Alignment and Vertical Alignment.
- Specify how a control is positioned horizontally and vertically in a cell.
- Several possible values for horizontalAlignment are as follows:
- BEGINNING: Left-aligned (the default value)
- CENTER: Center-aligned
- END: Right-aligned
- FILL: Filling all the available horizontal space
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutAlignmentHoriVerical {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.makeColumnsEqualWidth = true;
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1"); // Default alignment
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
list.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
button2.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
Button button3 = new Button(shell, SWT.PUSH);
button3.setText("3");
button3.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
shell.setSize(450, 400);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Using GridLayout
- GridLayout lays out controls in a grid.
- You can tune your GridLayout by setting GridData instances into your controls.
- GridData objects shouldn"t be reused among controls
- GridData objects tune how the layout treats the GridData"s associated controls.
GridData Constructors
ConstructorDescriptionpublic GridData()Constructs a default GridData.public GridData(int style)Constructs a GridData, setting member data values according to the values specified in style.
Using GridLayout with all default values
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
public class GridLayoutDefaultValues {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
shell.setLayout(gridLayout);
Button button1 = new Button(shell, SWT.PUSH);
button1.setText("button1");
List list = new List(shell, SWT.BORDER);
list.add("item 1");
list.add("item 2");
list.add("item 3");
Button button2 = new Button(shell, SWT.PUSH);
button2.setText("button #2");
shell.setSize(450, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}