Java/SWT JFace Eclipse/Layout
Содержание
- 1 A BorderLayout
- 2 Align widgets in a GridLayout
- 3 BorderLayout Sample
- 4 Control Size Location
- 5 Exclude a widget from a GridLayout
- 6 FillLayout Example
- 7 FillLayout Horizontal
- 8 FillLayout Vertical
- 9 FormLayout
- 10 FormLayout: center a label and single line text using a form layout
- 11 FormLayout Complex
- 12 FormLayout: create a simple dialog using form layout
- 13 FormLayout: create a simple OK and CANCEL dialog using form layout
- 14 FormLayout Example
- 15 FormLayout Form Attachment
- 16 FormLayouts
- 17 FormLayout Simple
- 18 GridLayout 2x2
- 19 Grid Layout Complex
- 20 GridLayout Example
- 21 GridLayout Example 2
- 22 GridLayout Sample Grab Space
- 23 Grid Layout Span
- 24 GridLayout with All Options
- 25 Insert widgets into a grid layout
- 26 Layout Components
- 27 Layout Example
- 28 Layout Terms
- 29 No Layout
- 30 RowLayout Example
- 31 Row Layout Horizontal
- 32 Row Layout Test
- 33 Simplest GridLayout
- 34 StackLayout Sample
- 35 Stack Layout Test
- 36 SWT FillLayout
- 37 SWT FillLayout Composite
- 38 SWT FillLayout Sample
- 39 SWT Form Layout Sample
- 40 SWT GridLayout
- 41 SWT GridLayout: align widgets in a horizontal row
- 42 SWT GridLayout: align widgets in a vertical column
- 43 SWT GridLayout Demo
- 44 SWT GridLayout: GridSpan
- 45 SWT GridLayout Sample
- 46 SWT Layout Example in One
- 47 SWT RowLayout
- 48 SWT RowLayout Sample
- 49 SWY Radial Layout
- 50 Use of Radial Layout
A BorderLayout
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Layout; import org.eclipse.swt.widgets.*; /**
* This class contains a BorderLayout, which is loosely patterned after the old * AWT BorderLayout. It uses theBorderData
class to determine * positioning of controls. To position controls, callcontrol.setLayoutData()
, * passing theBorderData
of your choice. * * For example: * ** shell.setLayoutData(new BorderLayout()); * Button button = new Button(shell, SWT.PUSH); * button.setLayoutData(BorderData.NORTH); *
* * Note that you can add as many controls to the same direction as you like, but * the last one added for the direction will be the one displayed. */
class YetAnotherBorderLayout extends Layout {
private Control north; private Control south; private Control east; private Control west; private Control center; /** * Computes the size for this BorderLayout. * * @param composite the composite that contains the controls * @param wHint width hint in pixels for the minimum width * @param hHint height hint in pixels for the minimum height * @param flushCache if true, flushes any cached values * @return Point * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.ruposite, * int, int, boolean) */ protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { getControls(composite); int width = 0, height = 0; // The width is the width of the west control // plus the width of the center control // plus the width of the east control. // If this is less than the width of the north // or the south control, however, use the largest // of those three widths. width += west == null ? 0 : getSize(west, flushCache).x; width += east == null ? 0 : getSize(east, flushCache).x; width += center == null ? 0 : getSize(center, flushCache).x; if (north != null) { Point pt = getSize(north, flushCache); width = Math.max(width, pt.x); } if (south != null) { Point pt = getSize(south, flushCache); width = Math.max(width, pt.x); } // The height is the height of the north control // plus the height of the maximum height of the // west, center, and east controls // plus the height of the south control. height += north == null ? 0 : getSize(north, flushCache).y; height += south == null ? 0 : getSize(south, flushCache).y; int heightOther = center == null ? 0 : getSize(center, flushCache).y; if (west != null) { Point pt = getSize(west, flushCache); heightOther = Math.max(heightOther, pt.y); } if (east != null) { Point pt = getSize(east, flushCache); heightOther = Math.max(heightOther, pt.y); } height += heightOther; // Respect the wHint and hHint return new Point(Math.max(width, wHint), Math.max(height, hHint)); } /** * This does the work of laying out our controls. * * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.ruposite, * boolean) */ protected void layout(Composite composite, boolean flushCache) { getControls(composite); Rectangle rect = composite.getClientArea(); int left = rect.x, right = rect.width, top = rect.y, bottom = rect.height; if (north != null) { Point pt = getSize(north, flushCache); north.setBounds(left, top, rect.width, pt.y); top += pt.y; } if (south != null) { Point pt = getSize(south, flushCache); south.setBounds(left, rect.height - pt.y, rect.width, pt.y); bottom -= pt.y; } if (east != null) { Point pt = getSize(east, flushCache); east.setBounds(rect.width - pt.x, top, pt.x, (bottom - top)); right -= pt.x; } if (west != null) { Point pt = getSize(west, flushCache); west.setBounds(left, top, pt.x, (bottom - top)); left += pt.x; } if (center != null) { center.setBounds(left, top, (right - left), (bottom - top)); } } protected Point getSize(Control control, boolean flushCache) { return control.ruputeSize(SWT.DEFAULT, SWT.DEFAULT, flushCache); } protected void getControls(Composite composite) { // Iterate through all the controls, setting // the member data according to the BorderData. // Note that we overwrite any previously set data. // Note also that we default to CENTER Control[] children = composite.getChildren(); for (int i = 0, n = children.length; i < n; i++) { Control child = children[i]; BorderData borderData = (BorderData) child.getLayoutData(); if (borderData == BorderData.NORTH) north = child; else if (borderData == BorderData.SOUTH) south = child; else if (borderData == BorderData.EAST) east = child; else if (borderData == BorderData.WEST) west = child; else center = child; } }
} //Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) public class BorderLayoutTest {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new YetAnotherBorderLayout()); Button b1 = new Button(shell, SWT.PUSH); b1.setText("North"); b1.setLayoutData(BorderData.NORTH); Button b2 = new Button(shell, SWT.PUSH); b2.setText("South"); b2.setLayoutData(BorderData.SOUTH); Button b3 = new Button(shell, SWT.PUSH); b3.setText("East"); b3.setLayoutData(BorderData.EAST); Button b4 = new Button(shell, SWT.PUSH); b4.setText("West"); b4.setLayoutData(BorderData.WEST); Button b5 = new Button(shell, SWT.PUSH); b5.setText("Center"); b5.setLayoutData(BorderData.CENTER); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}
</source>
Align widgets in a GridLayout
<source lang="java">
/*
* GridLayout snippet: align widgets in a GridLayout * * For a list of all SWT example snippets see * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#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.Shell; public class Snippet172 {
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(); }
}
</source>
BorderLayout Sample
<source lang="java">
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 30, 2004 11:52:21 PM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Layout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; /**
* Lays out a composite, arranging and resizing its components to fit in five * regions: north, south, east, west, and center. */
class BorderLayout extends Layout {
// Region constants. public static final int NORTH = 0; public static final int SOUTH = 1; public static final int CENTER = 2; public static final int EAST = 3; public static final int WEST = 4; /** * Indicates the region that a control belongs to. * */ public static class BorderData { public int region = CENTER; // default. public BorderData() { } public BorderData(int region) { this.region = region; } } // Controls in all the regions. public Control[] controls = new Control[5]; // Cached sizes. Point[] sizes; // Preferred width and height int width; int height; /* * (non-Javadoc) * * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.ruposite, * int, int, boolean) */ protected Point computeSize( Composite composite, int wHint, int hHint, boolean flushCache) { if (sizes == null || flushCache == true) refreshSizes(composite.getChildren()); int w = wHint; int h = hHint; if (w == SWT.DEFAULT) w = width; if (h == SWT.DEFAULT) h = height; return new Point(w, h); } /* * (non-Javadoc) * * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.ruposite, * boolean) */ protected void layout(Composite composite, boolean flushCache) { if (flushCache || sizes == null) refreshSizes(composite.getChildren()); Rectangle clientArea = composite.getClientArea(); // Enough space for all. if (controls[NORTH] != null) { controls[NORTH].setBounds( clientArea.x, clientArea.y, clientArea.width, sizes[NORTH].y); } if (controls[SOUTH] != null) { controls[SOUTH].setBounds( clientArea.x, clientArea.y + clientArea.height - sizes[SOUTH].y, clientArea.width, sizes[SOUTH].y); } if (controls[WEST] != null) { controls[WEST].setBounds( clientArea.x, clientArea.y + sizes[NORTH].y, sizes[WEST].x, clientArea.height - sizes[NORTH].y - sizes[SOUTH].y); } if (controls[EAST] != null) { controls[EAST].setBounds( clientArea.x + clientArea.width - sizes[EAST].x, clientArea.y + sizes[NORTH].y, sizes[EAST].x, clientArea.height - sizes[NORTH].y - sizes[SOUTH].y); } if (controls[CENTER] != null) { controls[CENTER].setBounds( clientArea.x + sizes[WEST].x, clientArea.y + sizes[NORTH].y, clientArea.width - sizes[WEST].x - sizes[EAST].x, clientArea.height - sizes[NORTH].y - sizes[SOUTH].y); } } private void refreshSizes(Control[] children) { for (int i = 0; i < children.length; i++) { Object layoutData = children[i].getLayoutData(); if (layoutData == null || (!(layoutData instanceof BorderData))) continue; BorderData borderData = (BorderData) layoutData; if (borderData.region < 0 || borderData.region > 4) // Invalid. continue; controls[borderData.region] = children[i]; } width = 0; height = 0; if (sizes == null) sizes = new Point[5]; for (int i = 0; i < controls.length; i++) { Control control = controls[i]; if (control == null) { sizes[i] = new Point(0, 0); } else { sizes[i] = control.ruputeSize(SWT.DEFAULT, SWT.DEFAULT, true); } } width = Math.max(width, sizes[NORTH].x); width = Math.max(width, sizes[WEST].x + sizes[CENTER].x + sizes[EAST].x); width = Math.max(width, sizes[SOUTH].x); height = Math.max(Math.max(sizes[WEST].y, sizes[EAST].y), sizes[CENTER].y) + sizes[NORTH].y + sizes[SOUTH].y; }
} /******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 31, 2004 1:05:58 AM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
public class BorderLayoutSample {
Display display = new Display(); Shell shell = new Shell(display); public BorderLayoutSample() { shell.setLayout(new BorderLayout()); Button buttonWest = new Button(shell, SWT.PUSH); buttonWest.setText("West"); buttonWest.setLayoutData(new BorderLayout.BorderData(BorderLayout.WEST)); Button buttonEast = new Button(shell, SWT.PUSH); buttonEast.setText("East"); buttonEast.setLayoutData(new BorderLayout.BorderData(BorderLayout.EAST)); Button buttonNorth = new Button(shell, SWT.PUSH); buttonNorth.setText("North"); buttonNorth.setLayoutData(new BorderLayout.BorderData(BorderLayout.NORTH)); Button buttonSouth = new Button(shell, SWT.PUSH); buttonSouth.setText("South"); buttonSouth.setLayoutData(new BorderLayout.BorderData(BorderLayout.SOUTH)); Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); text.setText("Center"); text.setLayoutData(new BorderLayout.BorderData(BorderLayout.CENTER)); shell.pack(); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } private void init() { } public static void main(String[] args) { new BorderLayoutSample(); }
}
</source>
Control Size Location
<source lang="java">
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 23, 2004 11:26:26 PM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; 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.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class ControlSizeLocation {
Display display = new Display();
Shell shell = new Shell(display);
Button button;
Text x;
Text y;
Text h;
Text w;
Button get;
Button set;
public ControlSizeLocation() {
init();
shell.pack();
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
GridLayout gridLayout = new GridLayout(2, true);
shell.setLayout(gridLayout);
Composite left = new Composite(shell, SWT.NULL);
left.setLayout(new GridLayout());
//left.setLayout(new FillLayout());
left.setLayoutData(new GridData(GridData.FILL_BOTH));
left.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
button = new Button(left, SWT.PUSH);
button.setText("Button");
button.setLayoutData(new GridData());
Composite right = new Composite(shell, SWT.NULL);
right.setLayout(new GridLayout(4, true));
right.setLayoutData(new GridData(GridData.FILL_BOTH));
Label label = new Label(right, SWT.NULL);
label.setText("X");
label = new Label(right, SWT.NULL);
label.setText("Y");
label = new Label(right, SWT.NULL);
label.setText("Width");
label = new Label(right, SWT.NULL);
label.setText("Height");
x = new Text(right, SWT.BORDER);
y = new Text(right, SWT.BORDER);
w = new Text(right, SWT.BORDER);
h = new Text(right, SWT.BORDER);
SelectionListener selectionListener = new SelectionListener() {
public void widgetSelected(SelectionEvent e) {
Button b = (Button) e.widget;
if (b == get) {
System.out.println("------------------------------");
System.out.println("getBounds: " + button.getBounds());
System.out.println("getLocation: " + button.getLocation());
System.out.println("getSize: " + button.getSize());
}else if(b == set) {
int vx = getNumber(x);
int vy = getNumber(y);
int vw = getNumber(w);
int vh = getNumber(h);
if(vx != -1 && vy != -1) {
if(vw != -1 && vh != -1) {
Rectangle rectangle = new Rectangle(vx, vy, vw, vh);
button.setBounds(rectangle);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("# setBounds: " + rectangle);
}else{
Point point = new Point(vx, vy);
button.setLocation(point);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("# setLocation: " + point);
}
}else if(vw != -1 && vh != -1) {
Point point = new Point(vw, vh);
button.setSize(point);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("# setSize: " + point);
}
}
}
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
};
get = new Button(right, SWT.PUSH);
get.setText("Get");
get.addSelectionListener(selectionListener);
set = new Button(right, SWT.PUSH);
set.setText("Set");
set.addSelectionListener(selectionListener);
}
/**
*
* @param text
* @return -1
if invalid.
*/
private int getNumber(Text text) {
if(text == null)
return -1;
String value = text.getText();
if(value == null || value.trim().length() == 0)
return -1;
try {
return Integer.parseInt(value.trim());
}catch(Exception e) {
}
return -1;
}
public static void main(String[] args) {
new ControlSizeLocation();
}
}
</source>
Exclude a widget from a GridLayout
<source lang="java">
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://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets */
public class Snippet175 {
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(); }
}
</source>
FillLayout Example
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class FillLayoutExample {
Display d; Shell s; FillLayoutExample() { d = new Display(); s = new Shell(d); s.setSize(250, 250); s.setText("A FillLayout Example"); s.setLayout(new FillLayout()); final Text t = new Text(s, SWT.MULTI | SWT.BORDER | SWT.WRAP); s.open(); while (!s.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); } public static void main(String[] argv) { new FillLayoutExample(); }
}
</source>
FillLayout Horizontal
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.SWT; public class FillLayoutHorizontal {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout(SWT.HORIZONTAL)); new Button(shell, SWT.PUSH).setText("one"); new Button(shell, SWT.PUSH).setText("two"); new Button(shell, SWT.PUSH).setText("three"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}
</source>
FillLayout Vertical
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.SWT; public class FillLayoutVertical {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new FillLayout(SWT.VERTICAL)); new Button(shell, SWT.PUSH).setText("one"); new Button(shell, SWT.PUSH).setText("two"); new Button(shell, SWT.PUSH).setText("three"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}
</source>
FormLayout
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.SWT; public class FormLayoutFormData {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); FormLayout layout = new FormLayout(); layout.marginHeight = 5; layout.marginWidth = 10; shell.setLayout(layout); Button button = new Button(shell, SWT.PUSH); button.setText("Button"); FormData data = new FormData(); data.height = 50; data.width = 50; button.setLayoutData(data); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}
</source>
FormLayout: center a label and single line text using a form layout
<source lang="java">
/*
* FormLayout example snippet: center a label and single line text using a form layout * * For a list of all SWT example snippets see * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets */
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.FontMetrics; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; 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 Snippet69 {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); Label label = new Label(shell, SWT.NONE | SWT.BORDER); label.setText("Name"); Text text = new Text(shell, SWT.NONE); FormLayout layout = new FormLayout(); layout.marginWidth = layout.marginHeight = 5; shell.setLayout(layout); GC gc = new GC(text); FontMetrics fm = gc.getFontMetrics(); int charWidth = fm.getAverageCharWidth(); int width = text.ruputeSize(charWidth * 8, SWT.DEFAULT).x; gc.dispose(); FormData data = new FormData(width, SWT.DEFAULT); text.setLayoutData(data); data.left = new FormAttachment(label, 5); data.top = new FormAttachment(label, 0, SWT.CENTER); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}
</source>
FormLayout Complex
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.SWT; public class FormLayoutComplex {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); FormLayout layout = new FormLayout(); shell.setLayout(layout); Button one = new Button(shell, SWT.PUSH); one.setText("One"); FormData data = new FormData(); data.top = new FormAttachment(0, 5); data.left = new FormAttachment(0, 5); data.bottom = new FormAttachment(50, -5); data.right = new FormAttachment(50, -5); one.setLayoutData(data); Composite composite = new Composite(shell, SWT.NONE); GridLayout gridLayout = new GridLayout(); gridLayout.marginHeight = 0; gridLayout.marginWidth = 0; composite.setLayout(gridLayout); Button two = new Button(composite, SWT.PUSH); two.setText("two"); GridData gridData = new GridData(GridData.FILL_BOTH); two.setLayoutData(gridData); Button three = new Button(composite, SWT.PUSH); three.setText("three"); gridData = new GridData(GridData.FILL_BOTH); three.setLayoutData(gridData); Button four = new Button(composite, SWT.PUSH); four.setText("four"); gridData = new GridData(GridData.FILL_BOTH); four.setLayoutData(gridData); data = new FormData(); data.top = new FormAttachment(0, 5); data.left = new FormAttachment(one, 5); data.bottom = new FormAttachment(50, -5); data.right = new FormAttachment(100, -5); composite.setLayoutData(data); Button five = new Button(shell, SWT.PUSH); five.setText("five"); data = new FormData(); data.top = new FormAttachment(one, 5); data.left = new FormAttachment(0, 5); data.bottom = new FormAttachment(100, -5); data.right = new FormAttachment(100, -5); five.setLayoutData(data); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}
</source>
FormLayout: create a simple dialog using form layout
<source lang="java">
/*
* FormLayout example snippet: create a simple dialog using form layout * * For a list of all SWT example snippets see * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets */
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class Snippet65 {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); Label label = new Label(shell, SWT.WRAP); label .setText("This is a long text string that will wrap when the dialog is resized."); List list = new List(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); list.setItems(new String[] { "Item 1", "Item2" }); Button button1 = new Button(shell, SWT.PUSH); button1.setText("Ok"); Button button2 = new Button(shell, SWT.PUSH); button2.setText("Cancel"); final int insetX = 4, insetY = 4; FormLayout formLayout = new FormLayout(); formLayout.marginWidth = insetX; formLayout.marginHeight = insetY; shell.setLayout(formLayout); Point size = label.ruputeSize(SWT.DEFAULT, SWT.DEFAULT); final FormData labelData = new FormData(size.x, SWT.DEFAULT); labelData.left = new FormAttachment(0, 0); labelData.right = new FormAttachment(100, 0); label.setLayoutData(labelData); shell.addListener(SWT.Resize, new Listener() { public void handleEvent(Event e) { Rectangle rect = shell.getClientArea(); labelData.width = rect.width - insetX * 2; shell.layout(); } }); FormData button2Data = new FormData(); button2Data.right = new FormAttachment(100, -insetX); button2Data.bottom = new FormAttachment(100, 0); button2.setLayoutData(button2Data); FormData button1Data = new FormData(); button1Data.right = new FormAttachment(button2, -insetX); button1Data.bottom = new FormAttachment(100, 0); button1.setLayoutData(button1Data); FormData listData = new FormData(); listData.left = new FormAttachment(0, 0); listData.right = new FormAttachment(100, 0); listData.top = new FormAttachment(label, insetY); listData.bottom = new FormAttachment(button2, -insetY); list.setLayoutData(listData); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}
</source>
FormLayout: create a simple OK and CANCEL dialog using form layout
<source lang="java">
/*
* FormLayout example snippet: create a simple OK/CANCEL dialog using form layout * * For a list of all SWT example snippets see * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets */
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class Snippet71 {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.pack(); shell.open(); Shell dialog = new Shell(shell, SWT.DIALOG_TRIM); Label label = new Label(dialog, SWT.NONE); label.setText("Exit the application?"); Button okButton = new Button(dialog, SWT.PUSH); okButton.setText("&OK"); Button cancelButton = new Button(dialog, SWT.PUSH); cancelButton.setText("&Cancel"); FormLayout form = new FormLayout(); form.marginWidth = form.marginHeight = 8; dialog.setLayout(form); FormData okData = new FormData(); okData.top = new FormAttachment(label, 8); okButton.setLayoutData(okData); FormData cancelData = new FormData(); cancelData.left = new FormAttachment(okButton, 8); cancelData.top = new FormAttachment(okButton, 0, SWT.TOP); cancelButton.setLayoutData(cancelData); dialog.setDefaultButton(okButton); dialog.pack(); dialog.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}
</source>
FormLayout Example
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; 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 FormLayoutExample {
Display d; Shell s; FormLayoutExample() { d = new Display(); s = new Shell(d); s.setSize(250, 250); s.setText("A FormLayout Example"); s.setLayout(new FormLayout()); final Label l1 = new Label(s, SWT.RIGHT); l1.setText("First Name"); FormData fd = new FormData(); fd.top = new FormAttachment(10, 10); fd.left = new FormAttachment(0, 10); fd.bottom = new FormAttachment(30, 0); fd.right = new FormAttachment(40, 0); l1.setLayoutData(fd); final Label l2 = new Label(s, SWT.RIGHT); l2.setText("Last Name"); fd = new FormData(); fd.top = new FormAttachment(l1, 5); fd.left = new FormAttachment(0, 10); fd.bottom = new FormAttachment(40, 0); fd.right = new FormAttachment(40, 0); l2.setLayoutData(fd); final Text t1 = new Text(s, SWT.BORDER | SWT.SINGLE); fd = new FormData(); fd.top = new FormAttachment(l1, 0, SWT.TOP); fd.left = new FormAttachment(l1, 10); t1.setLayoutData(fd); final Text t2 = new Text(s, SWT.BORDER | SWT.SINGLE); fd = new FormData(); fd.top = new FormAttachment(l2, 0, SWT.TOP); fd.left = new FormAttachment(l2, 10); t2.setLayoutData(fd); s.open(); while (!s.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); } public static void main(String[] argv) { new FormLayoutExample(); }
}
</source>
FormLayout Form Attachment
<source lang="java">
import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.SWT; public class FormLayoutFormAttachment {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); FormLayout layout = new FormLayout(); layout.marginHeight = 5; layout.marginWidth = 10; shell.setLayout(layout); Button button = new Button(shell, SWT.PUSH); button.setText("Button"); FormData data = new FormData(); data.height = 50; data.right = new FormAttachment(100, -50); data.left = new FormAttachment(0, 10); data.top = new FormAttachment(1, 4, 0); button.setLayoutData(data); Button button2 = new Button(shell, SWT.PUSH); button2.setText("Button 2"); data = new FormData(); button2.setLayoutData(data); data.bottom = new FormAttachment(100, 0); data.top = new FormAttachment(button, 5); data.left = new FormAttachment(button, 0, SWT.LEFT); data.right = new FormAttachment(button, 0, SWT.RIGHT); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}
</source>
FormLayouts
<source lang="java">
/******************************************************************************
* All Right Reserved. * Copyright (c) 1998, 2004 Jackwind Li Guojie * * Created on 2004-6-16 20:22:44 by JACK * $Id$ * *****************************************************************************/
import org.eclipse.jface.window.ApplicationWindow; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.forms.widgets.ColumnLayout; import org.eclipse.ui.forms.widgets.FormToolkit; import org.eclipse.ui.forms.widgets.ScrolledForm; import org.eclipse.ui.forms.widgets.TableWrapData; import org.eclipse.ui.forms.widgets.TableWrapLayout; public class FormLayouts extends ApplicationWindow{
FormToolkit toolkit; ScrolledForm form; /** * @param parentShell */ public FormLayouts(Shell parentShell) { super(parentShell); } private void demoColumnLayout() { ColumnLayout layout = new ColumnLayout(); layout.maxNumColumns = 4; form.getBody().setLayout(layout); Color color = form.getDisplay().getSystemColor(SWT.COLOR_YELLOW); Label label = null; for(int i=0; i<10; i++) { label = toolkit.createLabel(form.getBody(), "Text label #" + i); } } private void demoTableWrapLayout() { TableWrapLayout layout = new TableWrapLayout(); layout.numColumns = 2; form.getBody().setLayout(layout); Color color = form.getDisplay().getSystemColor(SWT.COLOR_YELLOW); Label label = toolkit.createLabel(form.getBody(), "Some text spans over two columns in the first row. ", SWT.WRAP); TableWrapData data = new TableWrapData(); data.colspan = 2; label.setLayoutData(data); label.setBackground(color); label = toolkit.createLabel(form.getBody(), "Some text in the first column of the second row. and here is goes on and on ... ... ... ... ", SWT.WRAP); label.setBackground(color); label = toolkit.createLabel(form.getBody(), "Some text in the second column of the second row. ", SWT.WRAP); label.setBackground(color); } /* * (non-Javadoc) * * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.ruposite) */ protected Control createContents(Composite parent) { Composite composite = new Composite(parent, SWT.NULL); composite.setLayout(new FillLayout()); // Sets up the toolkit. toolkit = new FormToolkit(getShell().getDisplay()); // Creates a form instance. form = toolkit.createScrolledForm(composite); form.setLayoutData(new GridData(GridData.FILL_BOTH)); // Sets title. form.setText("Form Layouts Demo"); // demoTableWrapLayout(); demoColumnLayout(); return composite; } public static void main(String[] args) { FormLayouts win = new FormLayouts(null); win.setBlockOnOpen(true); win.open(); }
}
</source>
FormLayout Simple
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.SWT; public class FormLayoutSimple {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); FormLayout layout = new FormLayout(); layout.marginHeight = 5; layout.marginWidth = 10; shell.setLayout(layout); new Button(shell, SWT.PUSH).setText("Button"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}
</source>
GridLayout 2x2
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.SWT; 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(); }
}
</source>
Grid Layout Complex
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.SWT; 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); // Create button "two" data = new GridData(GridData.FILL_BOTH); Button two = new Button(composite, SWT.PUSH); two.setText("two"); two.setLayoutData(data); // Create button "three" data = new GridData(GridData.HORIZONTAL_ALIGN_CENTER); Button three = new Button(composite, SWT.PUSH); three.setText("three"); three.setLayoutData(data); // 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(); }
}
</source>
GridLayout Example
<source lang="java">
import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class GridLayoutExample {
Display d; Shell s; GridLayoutExample() { d = new Display(); s = new Shell(d); s.setSize(250, 250); s.setText("A GridLayout Example"); GridLayout gl = new GridLayout(); gl.numColumns = 3; s.setLayout(gl); s.open(); while (!s.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); } public static void main(String[] argv) { new GridLayoutExample(); }
}
</source>
GridLayout Example 2
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridLayout; 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 GridLayoutExample2 {
Display d; Shell s; GridLayoutExample2() { d = new Display(); s = new Shell(d); s.setSize(250, 250); s.setText("A GridLayout Example"); GridLayout gl = new GridLayout(); gl.numColumns = 3; s.setLayout(gl); final Label l1 = new Label(s, SWT.BORDER); l1.setText("Column One"); final Label l2 = new Label(s, SWT.BORDER); l2.setText("Column Two"); final Label l3 = new Label(s, SWT.BORDER); l3.setText("Column Three"); final Text t1 = new Text(s, SWT.SINGLE | SWT.BORDER); final Text t2 = new Text(s, SWT.SINGLE | SWT.BORDER); final Text t3 = new Text(s, SWT.SINGLE | SWT.BORDER); final Text t4 = new Text(s, SWT.SINGLE | SWT.BORDER); final Text t5 = new Text(s, SWT.SINGLE | SWT.BORDER); final Text t6 = new Text(s, SWT.SINGLE | SWT.BORDER); s.open(); while (!s.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); } public static void main(String[] argv) { new GridLayoutExample2(); }
}
</source>
GridLayout Sample Grab Space
<source lang="java">
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 29, 2004 1:57:48 PM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
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.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class GridLayoutSampleGrabSpace {
public GridLayoutSampleGrabSpace() { Display display = new Display(); Shell shell = new Shell(display); GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 3; shell.setLayout(gridLayout); Label label = new Label(shell, SWT.BORDER); label.setText("label"); GridData gridData3 = new GridData(); gridData3.widthHint = 60; gridData3.heightHint = 20; label.setLayoutData(gridData3); Text text = new Text(shell, SWT.SINGLE | SWT.BORDER); text.setText("text"); GridData gridData = new GridData(); gridData.grabExcessHorizontalSpace = true; gridData.grabExcessVerticalSpace = true; gridData.horizontalAlignment = GridData.FILL; gridData.verticalAlignment = GridData.FILL; text.setLayoutData(gridData); Button button = new Button(shell, SWT.PUSH); button.setText("button"); GridData gridData2 = new GridData(); gridData2.grabExcessVerticalSpace = true; gridData2.grabExcessHorizontalSpace = true; gridData2.verticalAlignment = GridData.FILL; gridData2.horizontalAlignment = GridData.FILL; button.setLayoutData(gridData2); shell.setSize(300, 80); //shell.pack(); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } private void init() { } public static void main(String[] args) { new GridLayoutSampleGrabSpace(); }
}
</source>
Grid Layout Span
<source lang="java">
// -Djava.library.path=D:\Java_Dev\eclipse\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86 /******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 29, 2004 10:12:41 AM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
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 GridLayoutSampleSpan {
Display display = new Display(); Shell shell = new Shell(display); public GridLayoutSampleSpan() { GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 2; gridLayout.makeColumnsEqualWidth = true; shell.setLayout(gridLayout); Button button1 = new Button(shell, SWT.PUSH); button1.setText("button1"); button1.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); 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"); GridData gridData = new GridData(GridData.VERTICAL_ALIGN_END); gridData.horizontalSpan = 2; gridData.horizontalAlignment = GridData.FILL; button2.setLayoutData(gridData); Button button3 = new Button(shell, SWT.PUSH); button3.setText("3"); GridData gridData2 = new GridData(GridData.VERTICAL_ALIGN_END); gridData2.verticalSpan = 3; button3.setLayoutData(gridData2); shell.pack(); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } private void init() { } public static void main(String[] args) { new GridLayoutSampleSpan(); }
}
</source>
GridLayout with All Options
<source lang="java">
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; public class Ch6GridLayoutAllOptionsComposite extends Composite {
public Ch6GridLayoutAllOptionsComposite(Composite parent) { super(parent, SWT.NONE); int[] fillStyles = { SWT.NONE, GridData.FILL_HORIZONTAL, GridData.FILL_VERTICAL, GridData.FILL_BOTH }; int[] alignStyles = { SWT.NONE, GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.HORIZONTAL_ALIGN_END, GridData.HORIZONTAL_ALIGN_CENTER, GridData.HORIZONTAL_ALIGN_FILL, GridData.VERTICAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_END, GridData.VERTICAL_ALIGN_CENTER, GridData.VERTICAL_ALIGN_FILL }; GridLayout layout = new GridLayout(fillStyles.length, false); setLayout(layout); int count = 0; for (int i = 0; i < alignStyles.length; ++i) { for (int j = 0; j < fillStyles.length; ++j) { Button button = new Button(this, SWT.NONE); button.setText("Cell " + count++); button.setLayoutData(new GridData(fillStyles[j] | alignStyles[i])); } } }
}
</source>
Insert widgets into a grid layout
<source lang="java">
/*
* GridLayout example snippet: insert widgets into a grid layout * * For a list of all SWT example snippets see * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets */
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 Snippet6 {
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(); }
}
</source>
Layout Components
<source lang="java">
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 27, 2004 11:50:44 PM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.events.ControlEvent; import org.eclipse.swt.events.ControlListener; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.RowLayout; 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 Layouting {
Display display = new Display(); Shell shell = new Shell(display); int count = 0; public Layouting() { init(); shell.setLayout(new RowLayout()); final Composite composite = new Composite(shell, SWT.BORDER); composite.setLayout(new RowLayout()); composite.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); composite.addControlListener(new ControlListener() { public void controlMoved(ControlEvent e) { // TODO Auto-generated method stub } public void controlResized(ControlEvent e) { System.out.println("Composite resize."); } }); Button buttonAdd = new Button(shell, SWT.PUSH); buttonAdd.setText("Add new button"); buttonAdd.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { Button button = new Button(composite, SWT.PUSH); button.setText("Button #" + (count++)); //composite.layout(true); composite.pack(); //shell.layout(true; // shell.pack(true); } }); // shell.pack(); shell.setSize(450, 100); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } private void init() { } public static void main(String[] args) { new Layouting(); }
}
</source>
Layout Example
<source lang="java">
import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class LayoutExample {
Display d; Shell s; LayoutExample() { d = new Display(); s = new Shell(d); s.setSize(500, 500); s.setText("A Shell Menu Example"); s.setLayout(new FillLayout()); s.open(); while (!s.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); } public static void main(String[] argv) { new LayoutExample(); }
}
</source>
Layout Terms
<source lang="java">
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 27, 2004 7:23:10 PM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; 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 LayoutTerms {
Display display = new Display(); Shell shell = new Shell(display); public LayoutTerms() { init(); Composite composite; System.out.println("Bounds: " + shell.getBounds()); System.out.println("Client area: " + shell.getClientArea()); shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE)); RowLayout rowLayout = new RowLayout(); rowLayout.type = SWT.HORIZONTAL; rowLayout.wrap = true; rowLayout.marginLeft = 15; rowLayout.marginTop = 5; rowLayout.marginRight = 15; rowLayout.marginBottom = 10; rowLayout.spacing = 8; shell.setLayout(rowLayout); Button button1 = new Button(shell, SWT.PUSH); button1.setText("button1"); Button button2 = new Button(shell, SWT.PUSH); button2.setText("button number 2"); Button button3 = new Button(shell, SWT.PUSH); button3.setText("3"); shell.pack(); shell.open(); System.out.println("button1: " + button1.getBounds()); System.out.println("button2: " + button2.getBounds()); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } private void init() { } public static void main(String[] args) { new LayoutTerms(); }
}
</source>
No Layout
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.*; import org.eclipse.swt.SWT; public class NoLayoutSimple {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); Button button = new Button(shell, SWT.PUSH); button.setText("No layout"); button.setBounds(new Rectangle(5, 5, 100, 100)); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}
</source>
RowLayout Example
<source lang="java">
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; import org.eclipse.swt.widgets.Text; public class RowLayoutExample {
Display d; Shell s; RowLayoutExample() { d = new Display(); s = new Shell(d); s.setSize(250, 250); s.setText("A RowLayout Example"); s.setLayout(new RowLayout()); final Text t = new Text(s, SWT.SINGLE | SWT.BORDER); final Button b = new Button(s, SWT.BORDER); final Button b1 = new Button(s, SWT.BORDER); b.setText("OK"); b1.setText("Cancel"); s.open(); while (!s.isDisposed()) { if (!d.readAndDispatch()) d.sleep(); } d.dispose(); } public static void main(String[] argv) { new RowLayoutExample(); }
}
</source>
Row Layout Horizontal
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.SWT; public class RowLayoutHorizontal {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout(SWT.HORIZONTAL)); 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"); new Button(shell, SWT.PUSH).setText("five"); new Button(shell, SWT.PUSH).setText("six"); new Button(shell, SWT.PUSH).setText("seven"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}
</source>
Row Layout Test
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.widgets.*; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.layout.RowData; import org.eclipse.swt.SWT; public class RowLayoutTest {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); RowLayout layout = new RowLayout(SWT.VERTICAL); layout.marginLeft = 12; layout.marginTop = 0; layout.justify = true; 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"); new Button(shell, SWT.PUSH).setText("five"); new Button(shell, SWT.PUSH).setText("six"); Button b = new Button(shell, SWT.PUSH); b.setText("seven"); b.setLayoutData(new RowData(100, 100)); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}
</source>
Simplest GridLayout
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ruposite; public class Ch6GridLayoutComposite extends Composite {
public Ch6GridLayoutComposite(Composite parent) { super(parent, SWT.NONE); GridLayout layout = new GridLayout(4, false); setLayout(layout); for (int i = 0; i < 16; ++i) { Button button = new Button(this, SWT.NONE); button.setText("Cell " + i); } }
}
</source>
StackLayout Sample
<source lang="java">
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 30, 2004 10:59:14 PM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StackLayout; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StackLayoutSample {
Display display = new Display(); Shell shell = new Shell(display); final Button[] buttons = new Button[3]; public StackLayoutSample() { final StackLayout stackLayout = new StackLayout(); shell.setLayout(stackLayout); for(int i=0; i<buttons.length; i++) { buttons[i] = new Button(shell, SWT.NULL); buttons[i].setText("Button #" + i); buttons[i].addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { // Flip to next button. Button nextButton = null; for(int i=0; i<buttons.length; i++) { if(buttons[i] == e.widget) { if(i == buttons.length - 1) nextButton = buttons[0]; else nextButton = buttons[i+1]; } } stackLayout.topControl = nextButton; shell.layout(); } public void widgetDefaultSelected(SelectionEvent e) { } }); } // Initial stackLayout.topControl = buttons[0]; shell.setSize(200, 100); shell.open(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } private void init() { } public static void main(String[] args) { new StackLayoutSample(); }
}
</source>
Stack Layout Test
<source lang="java">
//Send questions, comments, bug reports, etc. to the authors: //Rob Warner (rwarner@interspatial.ru) //Robert Harris (rbrt_harris@yahoo.ru) import org.eclipse.swt.events.*; import org.eclipse.swt.widgets.*; import org.eclipse.swt.custom.StackLayout; import org.eclipse.swt.SWT; public class StackLayoutTest {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); StackLayout layout = new StackLayout(); shell.setLayout(layout); StackLayoutSelectionAdapter adapter = new StackLayoutSelectionAdapter(shell, layout); Button one = new Button(shell, SWT.PUSH); one.setText("one"); one.addSelectionListener(adapter); Button two = new Button(shell, SWT.PUSH); two.setText("two"); two.addSelectionListener(adapter); Button three = new Button(shell, SWT.PUSH); three.setText("three"); three.addSelectionListener(adapter); layout.topControl = one; shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
} class StackLayoutSelectionAdapter extends SelectionAdapter {
Shell shell; StackLayout layout; public StackLayoutSelectionAdapter(Shell shell, StackLayout layout) { this.shell = shell; this.layout = layout; } public void widgetSelected(SelectionEvent event) { Control control = layout.topControl; Control[] children = shell.getChildren(); int i = 0; for (int n = children.length; i < n; i++) { Control child = children[i]; if (child == control) { break; } } ++i; if (i >= children.length) { i = 0; } layout.topControl = children[i]; shell.layout(); }
}
</source>
SWT FillLayout
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ruposite; public class Ch5Composite extends Composite {
public Ch5Composite(Composite parent) { super(parent, SWT.NONE); FillLayout layout = new FillLayout(SWT.VERTICAL); setLayout(layout); for (int i = 0; i < 8; ++i) { Button button = new Button(this, SWT.NONE); button.setText("Sample Text"); } }
}
</source>
SWT FillLayout Composite
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ruposite; public class Ch6FillLayoutComposite extends Composite {
public Ch6FillLayoutComposite(Composite parent) { super(parent, SWT.NONE); FillLayout layout = new FillLayout(SWT.VERTICAL); setLayout(layout); for (int i = 0; i < 8; ++i) { Button button = new Button(this, SWT.NONE); button.setText("Sample Text"); } }
}
</source>
SWT FillLayout Sample
<source lang="java">
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 28, 2004 10:04:24 PM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class FillLayoutSample {
Display display = new Display(); Shell shell = new Shell(display); public FillLayoutSample() { FillLayout fillLayout = new FillLayout(SWT.VERTICAL); fillLayout.marginHeight = 5; fillLayout.marginWidth = 5; fillLayout.spacing = 1; shell.setLayout(fillLayout); Button button1 = new Button(shell, SWT.PUSH); button1.setText("button1"); Button button2 = new Button(shell, SWT.PUSH); button2.setText("button number 2"); Button button3 = new Button(shell, SWT.PUSH); button3.setText("3"); shell.pack(); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } private void init() { } public static void main(String[] args) { new FillLayoutSample(); }
}
</source>
SWT Form Layout Sample
<source lang="java">
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 30, 2004 7:11:12 PM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class FormLayoutSample {
Display display = new Display(); Shell shell = new Shell(display); public FormLayoutSample() { shell.setLayout(new FormLayout()); Button button1 = new Button(shell, SWT.PUSH); button1.setText("button1"); FormData formData = new FormData(); formData.left = new FormAttachment(20); formData.top = new FormAttachment(20); button1.setLayoutData(formData); Button button2 = new Button(shell, SWT.PUSH); button2.setText("button number 2"); formData = new FormData(); formData.left = new FormAttachment(button1, 0, SWT.CENTER); formData.top = new FormAttachment(button1, 0, SWT.CENTER); button2.setLayoutData(formData);
// Button button3 = new Button(shell, SWT.PUSH); // button3.setText("3"); // // formData = new FormData(); // formData.top = new FormAttachment(button2, 10); // formData.left = new FormAttachment(button2, 0, SWT.LEFT); // button3.setLayoutData(formData);
shell.pack(); //shell.setSize(500, 600); shell.open(); //textUser.forceFocus(); //System.out.println("Button3: " + button3.getBounds()); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } private void init() { } public static void main(String[] args) { new FormLayoutSample(); }
}
</source>
SWT GridLayout
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /**
* @author Steven Holzner */
public class SWTGridLayout {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setSize(300, 200); shell.setLayout(new RowLayout()); final Composite composite = new Composite(shell, SWT.NONE); GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 4; composite.setLayout(gridLayout); for (int loopIndex = 0; loopIndex < 18; loopIndex++) { Button button = new Button(composite, SWT.PUSH); button.setText("Button " + loopIndex); } shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}
</source>
SWT GridLayout: align widgets in a horizontal row
<source lang="java">
/*
* GridLayout snippet: align widgets in a horizontal row * * For a list of all SWT example snippets see * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets */
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowData; 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 Snippet176 {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); RowLayout layout = new RowLayout(SWT.HORIZONTAL); layout.wrap = true; layout.fill = false; layout.justify = true; shell.setLayout(layout); Button b = new Button(shell, SWT.PUSH); b.setText("Button 1"); 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("Not shown"); b.setVisible(false); RowData data = new RowData(); data.exclude = true; b.setLayoutData(data); b = new Button(shell, SWT.PUSH); b.setText("Button 200 high"); data = new RowData(); data.height = 200; b.setLayoutData(data); b = new Button(shell, SWT.PUSH); b.setText("Button 200 wide"); data = new RowData(); data.width = 200; b.setLayoutData(data); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}
</source>
SWT GridLayout: align widgets in a vertical column
<source lang="java">
/*
* GridLayout snippet: align widgets in a vertical column * * For a list of all SWT example snippets see * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets */
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowData; 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 Snippet177 { public static void main(String[] args) {
Display display = new Display(); Shell shell = new Shell(display); RowLayout layout = new RowLayout(SWT.VERTICAL); layout.wrap = true; layout.fill = true; layout.justify = false; shell.setLayout(layout); Button b = new Button(shell, SWT.PUSH); b.setText("Button 1"); 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("Not shown"); b.setVisible(false); RowData data = new RowData(); data.exclude = true; b.setLayoutData(data); b = new Button(shell, SWT.PUSH); b.setText("Button 200 high"); data = new RowData(); data.height = 200; b.setLayoutData(data); b = new Button(shell, SWT.PUSH); b.setText("Button 200 wide"); data = new RowData(); data.width = 200; b.setLayoutData(data); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose();
} }
</source>
SWT GridLayout Demo
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class LayoutClass {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setSize(200, 200); shell.setText("Layout Example"); GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 4; shell.setLayout(gridLayout); for (int loopIndex = 0; loopIndex < 28; loopIndex++) { Label label = new Label(shell, SWT.SHADOW_NONE); label.setText("Label " + loopIndex); } shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}
</source>
SWT GridLayout: GridSpan
<source lang="java">
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.Text; public class Ch6GridSpanComposite extends Composite {
public Ch6GridSpanComposite(Composite parent) { super(parent, SWT.NONE); GridLayout layout = new GridLayout(3, false); setLayout(layout); Button b = new Button(this, SWT.NONE); b.setText("Button 1"); b = new Button(this, SWT.NONE); b.setText("Button 2"); b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); b = new Button(this, SWT.NONE); b.setText("Button 3"); Text t = new Text(this, SWT.MULTI); GridData data = new GridData(GridData.FILL_BOTH); data.horizontalSpan = 2; data.verticalSpan = 2; t.setLayoutData(data); b = new Button(this, SWT.NONE); b.setText("Button 4"); b.setLayoutData(new GridData(GridData.FILL_VERTICAL)); b = new Button(this, SWT.NONE); b.setText("Button 5"); b.setLayoutData(new GridData(GridData.FILL_VERTICAL)); }
}
</source>
SWT GridLayout Sample
<source lang="java">
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 29, 2004 10:12:41 AM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
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 GridLayoutSample {
Display display = new Display(); Shell shell = new Shell(display); public GridLayoutSample() { GridLayout gridLayout = new GridLayout(); gridLayout.numColumns = 2; gridLayout.makeColumnsEqualWidth = true; shell.setLayout(gridLayout); Button button1 = new Button(shell, SWT.PUSH); button1.setText("button1"); button1.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING)); 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"); GridData gridData = new GridData(GridData.VERTICAL_ALIGN_END); gridData.horizontalIndent = 5; button2.setLayoutData(gridData); Button button3 = new Button(shell, SWT.PUSH); button3.setText("3"); button3.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL)); shell.pack(); shell.open(); //textUser.forceFocus(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } private void init() { } public static void main(String[] args) { new GridLayoutSample(); }
}
</source>
SWT Layout Example in One
<source lang="java">
import java.text.MessageFormat; import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.Vector; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.custom.SashForm; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.custom.TableEditor; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.events.ShellAdapter; import org.eclipse.swt.events.ShellEvent; import org.eclipse.swt.events.TraverseEvent; import org.eclipse.swt.events.TraverseListener; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.layout.RowData; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.rubo; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.CoolBar; import org.eclipse.swt.widgets.CoolItem; import org.eclipse.swt.widgets.Dialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.ProgressBar; import org.eclipse.swt.widgets.Scale; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Slider; import org.eclipse.swt.widgets.TabFolder; import org.eclipse.swt.widgets.TabItem; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.swt.widgets.ToolItem; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; public class LayoutExample {
private TabFolder tabFolder; /** * Creates an instance of a LayoutExample embedded inside the supplied * parent Composite. * * @param parent * the container of the example */ public LayoutExample(Composite parent) { tabFolder = new TabFolder(parent, SWT.NULL); Tab[] tabs = new Tab[] { new FillLayoutTab(this), new RowLayoutTab(this), new GridLayoutTab(this), new FormLayoutTab(this), }; for (int i = 0; i < tabs.length; i++) { TabItem item = new TabItem(tabFolder, SWT.NULL); item.setText(tabs[i].getTabText()); item.setControl(tabs[i].createTabFolderPage(tabFolder)); } } /** * Grabs input focus. */ public void setFocus() { tabFolder.setFocus(); } /** * Disposes of all resources associated with a particular instance of the * LayoutExample. */ public void dispose() { tabFolder = null; } /** * Invokes as a standalone program. */ public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); new LayoutExample(shell); shell.setText(getResourceString("window.title")); shell.addShellListener(new ShellAdapter() { public void shellClosed(ShellEvent e) { Shell[] shells = display.getShells(); for (int i = 0; i < shells.length; i++) { if (shells[i] != shell) shells[i].close(); } } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } /** * Gets a string from the resource bundle. We don"t want to crash because of * a missing String. Returns the key if not found. */ static String getResourceString(String key) { return key; } /** * Gets a string from the resource bundle and binds it with the given * arguments. If the key is not found, return the key. */ static String getResourceString(String key, Object[] args) { try { return MessageFormat.format(getResourceString(key), args); } catch (MissingResourceException e) { return key; } catch (NullPointerException e) { return "!" + key + "!"; } }
} /*******************************************************************************
* Copyright (c) 2000, 2003 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 ******************************************************************************/
/**
* Tab
is the abstract superclass of every page in the example"s
* tab folder. Each page in the tab folder displays a layout, and allows the
* user to manipulate the layout.
*
* A typical page in a Tab contains a two column composite. The left column
* contains the layout group, which contains the "layout composite" (the one
* that has the example layout). The right column contains the "control" group.
* The "control" group allows the user to interact with the example. Typical
* operations are modifying layout parameters, adding children to the "layout
* composite", and modifying child layout data. The "Code" button in the
* "control" group opens a new window containing code that will regenerate the
* layout. This code (or parts of it) can be selected and copied to the
* clipboard.
*/
abstract class Tab {
/* Common groups and composites */ Composite tabFolderPage; SashForm sash; Group layoutGroup, controlGroup, childGroup; /* The composite that contains the example layout */ Composite layoutComposite; /* Common controls for modifying the example layout */ String[] names; Control[] children; Button size, add, delete, clear, code; /* Common values for working with TableEditors */ Table table; int index; TableItem newItem, lastSelected; Vector data = new Vector(); /* Controlling instance */ final LayoutExample instance; /* Listeners */ SelectionListener selectionListener = new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { resetEditors(); } }; TraverseListener traverseListener = new TraverseListener() { public void keyTraversed(TraverseEvent e) { if (e.detail == SWT.TRAVERSE_RETURN) { e.doit = false; resetEditors(); } } }; /** * Creates the Tab within a given instance of LayoutExample. */ Tab(LayoutExample instance) { this.instance = instance; } /** * Creates the "child" group. This is the group that allows you to add * children to the layout. It exists within the controlGroup. */ void createChildGroup() { childGroup = new Group(controlGroup, SWT.NONE); childGroup.setText(LayoutExample.getResourceString("Children")); GridLayout layout = new GridLayout(); layout.numColumns = 3; childGroup.setLayout(layout); GridData data = new GridData(GridData.FILL_BOTH); data.horizontalSpan = 2; childGroup.setLayoutData(data); createChildWidgets(); } /** * Creates the controls for modifying the "children" table, and the table * itself. Subclasses override this method to augment the standard table. */ void createChildWidgets() { /* Controls for adding and removing children */ add = new Button(childGroup, SWT.PUSH); add.setText(LayoutExample.getResourceString("Add")); add.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); delete = new Button(childGroup, SWT.PUSH); delete.setText(LayoutExample.getResourceString("Delete")); delete.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); delete.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { resetEditors(); int[] selected = table.getSelectionIndices(); table.remove(selected); /* Refresh the control indices of the table */ for (int i = 0; i < table.getItemCount(); i++) { table.getItem(i).setText(0, String.valueOf(i)); } refreshLayoutComposite(); layoutComposite.layout(true); layoutGroup.layout(true); } }); clear = new Button(childGroup, SWT.PUSH); clear.setText(LayoutExample.getResourceString("Clear")); clear.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); clear.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { resetEditors(); children = layoutComposite.getChildren(); for (int i = 0; i < children.length; i++) { children[i].dispose(); } table.removeAll(); data.clear(); children = new Control[0]; layoutGroup.layout(true); } }); /* Create the "children" table */ table = new Table(childGroup, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION); table.setLinesVisible(true); table.setHeaderVisible(true); GridData gridData = new GridData(GridData.FILL_BOTH); gridData.horizontalSpan = 3; gridData.heightHint = 150; table.setLayoutData(gridData); table.addTraverseListener(traverseListener); /* Add columns to the table */ String[] columnHeaders = getLayoutDataFieldNames(); for (int i = 0; i < columnHeaders.length; i++) { TableColumn column = new TableColumn(table, SWT.NONE); column.setText(columnHeaders[i]); if (i == 0) column.setWidth(20); else if (i == 1) column.setWidth(80); else column.pack(); } } /** * Creates the TableEditor with a CCombo in the first column of the table. * This CCombo lists all the controls that the user can select to place on * their layout. */ void createComboEditor(CCombo combo, TableEditor comboEditor) { combo.setItems(new String[] { "Button", "Canvas", "Combo", "Composite", "CoolBar", "Group", "Label", "List", "ProgressBar", "Scale", "Slider", "StyledText", "Table", "Text", "ToolBar", "Tree" }); combo.setText(newItem.getText(1)); /* Set up editor */ comboEditor.horizontalAlignment = SWT.LEFT; comboEditor.grabHorizontal = true; comboEditor.minimumWidth = 50; comboEditor.setEditor(combo, newItem, 1); /* Add listener */ combo.addTraverseListener(new TraverseListener() { public void keyTraversed(TraverseEvent e) { if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_RETURN) { resetEditors(); } if (e.detail == SWT.TRAVERSE_ESCAPE) { disposeEditors(); } } }); } /** * Creates the "control" group. This is the group on the right half of each * example tab. It contains controls for adding new children to the * layoutComposite, and for modifying the children"s layout data. */ void createControlGroup() { controlGroup = new Group(sash, SWT.NONE); controlGroup.setText(LayoutExample.getResourceString("Parameters")); GridLayout layout = new GridLayout(); layout.numColumns = 2; controlGroup.setLayout(layout); size = new Button(controlGroup, SWT.CHECK); size.setText(LayoutExample.getResourceString("Preferred_Size")); size.setSelection(false); size.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { resetEditors(); if (size.getSelection()) { layoutComposite.setLayoutData(new GridData()); layoutGroup.layout(true); } else { layoutComposite.setLayoutData(new GridData( GridData.FILL_BOTH)); layoutGroup.layout(true); } } }); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; size.setLayoutData(data); createControlWidgets(); } /** * Creates the "control" widget children. Subclasses override this method to * augment the standard controls created. */ void createControlWidgets() { createChildGroup(); code = new Button(controlGroup, SWT.PUSH); code.setText(LayoutExample.getResourceString("Code")); GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_CENTER | GridData.GRAB_HORIZONTAL); gridData.horizontalSpan = 2; code.setLayoutData(gridData); code.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { final Shell shell = new Shell(); shell .setText(LayoutExample .getResourceString("Generated_Code")); shell.setLayout(new FillLayout()); final StyledText text = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); String layoutCode = generateCode().toString(); if (layoutCode.length() == 0) return; text.setText(layoutCode); Menu bar = new Menu(shell, SWT.BAR); shell.setMenuBar(bar); MenuItem editItem = new MenuItem(bar, SWT.CASCADE); editItem.setText(LayoutExample.getResourceString("Edit")); Menu menu = new Menu(bar); MenuItem select = new MenuItem(menu, SWT.PUSH); select.setText(LayoutExample.getResourceString("Select_All")); select.setAccelerator(SWT.MOD1 + "A"); select.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { text.selectAll(); } }); MenuItem copy = new MenuItem(menu, SWT.PUSH); copy.setText(LayoutExample.getResourceString("Copy")); copy.setAccelerator(SWT.MOD1 + "C"); copy.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { text.copy(); } }); MenuItem exit = new MenuItem(menu, SWT.PUSH); exit.setText(LayoutExample.getResourceString("Exit")); exit.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { shell.close(); } }); editItem.setMenu(menu); shell.pack(); shell.setSize(400, 500); shell.open(); Display display = shell.getDisplay(); while (!shell.isDisposed()) if (!display.readAndDispatch()) display.sleep(); } }); } /** * Creates the example layout. Subclasses override this method. */ void createLayout() { } /** * Creates the composite that contains the example layout. */ void createLayoutComposite() { layoutComposite = new Composite(layoutGroup, SWT.BORDER); layoutComposite.setLayoutData(new GridData(GridData.FILL_BOTH)); createLayout(); } /** * Creates the layout group. This is the group on the left half of each * example tab. */ void createLayoutGroup() { layoutGroup = new Group(sash, SWT.NONE); layoutGroup.setText(LayoutExample.getResourceString("Layout")); layoutGroup.setLayout(new GridLayout()); createLayoutComposite(); } /** * Creates the tab folder page. * * @param tabFolder * org.eclipse.swt.widgets.TabFolder * @return the new page for the tab folder */ Composite createTabFolderPage(TabFolder tabFolder) { /* Create a two column page with a SashForm */ tabFolderPage = new Composite(tabFolder, SWT.NULL); tabFolderPage.setLayout(new FillLayout()); sash = new SashForm(tabFolderPage, SWT.HORIZONTAL); /* Create the "layout" and "control" columns */ createLayoutGroup(); createControlGroup(); return tabFolderPage; } /** * Creates the TableEditor with a Text in the given column of the table. */ void createTextEditor(Text text, TableEditor textEditor, int column) { text.setFont(table.getFont()); text.selectAll(); textEditor.horizontalAlignment = SWT.LEFT; textEditor.grabHorizontal = true; textEditor.setEditor(text, newItem, column); text.addTraverseListener(new TraverseListener() { public void keyTraversed(TraverseEvent e) { if (e.detail == SWT.TRAVERSE_TAB_NEXT) { resetEditors(true); } if (e.detail == SWT.TRAVERSE_ESCAPE) { disposeEditors(); } } }); } /** * Disposes the editors without placing their contents into the table. * Subclasses override this method. */ void disposeEditors() { } /** * Generates the code needed to produce the example layout. */ StringBuffer generateCode() { /* Make sure all information being entered is stored in the table */ resetEditors(); /* Get names for controls in the layout */ names = new String[children.length]; for (int i = 0; i < children.length; i++) { Control control = children[i]; String controlClass = control.getClass().toString(); String controlType = controlClass.substring(controlClass .lastIndexOf(".") + 1); names[i] = controlType.toLowerCase() + i; } /* Create StringBuffer containing the code */ StringBuffer code = new StringBuffer(); code.append("import org.eclipse.swt.*;\n"); code.append("import org.eclipse.swt.custom.*;\n"); code.append("import org.eclipse.swt.graphics.*;\n"); code.append("import org.eclipse.swt.layout.*;\n"); code.append("import org.eclipse.swt.widgets.*;\n\n"); code.append("public class MyLayout {\n"); code.append("\tpublic static void main (String [] args) {\n"); code.append("\t\tDisplay display = new Display ();\n"); code.append("\t\tShell shell = new Shell (display);\n"); /* Get layout specific code */ code.append(generateLayoutCode()); code.append("\n\t\tshell.pack ();\n\t\tshell.open ();\n\n"); code.append("\t\twhile (!shell.isDisposed ()) {\n"); code.append("\t\t\tif (!display.readAndDispatch ())\n"); code .append("\t\t\t\tdisplay.sleep ();\n\t\t}\n\t\tdisplay.dispose ();\n\t}\n}"); return code; } /** * Generates layout specific code for the example layout. Subclasses * override this method. */ StringBuffer generateLayoutCode() { return new StringBuffer(); } /** * Returns the StringBuffer for the code which will create a child control. */ StringBuffer getChildCode(Control control, int i) { StringBuffer code = new StringBuffer(); /* Find the type of control */ String controlClass = control.getClass().toString(); String controlType = controlClass.substring(controlClass .lastIndexOf(".") + 1); /* Find the style of the control */ String styleString; if (controlType.equals("Button")) { styleString = "SWT.PUSH"; } else if (controlType.equals("Text")) { styleString = "SWT.BORDER"; } else if (controlType.equals("StyledText")) { styleString = "SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL"; } else if (controlType.equals("Canvas") || controlType.equals("Composite") || controlType.equals("Table") || controlType.equals("StyledText") || controlType.equals("ToolBar") || controlType.equals("Tree") || controlType.equals("List")) { styleString = "SWT.BORDER"; } else styleString = "SWT.NONE"; /* Write out the control being declared */ code.append("\n\t\t" + controlType + " " + names[i] + " = new " + controlType + " (shell, " + styleString + ");\n"); /* Add items to those controls that need items */ if (controlType.equals("Combo") || controlType.equals("List")) { code .append("\t\t" + names[i] + ".setItems (new String [] {\"Item 1\", \"Item 2\", \"Item 2\"});\n"); } else if (controlType.equals("Table")) { code.append("\t\t" + names[i] + ".setLinesVisible (true);\n"); for (int j = 1; j < 3; j++) { code.append("\t\tTableItem tableItem" + j + " = new TableItem (" + names[i] + ", SWT.NONE);\n"); code.append("\t\ttableItem" + j + ".setText (\"Item" + j + "\");\n"); } } else if (controlType.equals("Tree")) { for (int j = 1; j < 3; j++) { code.append("\t\tTreeItem treeItem" + j + " = new TreeItem (" + names[i] + ", SWT.NONE);\n"); code.append("\t\ttreeItem" + j + ".setText (\"Item" + j + "\");\n"); } } else if (controlType.equals("ToolBar")) { for (int j = 1; j < 3; j++) { code.append("\t\tToolItem toolItem" + j + " = new ToolItem (" + names[i] + ", SWT.NONE);\n"); code.append("\t\ttoolItem" + j + ".setText (\"Item" + j + "\");\n"); } } else if (controlType.equals("CoolBar")) { code.append("\t\tToolBar coolToolBar = new ToolBar (" + names[i] + ", SWT.BORDER);\n"); code .append("\t\tToolItem coolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); code.append("\t\tcoolToolItem.setText (\"Item 1\");\n"); code .append("\t\tcoolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); code.append("\t\tcoolToolItem.setText (\"Item 2\");\n"); code.append("\t\tCoolItem coolItem1 = new CoolItem (" + names[i] + ", SWT.NONE);\n"); code.append("\t\tcoolItem1.setControl (coolToolBar);\n"); code .append("\t\tPoint size = coolToolBar.ruputeSize (SWT.DEFAULT, SWT.DEFAULT);\n"); code .append("\t\tcoolItem1.setSize (coolItem1.ruputeSize (size.x, size.y));\n"); code.append("\t\tcoolToolBar = new ToolBar (" + names[i] + ", SWT.BORDER);\n"); code .append("\t\tcoolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); code.append("\t\tcoolToolItem.setText (\"Item 3\");\n"); code .append("\t\tcoolToolItem = new ToolItem (coolToolBar, SWT.NONE);\n"); code.append("\t\tcoolToolItem.setText (\"Item 4\");\n"); code.append("\t\tCoolItem coolItem2 = new CoolItem (" + names[i] + ", SWT.NONE);\n"); code.append("\t\tcoolItem2.setControl (coolToolBar);\n"); code .append("\t\tsize = coolToolBar.ruputeSize (SWT.DEFAULT, SWT.DEFAULT);\n"); code .append("\t\tcoolItem2.setSize (coolItem2.ruputeSize (size.x, size.y));\n"); code.append("\t\t" + names[i] + ".setSize (" + names[i] + ".ruputeSize (SWT.DEFAULT, SWT.DEFAULT));\n"); } else if (controlType.equals("ProgressBar")) { code.append("\t\t" + names[i] + ".setSelection (50);\n"); } /* Set text for those controls that support it */ if (controlType.equals("Button") || controlType.equals("Combo") || controlType.equals("Group") || controlType.equals("Label") || controlType.equals("StyledText") || controlType.equals("Text")) { code.append("\t\t" + names[i] + ".setText (\"" + names[i] + "\");\n"); } return code; } /** * Returns the layout data field names. Subclasses override this method. */ String[] getLayoutDataFieldNames() { return new String[] {}; } /** * Gets the text for the tab folder item. Subclasses override this method. */ String getTabText() { return ""; } /** * Refreshes the composite and draws all controls in the layout example. */ void refreshLayoutComposite() { /* Remove children that are already laid out */ children = layoutComposite.getChildren(); for (int i = 0; i < children.length; i++) { children[i].dispose(); } /* Add all children listed in the table */ TableItem[] items = table.getItems(); children = new Control[items.length]; String[] itemValues = new String[] { LayoutExample.getResourceString("Item", new String[] { "1" }), LayoutExample.getResourceString("Item", new String[] { "2" }), LayoutExample.getResourceString("Item", new String[] { "3" }) }; for (int i = 0; i < items.length; i++) { String control = items[i].getText(1); if (control.equals("Button")) { Button button = new Button(layoutComposite, SWT.PUSH); button.setText(LayoutExample.getResourceString("Button_Index", new String[] { new Integer(i).toString() })); children[i] = button; } else if (control.equals("Canvas")) { Canvas canvas = new Canvas(layoutComposite, SWT.BORDER); children[i] = canvas; } else if (control.equals("Combo")) { Combo combo = new Combo(layoutComposite, SWT.NONE); combo.setItems(itemValues); combo.setText(LayoutExample.getResourceString("Combo_Index", new String[] { new Integer(i).toString() })); children[i] = combo; } else if (control.equals("Composite")) { Composite composite = new Composite(layoutComposite, SWT.BORDER); children[i] = composite; } else if (control.equals("CoolBar")) { CoolBar coolBar = new CoolBar(layoutComposite, SWT.NONE); ToolBar toolBar = new ToolBar(coolBar, SWT.BORDER); ToolItem item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); CoolItem coolItem1 = new CoolItem(coolBar, 0); coolItem1.setControl(toolBar); toolBar = new ToolBar(coolBar, SWT.BORDER); item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "3" })); item = new ToolItem(toolBar, 0); item.setText(LayoutExample.getResourceString("Item", new String[] { "4" })); CoolItem coolItem2 = new CoolItem(coolBar, 0); coolItem2.setControl(toolBar); Point size = toolBar.ruputeSize(SWT.DEFAULT, SWT.DEFAULT); coolItem1.setSize(coolItem1.ruputeSize(size.x, size.y)); coolItem2.setSize(coolItem2.ruputeSize(size.x, size.y)); coolBar.setSize(coolBar.ruputeSize(SWT.DEFAULT, SWT.DEFAULT)); children[i] = coolBar; } else if (control.equals("Group")) { Group group = new Group(layoutComposite, SWT.NONE); group.setText(LayoutExample.getResourceString("Group_Index", new String[] { new Integer(i).toString() })); children[i] = group; } else if (control.equals("Label")) { Label label = new Label(layoutComposite, SWT.NONE); label.setText(LayoutExample.getResourceString("Label_Index", new String[] { new Integer(i).toString() })); children[i] = label; } else if (control.equals("List")) { List list = new List(layoutComposite, SWT.BORDER); list.setItems(itemValues); children[i] = list; } else if (control.equals("ProgressBar")) { ProgressBar progress = new ProgressBar(layoutComposite, SWT.NONE); progress.setSelection(50); children[i] = progress; } else if (control.equals("Scale")) { Scale scale = new Scale(layoutComposite, SWT.NONE); children[i] = scale; } else if (control.equals("Slider")) { Slider slider = new Slider(layoutComposite, SWT.NONE); children[i] = slider; } else if (control.equals("StyledText")) { StyledText styledText = new StyledText(layoutComposite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); styledText.setText(LayoutExample.getResourceString( "StyledText_Index", new String[] { new Integer(i) .toString() })); children[i] = styledText; } else if (control.equals("Table")) { Table table = new Table(layoutComposite, SWT.BORDER); table.setLinesVisible(true); TableItem item1 = new TableItem(table, 0); item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); TableItem item2 = new TableItem(table, 0); item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); children[i] = table; } else if (control.equals("Text")) { Text text = new Text(layoutComposite, SWT.BORDER); text.setText(LayoutExample.getResourceString("Text_Index", new String[] { new Integer(i).toString() })); children[i] = text; } else if (control.equals("ToolBar")) { ToolBar toolBar = new ToolBar(layoutComposite, SWT.BORDER); ToolItem item1 = new ToolItem(toolBar, 0); item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); ToolItem item2 = new ToolItem(toolBar, 0); item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); children[i] = toolBar; } else { Tree tree = new Tree(layoutComposite, SWT.BORDER); TreeItem item1 = new TreeItem(tree, 0); item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" })); TreeItem item2 = new TreeItem(tree, 0); item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" })); children[i] = tree; } } } /** * Takes information from TableEditors and stores it. Subclasses override * this method. */ void resetEditors() { resetEditors(false); } void resetEditors(boolean tab) { } /** * Sets the layout data for the children of the layout. Subclasses override * this method. */ void setLayoutData() { } /** * Sets the state of the layout. Subclasses override this method. */ void setLayoutState() { }
} /*******************************************************************************
* Copyright (c) 2000, 2003 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 ******************************************************************************/
class RowLayoutTab extends Tab {
/* Controls for setting layout parameters */ Button horizontal, vertical; Button wrap, pack, justify; Combo marginRight, marginLeft, marginTop, marginBottom, spacing; /* The example layout instance */ RowLayout rowLayout; /* TableEditors and related controls */ TableEditor comboEditor, widthEditor, heightEditor; CCombo combo; Text widthText, heightText; /* Constants */ final int COMBO_COL = 1; final int WIDTH_COL = 2; final int HEIGHT_COL = 3; final int TOTAL_COLS = 4; /** * Creates the Tab within a given instance of LayoutExample. */ RowLayoutTab(LayoutExample instance) { super(instance); } /** * Creates the widgets in the "child" group. */ void createChildWidgets() { /* Add common controls */ super.createChildWidgets(); /* Add TableEditors */ comboEditor = new TableEditor(table); widthEditor = new TableEditor(table); heightEditor = new TableEditor(table); table.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent e) { resetEditors(); index = table.getSelectionIndex(); Point pt = new Point(e.x, e.y); newItem = table.getItem(pt); if (newItem == null) return; TableItem oldItem = comboEditor.getItem(); if (newItem == oldItem || newItem != lastSelected) { lastSelected = newItem; return; } table.showSelection(); combo = new CCombo(table, SWT.READ_ONLY); createComboEditor(combo, comboEditor); widthText = new Text(table, SWT.SINGLE); widthText .setText(((String[]) data.elementAt(index))[WIDTH_COL]); createTextEditor(widthText, widthEditor, WIDTH_COL); heightText = new Text(table, SWT.SINGLE); heightText .setText(((String[]) data.elementAt(index))[HEIGHT_COL]); createTextEditor(heightText, heightEditor, HEIGHT_COL); for (int i = 0; i < table.getColumnCount(); i++) { Rectangle rect = newItem.getBounds(i); if (rect.contains(pt)) { switch (i) { case COMBO_COL: combo.setFocus(); break; case WIDTH_COL: widthText.setFocus(); break; case HEIGHT_COL: heightText.setFocus(); break; default: resetEditors(); break; } } } } }); /* Add listener to add an element to the table */ add.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { TableItem item = new TableItem(table, 0); String[] insert = new String[] { String.valueOf(table.indexOf(item)), "Button", "-1", "-1" }; item.setText(insert); data.addElement(insert); resetEditors(); } }); } /** * Creates the control widgets. */ void createControlWidgets() { /* Controls the type of RowLayout */ Group typeGroup = new Group(controlGroup, SWT.NONE); typeGroup.setText(LayoutExample.getResourceString("Type")); typeGroup.setLayout(new GridLayout()); GridData data = new GridData(GridData.FILL_HORIZONTAL); typeGroup.setLayoutData(data); horizontal = new Button(typeGroup, SWT.RADIO); horizontal.setText("SWT.HORIZONTAL"); horizontal.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); horizontal.setSelection(true); horizontal.addSelectionListener(selectionListener); vertical = new Button(typeGroup, SWT.RADIO); vertical.setText("SWT.VERTICAL"); vertical.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); vertical.addSelectionListener(selectionListener); /* Controls the margins and spacing of the RowLayout */ String[] marginValues = new String[] { "0", "3", "5", "10" }; Group marginGroup = new Group(controlGroup, SWT.NONE); marginGroup.setText(LayoutExample.getResourceString("Margins_Spacing")); data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); data.verticalSpan = 2; marginGroup.setLayoutData(data); GridLayout layout = new GridLayout(); layout.numColumns = 2; marginGroup.setLayout(layout); new Label(marginGroup, SWT.NONE).setText("marginRight"); marginRight = new Combo(marginGroup, SWT.NONE); marginRight.setItems(marginValues); marginRight.select(1); marginRight.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); marginRight.addSelectionListener(selectionListener); marginRight.addTraverseListener(traverseListener); new Label(marginGroup, SWT.NONE).setText("marginLeft"); marginLeft = new Combo(marginGroup, SWT.NONE); marginLeft.setItems(marginValues); marginLeft.select(1); marginLeft.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); marginLeft.addSelectionListener(selectionListener); marginLeft.addTraverseListener(traverseListener); new Label(marginGroup, SWT.NONE).setText("marginTop"); marginTop = new Combo(marginGroup, SWT.NONE); marginTop.setItems(marginValues); marginTop.select(1); marginTop.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); marginTop.addSelectionListener(selectionListener); marginTop.addTraverseListener(traverseListener); new Label(marginGroup, SWT.NONE).setText("marginBottom"); marginBottom = new Combo(marginGroup, SWT.NONE); marginBottom.setItems(marginValues); marginBottom.select(1); marginBottom.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); marginBottom.addSelectionListener(selectionListener); marginBottom.addTraverseListener(traverseListener); new Label(marginGroup, SWT.NONE).setText("spacing"); spacing = new Combo(marginGroup, SWT.NONE); spacing.setItems(marginValues); spacing.select(1); spacing.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); spacing.addSelectionListener(selectionListener); spacing.addTraverseListener(traverseListener); /* Controls other parameters of the RowLayout */ Group specGroup = new Group(controlGroup, SWT.NONE); specGroup.setText(LayoutExample.getResourceString("Properties")); specGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); specGroup.setLayout(new GridLayout()); wrap = new Button(specGroup, SWT.CHECK); wrap.setText("wrap"); wrap.setSelection(true); wrap.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); wrap.addSelectionListener(selectionListener); pack = new Button(specGroup, SWT.CHECK); pack.setText("pack"); pack.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); pack.setSelection(true); pack.addSelectionListener(selectionListener); justify = new Button(specGroup, SWT.CHECK); justify.setText("justify"); justify.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); justify.addSelectionListener(selectionListener); /* Add common controls */ super.createControlWidgets(); /* Position the sash */ sash.setWeights(new int[] { 6, 5 }); } /** * Creates the example layout. */ void createLayout() { rowLayout = new RowLayout(); layoutComposite.setLayout(rowLayout); } /** * Disposes the editors without placing their contents into the table. */ void disposeEditors() { comboEditor.setEditor(null, null, -1); combo.dispose(); widthText.dispose(); heightText.dispose(); } /** * Generates code for the example layout. */ StringBuffer generateLayoutCode() { StringBuffer code = new StringBuffer(); code.append("\t\tRowLayout rowLayout = new RowLayout ();\n"); if (rowLayout.type == SWT.VERTICAL) { code.append("\t\trowLayout.type = SWT.VERTICAL;\n"); } if (rowLayout.wrap == false) { code.append("\t\trowLayout.wrap = false;\n"); } if (rowLayout.pack == false) { code.append("\t\trowLayout.pack = false;\n"); } if (rowLayout.justify == true) { code.append("\t\trowLayout.justify = true;\n"); } if (rowLayout.marginLeft != 3) { code.append("\t\trowLayout.marginLeft = " + rowLayout.marginLeft + ";\n"); } if (rowLayout.marginRight != 3) { code.append("\t\trowLayout.marginRight = " + rowLayout.marginRight + ";\n"); } if (rowLayout.marginTop != 3) { code.append("\t\trowLayout.marginTop = " + rowLayout.marginTop + ";\n"); } if (rowLayout.marginBottom != 3) { code.append("\t\trowLayout.marginBottom = " + rowLayout.marginBottom + ";\n"); } if (rowLayout.spacing != 3) { code.append("\t\trowLayout.spacing = " + rowLayout.spacing + ";\n"); } code.append("\t\tshell.setLayout (rowLayout);\n"); boolean first = true; for (int i = 0; i < children.length; i++) { Control control = children[i]; code.append(getChildCode(control, i)); RowData data = (RowData) control.getLayoutData(); if (data != null) { if (data.width != -1 || data.height != -1) { code.append("\t\t"); if (first) { code.append("RowData "); first = false; } if (data.width == -1) { code.append("data = new RowData (SWT.DEFAULT, " + data.height + ");\n"); } else if (data.height == -1) { code.append("data = new RowData (" + data.width + ", SWT.DEFAULT);\n"); } else { code.append("data = new RowData (" + data.width + ", " + data.height + ");\n"); } code.append("\t\t" + names[i] + ".setLayoutData (data);\n"); } } } return code; } /** * Returns the layout data field names. */ String[] getLayoutDataFieldNames() { return new String[] { "", "Control", "width", "height" }; } /** * Gets the text for the tab folder item. */ String getTabText() { return "RowLayout"; } /** * Takes information from TableEditors and stores it. */ void resetEditors() { resetEditors(false); } void resetEditors(boolean tab) { TableItem oldItem = comboEditor.getItem(); if (oldItem != null) { int row = table.indexOf(oldItem); /* Make sure user has entered valid data */ try { new Integer(widthText.getText()).intValue(); } catch (NumberFormatException e) { widthText.setText(oldItem.getText(WIDTH_COL)); } try { new Integer(heightText.getText()).intValue(); } catch (NumberFormatException e) { heightText.setText(oldItem.getText(HEIGHT_COL)); } String[] insert = new String[] { String.valueOf(row), combo.getText(), widthText.getText(), heightText.getText() }; data.setElementAt(insert, row); for (int i = 0; i < TOTAL_COLS; i++) { oldItem.setText(i, ((String[]) data.elementAt(row))[i]); } if (!tab) disposeEditors(); } setLayoutState(); refreshLayoutComposite(); setLayoutData(); layoutComposite.layout(true); layoutGroup.layout(true); } /** * Sets the layout data for the children of the layout. */ void setLayoutData() { Control[] children = layoutComposite.getChildren(); TableItem[] items = table.getItems(); RowData data; int width, height; for (int i = 0; i < children.length; i++) { width = new Integer(items[i].getText(WIDTH_COL)).intValue(); height = new Integer(items[i].getText(HEIGHT_COL)).intValue(); data = new RowData(width, height); children[i].setLayoutData(data); } } /** * Sets the state of the layout. */ void setLayoutState() { /* Set the type of layout */ if (vertical.getSelection()) { rowLayout.type = SWT.VERTICAL; } else { rowLayout.type = SWT.HORIZONTAL; } /* Set the margins and spacing */ try { rowLayout.marginRight = new Integer(marginRight.getText()) .intValue(); } catch (NumberFormatException e) { rowLayout.marginRight = 3; marginRight.select(1); } try { rowLayout.marginLeft = new Integer(marginLeft.getText()).intValue(); } catch (NumberFormatException e) { rowLayout.marginLeft = 3; marginLeft.select(1); } try { rowLayout.marginTop = new Integer(marginTop.getText()).intValue(); } catch (NumberFormatException e) { rowLayout.marginTop = 3; marginTop.select(1); } try { rowLayout.marginBottom = new Integer(marginBottom.getText()) .intValue(); } catch (NumberFormatException e) { rowLayout.marginBottom = 3; marginBottom.select(1); } try { rowLayout.spacing = new Integer(spacing.getText()).intValue(); } catch (NumberFormatException e) { rowLayout.spacing = 3; spacing.select(1); } /* Set the other layout properties */ rowLayout.wrap = wrap.getSelection(); rowLayout.pack = pack.getSelection(); rowLayout.justify = justify.getSelection(); }
} /*******************************************************************************
* Copyright (c) 2000, 2003 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 ******************************************************************************/
class GridLayoutTab extends Tab {
/* Controls for setting layout parameters */ Text numColumns; Button makeColumnsEqualWidth; Combo marginHeight, marginWidth, horizontalSpacing, verticalSpacing; /* The example layout instance */ GridLayout gridLayout; /* TableEditors and related controls */ TableEditor comboEditor, widthEditor, heightEditor; TableEditor vAlignEditor, hAlignEditor, hIndentEditor; TableEditor hSpanEditor, vSpanEditor, hGrabEditor, vGrabEditor; CCombo combo, vAlign, hAlign, hGrab, vGrab; Text widthText, heightText, hIndent, hSpan, vSpan; /* Constants */ final int COMBO_COL = 1; final int WIDTH_COL = 2; final int HEIGHT_COL = 3; final int HALIGN_COL = 4; final int VALIGN_COL = 5; final int HINDENT_COL = 6; final int HSPAN_COL = 7; final int VSPAN_COL = 8; final int HGRAB_COL = 9; final int VGRAB_COL = 10; final int TOTAL_COLS = 11; /** * Creates the Tab within a given instance of LayoutExample. */ GridLayoutTab(LayoutExample instance) { super(instance); } /** * Creates the widgets in the "child" group. */ void createChildWidgets() { /* Create the TraverseListener */ final TraverseListener traverseListener = new TraverseListener() { public void keyTraversed(TraverseEvent e) { if (e.detail == SWT.TRAVERSE_RETURN || e.detail == SWT.TRAVERSE_TAB_NEXT) resetEditors(); if (e.detail == SWT.TRAVERSE_ESCAPE) disposeEditors(); } }; /* Add common controls */ super.createChildWidgets(); /* Add TableEditors */ comboEditor = new TableEditor(table); widthEditor = new TableEditor(table); heightEditor = new TableEditor(table); vAlignEditor = new TableEditor(table); hAlignEditor = new TableEditor(table); hIndentEditor = new TableEditor(table); hSpanEditor = new TableEditor(table); vSpanEditor = new TableEditor(table); hGrabEditor = new TableEditor(table); vGrabEditor = new TableEditor(table); table.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent e) { resetEditors(); index = table.getSelectionIndex(); Point pt = new Point(e.x, e.y); newItem = table.getItem(pt); if (newItem == null) return; TableItem oldItem = comboEditor.getItem(); if (newItem == oldItem || newItem != lastSelected) { lastSelected = newItem; return; } table.showSelection(); combo = new CCombo(table, SWT.READ_ONLY); createComboEditor(combo, comboEditor); widthText = new Text(table, SWT.SINGLE); widthText .setText(((String[]) data.elementAt(index))[WIDTH_COL]); createTextEditor(widthText, widthEditor, WIDTH_COL); heightText = new Text(table, SWT.SINGLE); heightText .setText(((String[]) data.elementAt(index))[HEIGHT_COL]); createTextEditor(heightText, heightEditor, HEIGHT_COL); String[] alignValues = new String[] { "BEGINNING", "CENTER", "END", "FILL" }; hAlign = new CCombo(table, SWT.NONE); hAlign.setItems(alignValues); hAlign.setText(newItem.getText(HALIGN_COL)); hAlignEditor.horizontalAlignment = SWT.LEFT; hAlignEditor.grabHorizontal = true; hAlignEditor.minimumWidth = 50; hAlignEditor.setEditor(hAlign, newItem, HALIGN_COL); hAlign.addTraverseListener(traverseListener); vAlign = new CCombo(table, SWT.NONE); vAlign.setItems(alignValues); vAlign.setText(newItem.getText(VALIGN_COL)); vAlignEditor.horizontalAlignment = SWT.LEFT; vAlignEditor.grabHorizontal = true; vAlignEditor.minimumWidth = 50; vAlignEditor.setEditor(vAlign, newItem, VALIGN_COL); vAlign.addTraverseListener(traverseListener); hIndent = new Text(table, SWT.SINGLE); hIndent .setText(((String[]) data.elementAt(index))[HINDENT_COL]); createTextEditor(hIndent, hIndentEditor, HINDENT_COL); hSpan = new Text(table, SWT.SINGLE); hSpan.setText(((String[]) data.elementAt(index))[HSPAN_COL]); createTextEditor(hSpan, hSpanEditor, HSPAN_COL); vSpan = new Text(table, SWT.SINGLE); vSpan.setText(((String[]) data.elementAt(index))[VSPAN_COL]); createTextEditor(vSpan, vSpanEditor, VSPAN_COL); String[] boolValues = new String[] { "false", "true" }; hGrab = new CCombo(table, SWT.NONE); hGrab.setItems(boolValues); hGrab.setText(newItem.getText(HGRAB_COL)); hGrabEditor.horizontalAlignment = SWT.LEFT; hGrabEditor.grabHorizontal = true; hGrabEditor.minimumWidth = 50; hGrabEditor.setEditor(hGrab, newItem, HGRAB_COL); hGrab.addTraverseListener(traverseListener); vGrab = new CCombo(table, SWT.NONE); vGrab.setItems(boolValues); vGrab.setText(newItem.getText(VGRAB_COL)); vGrabEditor.horizontalAlignment = SWT.LEFT; vGrabEditor.grabHorizontal = true; vGrabEditor.minimumWidth = 50; vGrabEditor.setEditor(vGrab, newItem, VGRAB_COL); vGrab.addTraverseListener(traverseListener); for (int i = 0; i < table.getColumnCount(); i++) { Rectangle rect = newItem.getBounds(i); if (rect.contains(pt)) { switch (i) { case COMBO_COL: combo.setFocus(); break; case WIDTH_COL: widthText.setFocus(); break; case HEIGHT_COL: heightText.setFocus(); break; case HALIGN_COL: hAlign.setFocus(); break; case VALIGN_COL: vAlign.setFocus(); break; case HINDENT_COL: hIndent.setFocus(); break; case HSPAN_COL: hSpan.setFocus(); break; case VSPAN_COL: vSpan.setFocus(); break; case HGRAB_COL: hGrab.setFocus(); break; case VGRAB_COL: vGrab.setFocus(); break; default: resetEditors(); break; } } } } }); /* Add listener to add an element to the table */ add.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { TableItem item = new TableItem(table, 0); String[] insert = new String[] { String.valueOf(table.indexOf(item)), "Button", "-1", "-1", "BEGINNING", "CENTER", "0", "1", "1", "false", "false" }; item.setText(insert); data.addElement(insert); resetEditors(); } }); } /** * Creates the control widgets. */ void createControlWidgets() { /* Rearrange the layout of the control group */ size.setLayoutData(new GridData()); /* Controls the margins and spacing of the GridLayout */ String[] marginValues = new String[] { "0", "3", "5", "10" }; Group marginGroup = new Group(controlGroup, SWT.NONE); marginGroup.setText(LayoutExample.getResourceString("Margins_Spacing")); GridData data = new GridData(GridData.FILL_HORIZONTAL); data.verticalSpan = 2; marginGroup.setLayoutData(data); GridLayout layout = new GridLayout(); layout.numColumns = 2; marginGroup.setLayout(layout); new Label(marginGroup, SWT.NONE).setText("marginHeight"); marginHeight = new Combo(marginGroup, SWT.NONE); marginHeight.setItems(marginValues); marginHeight.select(2); data = new GridData(GridData.FILL_HORIZONTAL); data.widthHint = 60; marginHeight.setLayoutData(data); marginHeight.addSelectionListener(selectionListener); marginHeight.addTraverseListener(traverseListener); new Label(marginGroup, SWT.NONE).setText("marginWidth"); marginWidth = new Combo(marginGroup, SWT.NONE); marginWidth.setItems(marginValues); marginWidth.select(2); data = new GridData(GridData.FILL_HORIZONTAL); data.widthHint = 60; marginWidth.setLayoutData(data); marginWidth.addSelectionListener(selectionListener); marginWidth.addTraverseListener(traverseListener); new Label(marginGroup, SWT.NONE).setText("horizontalSpacing"); horizontalSpacing = new Combo(marginGroup, SWT.NONE); horizontalSpacing.setItems(marginValues); horizontalSpacing.select(2); data = new GridData(GridData.FILL_HORIZONTAL); data.widthHint = 60; horizontalSpacing.setLayoutData(data); horizontalSpacing.addSelectionListener(selectionListener); horizontalSpacing.addTraverseListener(traverseListener); new Label(marginGroup, SWT.NONE).setText("verticalSpacing"); verticalSpacing = new Combo(marginGroup, SWT.NONE); verticalSpacing.setItems(marginValues); verticalSpacing.select(2); data = new GridData(GridData.FILL_HORIZONTAL); data.widthHint = 60; verticalSpacing.setLayoutData(data); verticalSpacing.addSelectionListener(selectionListener); verticalSpacing.addTraverseListener(traverseListener); /* Controls the columns in the GridLayout */ Group columnGroup = new Group(controlGroup, SWT.NONE); columnGroup.setText(LayoutExample.getResourceString("Columns")); layout = new GridLayout(); layout.numColumns = 2; columnGroup.setLayout(layout); data = new GridData(GridData.VERTICAL_ALIGN_FILL); columnGroup.setLayoutData(data); numColumns = new Text(columnGroup, SWT.BORDER); numColumns.setText("1"); numColumns.addSelectionListener(selectionListener); numColumns.addTraverseListener(traverseListener); data = new GridData(GridData.FILL_HORIZONTAL); data.widthHint = 15; numColumns.setLayoutData(data); new Label(columnGroup, SWT.NONE).setText("numColumns"); makeColumnsEqualWidth = new Button(columnGroup, SWT.CHECK); makeColumnsEqualWidth.setText("makeColumnsEqualWidth"); makeColumnsEqualWidth.addSelectionListener(selectionListener); data = new GridData(GridData.FILL_HORIZONTAL); data.horizontalSpan = 2; data.horizontalIndent = 14; makeColumnsEqualWidth.setLayoutData(data); /* Add common controls */ super.createControlWidgets(); controlGroup.pack(); } /** * Creates the example layout. */ void createLayout() { gridLayout = new GridLayout(); layoutComposite.setLayout(gridLayout); } /** * Disposes the editors without placing their contents into the table. */ void disposeEditors() { comboEditor.setEditor(null, null, -1); combo.dispose(); widthText.dispose(); heightText.dispose(); hAlign.dispose(); vAlign.dispose(); hIndent.dispose(); hSpan.dispose(); vSpan.dispose(); hGrab.dispose(); vGrab.dispose(); } /** * Generates code for the example layout. */ StringBuffer generateLayoutCode() { StringBuffer code = new StringBuffer(); code.append("\t\tGridLayout gridLayout = new GridLayout ();\n"); if (gridLayout.numColumns != 1) { code.append("\t\tgridLayout.numColumns = " + gridLayout.numColumns + ";\n"); } if (gridLayout.makeColumnsEqualWidth) { code.append("\t\tgridLayout.makeColumnsEqualWidth = true;\n"); } if (gridLayout.marginHeight != 5) { code.append("\t\tgridLayout.marginHeight = " + gridLayout.marginHeight + ";\n"); } if (gridLayout.marginWidth != 5) { code.append("\t\tgridLayout.marginWidth = " + gridLayout.marginWidth + ";\n"); } if (gridLayout.horizontalSpacing != 5) { code.append("\t\tgridLayout.horizontalSpacing = " + gridLayout.horizontalSpacing + ";\n"); } if (gridLayout.verticalSpacing != 5) { code.append("\t\tgridLayout.verticalSpacing = " + gridLayout.verticalSpacing + ";\n"); } code.append("\t\tshell.setLayout (gridLayout);\n"); boolean first = true; for (int i = 0; i < children.length; i++) { Control control = children[i]; code.append(getChildCode(control, i)); GridData data = (GridData) control.getLayoutData(); if (data != null) { code.append("\t\t"); if (first) { code.append("GridData "); first = false; } code.append("data = new GridData ();\n"); if (data.widthHint != SWT.DEFAULT) { code.append("\t\tdata.widthHint = " + data.widthHint + ";\n"); } if (data.heightHint != SWT.DEFAULT) { code.append("\t\tdata.heightHint = " + data.heightHint + ";\n"); } if (data.horizontalAlignment != GridData.BEGINNING) { String alignment; int hAlignment = data.horizontalAlignment; if (hAlignment == GridData.CENTER) alignment = "GridData.CENTER"; else if (hAlignment == GridData.END) alignment = "GridData.END"; else alignment = "GridData.FILL"; code.append("\t\tdata.horizontalAlignment = " + alignment + ";\n"); } if (data.verticalAlignment != GridData.CENTER) { String alignment; int vAlignment = data.verticalAlignment; if (vAlignment == GridData.BEGINNING) alignment = "GridData.BEGINNING"; else if (vAlignment == GridData.END) alignment = "GridData.END"; else alignment = "GridData.FILL"; code.append("\t\tdata.verticalAlignment = " + alignment + ";\n"); } if (data.horizontalIndent != 0) { code.append("\t\tdata.horizontalIndent = " + data.horizontalIndent + ";\n"); } if (data.horizontalSpan != 1) { code.append("\t\tdata.horizontalSpan = " + data.horizontalSpan + ";\n"); } if (data.verticalSpan != 1) { code.append("\t\tdata.verticalSpan = " + data.verticalSpan + ";\n"); } if (data.grabExcessHorizontalSpace) { code.append("\t\tdata.grabExcessHorizontalSpace = true;\n"); } if (data.grabExcessVerticalSpace) { code.append("\t\tdata.grabExcessVerticalSpace = true;\n"); } if (code.substring(code.length() - 33).equals( "GridData data = new GridData ();\n")) { code.delete(code.length() - 33, code.length()); first = true; } else if (code.substring(code.length() - 24).equals( "data = new GridData ();\n")) { code.delete(code.length() - 24, code.length()); } else { code.append("\t\t" + names[i] + ".setLayoutData (data);\n"); } } } return code; } /** * Returns the layout data field names. */ String[] getLayoutDataFieldNames() { return new String[] { "", "Control", "width", "height", "horizontalAlignment", "verticalAlignment", "horizontalIndent", "horizontalSpan", "verticalSpan", "grabExcessHorizontalSpace", "grabExcessVerticalSpace" }; } /** * Gets the text for the tab folder item. */ String getTabText() { return "GridLayout"; } /** * Takes information from TableEditors and stores it. */ void resetEditors() { resetEditors(false); } void resetEditors(boolean tab) { TableItem oldItem = comboEditor.getItem(); if (oldItem != null) { int row = table.indexOf(oldItem); try { new Integer(widthText.getText()).intValue(); } catch (NumberFormatException e) { widthText.setText(oldItem.getText(WIDTH_COL)); } try { new Integer(heightText.getText()).intValue(); } catch (NumberFormatException e) { heightText.setText(oldItem.getText(HEIGHT_COL)); } try { new Integer(hIndent.getText()).intValue(); } catch (NumberFormatException e) { hIndent.setText(oldItem.getText(HINDENT_COL)); } try { new Integer(hSpan.getText()).intValue(); } catch (NumberFormatException e) { hSpan.setText(oldItem.getText(HSPAN_COL)); } try { new Integer(vSpan.getText()).intValue(); } catch (NumberFormatException e) { vSpan.setText(oldItem.getText(VSPAN_COL)); } String[] insert = new String[] { String.valueOf(row), combo.getText(), widthText.getText(), heightText.getText(), hAlign.getText(), vAlign.getText(), hIndent.getText(), hSpan.getText(), vSpan.getText(), hGrab.getText(), vGrab.getText() }; data.setElementAt(insert, row); for (int i = 0; i < TOTAL_COLS; i++) { oldItem.setText(i, ((String[]) data.elementAt(row))[i]); } if (!tab) disposeEditors(); } setLayoutState(); refreshLayoutComposite(); setLayoutData(); layoutComposite.layout(true); layoutGroup.layout(true); } /** * Sets the layout data for the children of the layout. */ void setLayoutData() { Control[] children = layoutComposite.getChildren(); TableItem[] items = table.getItems(); GridData data; int hIndent, hSpan, vSpan; String vAlign, hAlign, vGrab, hGrab; for (int i = 0; i < children.length; i++) { data = new GridData(); /* Set widthHint and heightHint */ data.widthHint = new Integer(items[i].getText(WIDTH_COL)) .intValue(); data.heightHint = new Integer(items[i].getText(HEIGHT_COL)) .intValue(); /* Set vertical alignment and horizontal alignment */ hAlign = items[i].getText(HALIGN_COL); if (hAlign.equals("CENTER")) { data.horizontalAlignment = GridData.CENTER; } else if (hAlign.equals("END")) { data.horizontalAlignment = GridData.END; } else if (hAlign.equals("FILL")) { data.horizontalAlignment = GridData.FILL; } else { data.horizontalAlignment = GridData.BEGINNING; } vAlign = items[i].getText(VALIGN_COL); if (vAlign.equals("BEGINNING")) { data.verticalAlignment = GridData.BEGINNING; } else if (vAlign.equals("END")) { data.verticalAlignment = GridData.END; } else if (vAlign.equals("FILL")) { data.verticalAlignment = GridData.FILL; } else { data.verticalAlignment = GridData.CENTER; } /* Set indents and spans */ hIndent = new Integer(items[i].getText(HINDENT_COL)).intValue(); data.horizontalIndent = hIndent; hSpan = new Integer(items[i].getText(HSPAN_COL)).intValue(); data.horizontalSpan = hSpan; vSpan = new Integer(items[i].getText(VSPAN_COL)).intValue(); data.verticalSpan = vSpan; /* Set grabbers */ hGrab = items[i].getText(HGRAB_COL); if (hGrab.equals("true")) { data.grabExcessHorizontalSpace = true; } else { data.grabExcessHorizontalSpace = false; } vGrab = items[i].getText(VGRAB_COL); if (vGrab.equals("true")) { data.grabExcessVerticalSpace = true; } else { data.grabExcessVerticalSpace = false; } children[i].setLayoutData(data); } } /** * Sets the state of the layout. */ void setLayoutState() { /* Set the columns for the layout */ try { gridLayout.numColumns = new Integer(numColumns.getText()) .intValue(); } catch (NumberFormatException e) { gridLayout.numColumns = 1; } gridLayout.makeColumnsEqualWidth = makeColumnsEqualWidth.getSelection(); /* Set the margins and spacing */ try { gridLayout.marginHeight = new Integer(marginHeight.getText()) .intValue(); } catch (NumberFormatException e) { gridLayout.marginHeight = 5; marginHeight.select(2); } try { gridLayout.marginWidth = new Integer(marginWidth.getText()) .intValue(); } catch (NumberFormatException e) { gridLayout.marginWidth = 5; marginWidth.select(2); } try { gridLayout.horizontalSpacing = new Integer(horizontalSpacing .getText()).intValue(); } catch (NumberFormatException e) { gridLayout.horizontalSpacing = 5; horizontalSpacing.select(2); } try { gridLayout.verticalSpacing = new Integer(verticalSpacing.getText()) .intValue(); } catch (NumberFormatException e) { gridLayout.verticalSpacing = 5; verticalSpacing.select(2); } }
} /*******************************************************************************
* Copyright (c) 2000, 2003 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 ******************************************************************************/
class FormLayoutTab extends Tab {
/* Controls for setting layout parameters */
Combo marginHeight, marginWidth;
/* The example layout instance */
FormLayout formLayout;
/* TableEditors and related controls */
TableEditor comboEditor, widthEditor, heightEditor;
TableEditor leftEditor, rightEditor, topEditor, bottomEditor;
CCombo combo;
Text widthText, heightText;
Button leftAttach, rightAttach, topAttach, bottomAttach;
/* Constants */
final int COMBO_COL = 1;
final int WIDTH_COL = 2;
final int HEIGHT_COL = 3;
final int LEFT_COL = 4;
final int RIGHT_COL = 5;
final int TOP_COL = 6;
final int BOTTOM_COL = 7;
final int MODIFY_COLS = 4; // The number of columns with combo or text
// editors
final int TOTAL_COLS = 8;
/**
* Creates the Tab within a given instance of LayoutExample.
*/
FormLayoutTab(LayoutExample instance) {
super(instance);
}
/**
* Returns the constant for the alignment for an attachment given a string.
*/
int alignmentConstant(String align) {
if (align.equals("LEFT"))
return SWT.LEFT;
if (align.equals("RIGHT"))
return SWT.RIGHT;
if (align.equals("TOP"))
return SWT.TOP;
if (align.equals("BOTTOM"))
return SWT.BOTTOM;
if (align.equals("CENTER"))
return SWT.CENTER;
return SWT.DEFAULT;
}
/**
* Returns a string representing the alignment for an attachment given a
* constant.
*/
String alignmentString(int align) {
switch (align) {
case SWT.LEFT:
return "LEFT";
case SWT.RIGHT:
return "RIGHT";
case SWT.TOP:
return "TOP";
case SWT.BOTTOM:
return "BOTTOM";
case SWT.CENTER:
return "CENTER";
}
return "DEFAULT";
}
/**
* Update the attachment field in case the type of control has changed.
*/
String checkAttachment(String oldAttach, FormAttachment newAttach) {
String controlClass = newAttach.control.getClass().toString();
String controlType = controlClass.substring(controlClass
.lastIndexOf(".") + 1);
int i = 0;
while (i < oldAttach.length()
&& !Character.isDigit(oldAttach.charAt(i))) {
i++;
}
String index = oldAttach.substring(i, oldAttach.indexOf(","));
return controlType + index + "," + newAttach.offset + ":"
+ alignmentString(newAttach.alignment);
}
/**
* Creates the widgets in the "child" group.
*/
void createChildWidgets() {
/* Add common controls */
super.createChildWidgets();
/* Resize the columns */
table.getColumn(LEFT_COL).setWidth(100);
table.getColumn(RIGHT_COL).setWidth(100);
table.getColumn(TOP_COL).setWidth(100);
table.getColumn(BOTTOM_COL).setWidth(100);
/* Add TableEditors */
comboEditor = new TableEditor(table);
widthEditor = new TableEditor(table);
heightEditor = new TableEditor(table);
leftEditor = new TableEditor(table);
rightEditor = new TableEditor(table);
topEditor = new TableEditor(table);
bottomEditor = new TableEditor(table);
table.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
resetEditors();
index = table.getSelectionIndex();
Point pt = new Point(e.x, e.y);
newItem = table.getItem(pt);
if (newItem == null)
return;
TableItem oldItem = comboEditor.getItem();
if (newItem == oldItem || newItem != lastSelected) {
lastSelected = newItem;
return;
}
table.showSelection();
combo = new CCombo(table, SWT.READ_ONLY);
createComboEditor(combo, comboEditor);
widthText = new Text(table, SWT.SINGLE);
widthText
.setText(((String[]) data.elementAt(index))[WIDTH_COL]);
createTextEditor(widthText, widthEditor, WIDTH_COL);
heightText = new Text(table, SWT.SINGLE);
heightText
.setText(((String[]) data.elementAt(index))[HEIGHT_COL]);
createTextEditor(heightText, heightEditor, HEIGHT_COL);
leftAttach = new Button(table, SWT.PUSH);
leftAttach.setText(LayoutExample
.getResourceString("Attach_Edit"));
leftEditor.horizontalAlignment = SWT.LEFT;
leftEditor.grabHorizontal = true;
leftEditor.minimumWidth = leftAttach.ruputeSize(SWT.DEFAULT,
SWT.DEFAULT).x;
leftEditor.setEditor(leftAttach, newItem, LEFT_COL);
leftAttach.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Shell shell = tabFolderPage.getShell();
AttachDialog dialog = new AttachDialog(shell);
dialog.setText(LayoutExample
.getResourceString("Left_Attachment"));
dialog.setColumn(LEFT_COL);
String attach = dialog.open();
newItem.setText(LEFT_COL, attach);
resetEditors();
}
});
rightAttach = new Button(table, SWT.PUSH);
rightAttach.setText(LayoutExample
.getResourceString("Attach_Edit"));
rightEditor.horizontalAlignment = SWT.LEFT;
rightEditor.grabHorizontal = true;
rightEditor.minimumWidth = rightAttach.ruputeSize(SWT.DEFAULT,
SWT.DEFAULT).x;
rightEditor.setEditor(rightAttach, newItem, RIGHT_COL);
rightAttach.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Shell shell = tabFolderPage.getShell();
AttachDialog dialog = new AttachDialog(shell);
dialog.setText(LayoutExample
.getResourceString("Right_Attachment"));
dialog.setColumn(RIGHT_COL);
String attach = dialog.open();
newItem.setText(RIGHT_COL, attach);
if (newItem.getText(LEFT_COL).endsWith(")"))
newItem.setText(LEFT_COL, "");
resetEditors();
}
});
topAttach = new Button(table, SWT.PUSH);
topAttach.setText(LayoutExample
.getResourceString("Attach_Edit"));
topEditor.horizontalAlignment = SWT.LEFT;
topEditor.grabHorizontal = true;
topEditor.minimumWidth = topAttach.ruputeSize(SWT.DEFAULT,
SWT.DEFAULT).x;
topEditor.setEditor(topAttach, newItem, TOP_COL);
topAttach.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Shell shell = tabFolderPage.getShell();
AttachDialog dialog = new AttachDialog(shell);
dialog.setText(LayoutExample
.getResourceString("Top_Attachment"));
dialog.setColumn(TOP_COL);
String attach = dialog.open();
newItem.setText(TOP_COL, attach);
resetEditors();
}
});
bottomAttach = new Button(table, SWT.PUSH);
bottomAttach.setText(LayoutExample
.getResourceString("Attach_Edit"));
bottomEditor.horizontalAlignment = SWT.LEFT;
bottomEditor.grabHorizontal = true;
bottomEditor.minimumWidth = bottomAttach.ruputeSize(
SWT.DEFAULT, SWT.DEFAULT).x;
bottomEditor.setEditor(bottomAttach, newItem, BOTTOM_COL);
bottomAttach.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Shell shell = tabFolderPage.getShell();
AttachDialog dialog = new AttachDialog(shell);
dialog.setText(LayoutExample
.getResourceString("Bottom_Attachment"));
dialog.setColumn(BOTTOM_COL);
String attach = dialog.open();
newItem.setText(BOTTOM_COL, attach);
if (newItem.getText(TOP_COL).endsWith(")"))
newItem.setText(TOP_COL, "");
resetEditors();
}
});
for (int i = 0; i < table.getColumnCount(); i++) {
Rectangle rect = newItem.getBounds(i);
if (rect.contains(pt)) {
switch (i) {
case 0:
resetEditors();
break;
case COMBO_COL:
combo.setFocus();
break;
case WIDTH_COL:
widthText.setFocus();
break;
case HEIGHT_COL:
heightText.setFocus();
break;
default:
break;
}
}
}
}
});
/* Add listener to add an element to the table */
add.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
TableItem item = new TableItem(table, 0);
String[] insert = new String[] {
String.valueOf(table.indexOf(item)),
"Button",
"-1",
"-1",
"0,0 (" + LayoutExample.getResourceString("Default")
+ ")",
"",
"0,0 (" + LayoutExample.getResourceString("Default")
+ ")", "" };
item.setText(insert);
data.addElement(insert);
resetEditors();
}
});
}
/**
* Creates the control widgets.
*/
void createControlWidgets() {
/* Controls the margins and spacing of the FormLayout */
String[] marginValues = new String[] { "0", "3", "5", "10" };
Group marginGroup = new Group(controlGroup, SWT.NONE);
marginGroup.setText(LayoutExample.getResourceString("Margins"));
GridLayout layout = new GridLayout();
layout.numColumns = 2;
marginGroup.setLayout(layout);
marginGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
new Label(marginGroup, SWT.NONE).setText("marginHeight");
marginHeight = new Combo(marginGroup, SWT.NONE);
marginHeight.setItems(marginValues);
marginHeight.select(0);
marginHeight.addSelectionListener(selectionListener);
marginHeight.addTraverseListener(traverseListener);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = 60;
marginHeight.setLayoutData(data);
new Label(marginGroup, SWT.NONE).setText("marginWidth");
marginWidth = new Combo(marginGroup, SWT.NONE);
marginWidth.setItems(marginValues);
marginWidth.select(0);
marginWidth.addSelectionListener(selectionListener);
marginWidth.addTraverseListener(traverseListener);
data = new GridData(GridData.FILL_HORIZONTAL);
data.widthHint = 60;
marginWidth.setLayoutData(data);
/* Add common controls */
super.createControlWidgets();
/* Position the sash */
sash.setWeights(new int[] { 6, 4 });
}
/**
* Creates the example layout.
*/
void createLayout() {
formLayout = new FormLayout();
layoutComposite.setLayout(formLayout);
}
/**
* Disposes the editors without placing their contents into the table.
*/
void disposeEditors() {
comboEditor.setEditor(null, null, -1);
combo.dispose();
widthText.dispose();
heightText.dispose();
leftAttach.dispose();
rightAttach.dispose();
topAttach.dispose();
bottomAttach.dispose();
}
/**
* Generates code for the example layout.
*/
StringBuffer generateLayoutCode() {
StringBuffer code = new StringBuffer();
code.append("\t\tFormLayout formLayout = new FormLayout ();\n");
if (formLayout.marginHeight != 0) {
code.append("\t\tformLayout.marginHeight = "
+ formLayout.marginHeight + ";\n");
}
if (formLayout.marginWidth != 0) {
code.append("\t\tformLayout.marginWidth = "
+ formLayout.marginWidth + ";\n");
}
code.append("\t\tshell.setLayout (formLayout);\n");
boolean first = true;
for (int i = 0; i < children.length; i++) {
Control control = children[i];
code.append(getChildCode(control, i));
FormData data = (FormData) control.getLayoutData();
if (data != null) {
code.append("\t\t");
if (first) {
code.append("FormData ");
first = false;
}
code.append("data = new FormData ();\n");
if (data.width != SWT.DEFAULT) {
code.append("\t\tdata.width = " + data.width + ";\n");
}
if (data.height != SWT.DEFAULT) {
code.append("\t\tdata.height = " + data.height + ";\n");
}
if (data.left != null) {
if (data.left.control != null) {
TableItem item = table.getItem(i);
String controlString = item.getText(LEFT_COL);
int index = new Integer(controlString.substring(
controlString.indexOf(",") - 1, controlString
.indexOf(","))).intValue();
code
.append("\t\tdata.left = new FormAttachment ("
+ names[index] + ", "
+ data.left.offset + ", SWT."
+ alignmentString(data.left.alignment)
+ ");\n");
} else {
if (data.right != null
|| (data.left.numerator != 0 || data.left.offset != 0)) {
code.append("\t\tdata.left = new FormAttachment ("
+ data.left.numerator + ", "
+ data.left.offset + ");\n");
}
}
}
if (data.right != null) {
if (data.right.control != null) {
TableItem item = table.getItem(i);
String controlString = item.getText(RIGHT_COL);
int index = new Integer(controlString.substring(
controlString.indexOf(",") - 1, controlString
.indexOf(","))).intValue();
code.append("\t\tdata.right = new FormAttachment ("
+ names[index] + ", " + data.right.offset
+ ", SWT."
+ alignmentString(data.right.alignment)
+ ");\n");
} else {
code.append("\t\tdata.right = new FormAttachment ("
+ data.right.numerator + ", "
+ data.right.offset + ");\n");
}
}
if (data.top != null) {
if (data.top.control != null) {
TableItem item = table.getItem(i);
String controlString = item.getText(TOP_COL);
int index = new Integer(controlString.substring(
controlString.indexOf(",") - 1, controlString
.indexOf(","))).intValue();
code.append("\t\tdata.top = new FormAttachment ("
+ names[index] + ", " + data.top.offset
+ ", SWT."
+ alignmentString(data.top.alignment) + ");\n");
} else {
if (data.bottom != null
|| (data.top.numerator != 0 || data.top.offset != 0)) {
code.append("\t\tdata.top = new FormAttachment ("
+ data.top.numerator + ", "
+ data.top.offset + ");\n");
}
}
}
if (data.bottom != null) {
if (data.bottom.control != null) {
TableItem item = table.getItem(i);
String controlString = item.getText(BOTTOM_COL);
int index = new Integer(controlString.substring(
controlString.indexOf(",") - 1, controlString
.indexOf(","))).intValue();
code.append("\t\tdata.bottom = new FormAttachment ("
+ names[index] + ", " + data.bottom.offset
+ ", SWT."
+ alignmentString(data.bottom.alignment)
+ ");\n");
} else {
code.append("\t\tdata.bottom = new FormAttachment ("
+ data.bottom.numerator + ", "
+ data.bottom.offset + ");\n");
}
}
code.append("\t\t" + names[i] + ".setLayoutData (data);\n");
}
}
return code;
}
/**
* Returns the layout data field names.
*/
String[] getLayoutDataFieldNames() {
return new String[] { "", "Control", "width", "height", "left",
"right", "top", "bottom" };
}
/**
* Gets the text for the tab folder item.
*/
String getTabText() {
return "FormLayout";
}
/**
* Takes information from TableEditors and stores it.
*/
void resetEditors() {
resetEditors(false);
}
void resetEditors(boolean tab) {
TableItem oldItem = comboEditor.getItem();
if (oldItem != null) {
int row = table.indexOf(oldItem);
try {
new Integer(widthText.getText()).intValue();
} catch (NumberFormatException e) {
widthText.setText(oldItem.getText(WIDTH_COL));
}
try {
new Integer(heightText.getText()).intValue();
} catch (NumberFormatException e) {
heightText.setText(oldItem.getText(HEIGHT_COL));
}
String[] insert = new String[] { String.valueOf(row),
combo.getText(), widthText.getText(), heightText.getText() };
data.setElementAt(insert, row);
for (int i = 0; i < MODIFY_COLS; i++) {
oldItem.setText(i, ((String[]) data.elementAt(row))[i]);
}
if (!tab)
disposeEditors();
}
setLayoutState();
refreshLayoutComposite();
setLayoutData();
layoutComposite.layout(true);
layoutGroup.layout(true);
}
/**
* Sets an attachment to the edge of a widget using the information in the
* table.
*/
FormAttachment setAttachment(String attachment) {
String control, align;
int position, offset;
int comma = attachment.indexOf(",");
char first = attachment.charAt(0);
if (Character.isLetter(first)) {
/* Case where there is a control */
control = attachment.substring(0, comma);
int i = 0;
while (i < control.length()
&& !Character.isDigit(control.charAt(i))) {
i++;
}
String end = control.substring(i);
int index = new Integer(end).intValue();
Control attachControl = children[index];
int colon = attachment.indexOf(":");
try {
offset = new Integer(attachment.substring(comma + 1, colon))
.intValue();
} catch (NumberFormatException e) {
offset = 0;
}
align = attachment.substring(colon + 1);
return new FormAttachment(attachControl, offset,
alignmentConstant(align));
} else {
/* Case where there is a position */
try {
position = new Integer(attachment.substring(0, comma))
.intValue();
} catch (NumberFormatException e) {
position = 0;
}
try {
offset = new Integer(attachment.substring(comma + 1))
.intValue();
} catch (NumberFormatException e) {
offset = 0;
}
return new FormAttachment(position, offset);
}
}
/**
* Sets the layout data for the children of the layout.
*/
void setLayoutData() {
Control[] children = layoutComposite.getChildren();
TableItem[] items = table.getItems();
FormData data;
int width, height;
String left, right, top, bottom;
for (int i = 0; i < children.length; i++) {
width = new Integer(items[i].getText(WIDTH_COL)).intValue();
height = new Integer(items[i].getText(HEIGHT_COL)).intValue();
data = new FormData();
if (width > 0)
data.width = width;
if (height > 0)
data.height = height;
left = items[i].getText(LEFT_COL);
if (left.length() > 0) {
data.left = setAttachment(left);
if (data.left.control != null) {
String attachment = checkAttachment(left, data.left);
items[i].setText(LEFT_COL, attachment);
}
}
right = items[i].getText(RIGHT_COL);
if (right.length() > 0) {
data.right = setAttachment(right);
if (data.right.control != null) {
String attachment = checkAttachment(right, data.right);
items[i].setText(RIGHT_COL, attachment);
}
}
top = items[i].getText(TOP_COL);
if (top.length() > 0) {
data.top = setAttachment(top);
if (data.top.control != null) {
String attachment = checkAttachment(top, data.top);
items[i].setText(TOP_COL, attachment);
}
}
bottom = items[i].getText(BOTTOM_COL);
if (bottom.length() > 0) {
data.bottom = setAttachment(bottom);
if (data.bottom.control != null) {
String attachment = checkAttachment(bottom, data.bottom);
items[i].setText(BOTTOM_COL, attachment);
}
}
children[i].setLayoutData(data);
}
}
/**
* Sets the state of the layout.
*/
void setLayoutState() {
/* Set the margins and spacing */
try {
formLayout.marginHeight = new Integer(marginHeight.getText())
.intValue();
} catch (NumberFormatException e) {
formLayout.marginHeight = 0;
marginHeight.select(0);
}
try {
formLayout.marginWidth = new Integer(marginWidth.getText())
.intValue();
} catch (NumberFormatException e) {
formLayout.marginWidth = 0;
marginWidth.select(0);
}
}
/**
* AttachDialog
is the class that creates a dialog specific
* for this example. It creates a dialog with controls to set the values in
* a FormAttachment.
*/
public class AttachDialog extends Dialog {
String result = "";
String controlInput, positionInput, alignmentInput, offsetInput;
int col = 0;
public AttachDialog(Shell parent, int style) {
super(parent, style);
}
public AttachDialog(Shell parent) {
this(parent, 0);
}
public void setColumn(int col) {
this.col = col;
}
public String open() {
Shell parent = getParent();
final Shell shell = new Shell(parent, SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
shell.setText(getText());
GridLayout layout = new GridLayout();
layout.numColumns = 3;
layout.makeColumnsEqualWidth = true;
shell.setLayout(layout);
/* Find out what was previously set as an attachment */
TableItem newItem = leftEditor.getItem();
result = newItem.getText(col);
String oldAttach = result;
String oldPos = "0", oldControl = "", oldAlign = "DEFAULT", oldOffset = "0";
boolean isControl = false;
if (oldAttach.length() != 0) {
char first = oldAttach.charAt(0);
if (Character.isLetter(first)) {
/* We have a control */
isControl = true;
oldControl = oldAttach.substring(0, oldAttach.indexOf(","));
oldAlign = oldAttach.substring(oldAttach.indexOf(":") + 1);
oldOffset = oldAttach.substring(oldAttach.indexOf(",") + 1,
oldAttach.indexOf(":"));
} else {
/* We have a position */
oldPos = oldAttach.substring(0, oldAttach.indexOf(","));
oldOffset = oldAttach.substring(oldAttach.indexOf(",") + 1);
if (oldOffset.endsWith(")")) { // i.e. (Default)
oldOffset = oldOffset.substring(0, oldOffset
.indexOf(" "));
}
}
}
/* Add position field */
final Button posButton = new Button(shell, SWT.RADIO);
posButton.setText(LayoutExample.getResourceString("Position"));
posButton.setSelection(!isControl);
final Combo position = new Combo(shell, SWT.NONE);
position.setItems(new String[] { "0", "25", "50", "75", "100" });
position.setText(oldPos);
position.setEnabled(!isControl);
GridData data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
position.setLayoutData(data);
/* Add control field */
final Button contButton = new Button(shell, SWT.RADIO);
contButton.setText(LayoutExample.getResourceString("Control"));
contButton.setSelection(isControl);
final Combo control = new Combo(shell, SWT.READ_ONLY);
TableItem[] items = table.getItems();
TableItem currentItem = leftEditor.getItem();
for (int i = 0; i < table.getItemCount(); i++) {
if (items[i].getText(0).length() > 0) {
if (items[i] != currentItem) {
control.add(items[i].getText(COMBO_COL) + i);
}
}
}
if (oldControl.length() != 0)
control.setText(oldControl);
else
control.select(0);
control.setEnabled(isControl);
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
control.setLayoutData(data);
/* Add alignment field */
new Label(shell, SWT.NONE).setText(LayoutExample
.getResourceString("Alignment"));
final Combo alignment = new Combo(shell, SWT.NONE);
String[] alignmentValues;
if (col == LEFT_COL || col == RIGHT_COL) {
alignmentValues = new String[] { "SWT.LEFT", "SWT.RIGHT",
"SWT.CENTER", "SWT.DEFAULT" };
} else {
// col == TOP_COL || col == BOTTOM_COL
alignmentValues = new String[] { "SWT.TOP", "SWT.BOTTOM",
"SWT.CENTER", "SWT.DEFAULT" };
}
alignment.setItems(alignmentValues);
alignment.setText("SWT." + oldAlign);
alignment.setEnabled(isControl);
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
alignment.setLayoutData(data);
/* Add offset field */
new Label(shell, SWT.NONE).setText(LayoutExample
.getResourceString("Offset"));
final Text offset = new Text(shell, SWT.SINGLE | SWT.BORDER);
offset.setText(oldOffset);
data = new GridData(GridData.FILL_HORIZONTAL);
data.horizontalSpan = 2;
offset.setLayoutData(data);
/* Add listeners for choosing between position and control */
posButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
position.setEnabled(true);
control.setEnabled(false);
alignment.setEnabled(false);
}
});
contButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
position.setEnabled(false);
control.setEnabled(true);
alignment.setEnabled(true);
}
});
Button clear = new Button(shell, SWT.PUSH);
clear.setText(LayoutExample.getResourceString("Clear"));
clear.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
clear.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
result = "";
shell.close();
}
});
/* OK button sets data into table */
Button ok = new Button(shell, SWT.PUSH);
ok.setText(LayoutExample.getResourceString("OK"));
ok.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
ok.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
controlInput = control.getText();
alignmentInput = alignment.getText().substring(4);
positionInput = position.getText();
if (positionInput.length() == 0)
positionInput = "0";
try {
new Integer(positionInput).intValue();
} catch (NumberFormatException except) {
positionInput = "0";
}
offsetInput = offset.getText();
if (offsetInput.length() == 0)
offsetInput = "0";
try {
new Integer(offsetInput).intValue();
} catch (NumberFormatException except) {
offsetInput = "0";
}
if (posButton.getSelection() || controlInput.length() == 0) {
result = positionInput + "," + offsetInput;
} else {
result = controlInput + "," + offsetInput + ":"
+ alignmentInput;
}
shell.close();
}
});
Button cancel = new Button(shell, SWT.PUSH);
cancel.setText(LayoutExample.getResourceString("Cancel"));
cancel.setLayoutData(new GridData(
GridData.HORIZONTAL_ALIGN_BEGINNING));
cancel.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
shell.close();
}
});
shell.setDefaultButton(ok);
shell.pack();
/* Center the dialog */
Point center = parent.getLocation();
center.x = center.x + (parent.getBounds().width / 2)
- (shell.getBounds().width / 2);
center.y = center.y + (parent.getBounds().height / 2)
- (shell.getBounds().height / 2);
shell.setLocation(center);
shell.open();
Display display = shell.getDisplay();
while (!shell.isDisposed()) {
if (display.readAndDispatch())
display.sleep();
}
return result;
}
}
} /*******************************************************************************
* Copyright (c) 2000, 2003 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 ******************************************************************************/
class FillLayoutTab extends Tab {
/* Controls for setting layout parameters */ Button horizontal, vertical; /* The example layout instance */ FillLayout fillLayout; /* TableEditors and related controls */ TableEditor comboEditor; CCombo combo; /** * Creates the Tab within a given instance of LayoutExample. */ FillLayoutTab(LayoutExample instance) { super(instance); } /** * Creates the widgets in the "child" group. */ void createChildWidgets() { /* Add common controls */ super.createChildWidgets(); /* Add TableEditors */ comboEditor = new TableEditor(table); table.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { resetEditors(); index = table.getSelectionIndex(); if (index == -1) return; TableItem oldItem = comboEditor.getItem(); newItem = table.getItem(index); if (newItem == oldItem || newItem != lastSelected) { lastSelected = newItem; return; } table.showSelection(); combo = new CCombo(table, SWT.READ_ONLY); createComboEditor(combo, comboEditor); } }); /* Add listener to add an element to the table */ add.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { TableItem item = new TableItem(table, 0); item.setText(0, String.valueOf(table.indexOf(item))); item.setText(1, "Button"); data.addElement("Button"); resetEditors(); } }); } /** * Creates the control widgets. */ void createControlWidgets() { /* Controls the type of FillLayout */ Group typeGroup = new Group(controlGroup, SWT.NONE); typeGroup.setText(LayoutExample.getResourceString("Type")); typeGroup.setLayout(new GridLayout()); typeGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); horizontal = new Button(typeGroup, SWT.RADIO); horizontal.setText("SWT.HORIZONTAL"); horizontal.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); horizontal.setSelection(true); horizontal.addSelectionListener(selectionListener); vertical = new Button(typeGroup, SWT.RADIO); vertical.setText("SWT.VERTICAL"); vertical.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); vertical.addSelectionListener(selectionListener); /* Add common controls */ super.createControlWidgets(); /* Position the sash */ sash.setWeights(new int[] { 4, 1 }); } /** * Creates the example layout. */ void createLayout() { fillLayout = new FillLayout(); layoutComposite.setLayout(fillLayout); } /** * Disposes the editors without placing their contents into the table. */ void disposeEditors() { comboEditor.setEditor(null, null, -1); combo.dispose(); } /** * Generates code for the example layout. */ StringBuffer generateLayoutCode() { StringBuffer code = new StringBuffer(); code.append("\t\tFillLayout fillLayout = new FillLayout ();\n"); if (fillLayout.type == SWT.VERTICAL) { code.append("\t\tfillLayout.type = SWT.VERTICAL;\n"); } code.append("\t\tshell.setLayout (fillLayout);\n"); for (int i = 0; i < children.length; i++) { Control control = children[i]; code.append(getChildCode(control, i)); } return code; } /** * Returns the layout data field names. */ String[] getLayoutDataFieldNames() { return new String[] { "", "Control" }; } /** * Gets the text for the tab folder item. */ String getTabText() { return "FillLayout"; } /** * Takes information from TableEditors and stores it. */ void resetEditors() { TableItem oldItem = comboEditor.getItem(); comboEditor.setEditor(null, null, -1); if (oldItem != null) { int row = table.indexOf(oldItem); data.insertElementAt(combo.getText(), row); oldItem.setText(1, data.elementAt(row).toString()); combo.dispose(); } setLayoutState(); refreshLayoutComposite(); layoutComposite.layout(true); layoutGroup.layout(true); } /** * Sets the state of the layout. */ void setLayoutState() { if (vertical.getSelection()) { fillLayout.type = SWT.VERTICAL; } else { fillLayout.type = SWT.HORIZONTAL; } }
}
</source>
SWT RowLayout
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowData; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ruposite; public class Ch6RowLayoutComposite extends Composite {
public Ch6RowLayoutComposite(Composite parent) { super(parent, SWT.NONE); RowLayout layout = new RowLayout(SWT.HORIZONTAL); setLayout(layout); for (int i = 0; i < 16; ++i) { Button button = new Button(this, SWT.NONE); button.setText("Sample Text"); button.setLayoutData(new RowData(200 + 5 * i, 20 + i)); } }
}
</source>
SWT RowLayout Sample
<source lang="java">
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie * All right reserved. * * Created on Jan 29, 2004 12:29:17 AM by JACK * $Id$ * * visit: http://www.asprise.ru/swt *****************************************************************************/
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowData; import org.eclipse.swt.layout.RowLayout; 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 RowLayoutSample {
Display display = new Display(); Shell shell = new Shell(display); public RowLayoutSample() { RowLayout rowLayout = new RowLayout(); //rowLayout.fill = true; //rowLayout.justify = true; //rowLayout.pack = false; //rowLayout.type = SWT.VERTICAL; //rowLayout.wrap = false; shell.setLayout(rowLayout); Button button1 = new Button(shell, SWT.PUSH); button1.setText("button1"); button1.setLayoutData(new RowData(100, 35)); 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(120, 120); shell.pack(); shell.open(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); } private void init() { } public static void main(String[] args) { new RowLayoutSample(); }
}
</source>
SWY Radial Layout
<source lang="java">
/* SWT/JFace in Action GUI Design with Eclipse 3.0 Matthew Scarpino, Stephen Holder, Stanford Ng, and Laurent Mihalkovic ISBN: 1932394273 Publisher: Manning
- /
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Layout; public class RadialLayout extends Layout {
public RadialLayout() { super(); } protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { Point maxDimensions = calculateMaxDimensions(composite.getChildren()); int stepsPerHemisphere = stepsPerHemisphere(composite.getChildren().length); int maxWidth = maxDimensions.x; int maxHeight = maxDimensions.y; int dimensionMultiplier = (stepsPerHemisphere + 1); int controlWidth = maxWidth * dimensionMultiplier; int controlHeight = maxHeight * dimensionMultiplier; int diameter = Math.max(controlWidth, controlHeight); Point preferredSize = new Point(diameter, diameter); if (wHint != SWT.DEFAULT) { if (preferredSize.x > wHint) { preferredSize.x = wHint; } } if (hHint != SWT.DEFAULT) { if (preferredSize.y > hHint) { preferredSize.y = hHint; } } return preferredSize; } protected void layout(Composite composite, boolean flushCache) { Point[] positions = calculateControlPositions(composite); Control[] controls = composite.getChildren(); for (int i = 0; i < controls.length; i++) { Point preferredSize = controls[i].ruputeSize(SWT.DEFAULT, SWT.DEFAULT); controls[i].setBounds(positions[i].x, positions[i].y, preferredSize.x, preferredSize.y); } } private Point[] calculateControlPositions(Composite composite) { int controlCount = composite.getChildren().length; int stepsPerHemisphere = stepsPerHemisphere(controlCount); Point[] positions = new Point[controlCount]; Point maxControlDimensions = calculateMaxDimensions(composite .getChildren()); int maxControlWidth = maxControlDimensions.x; Rectangle clientArea = composite.getClientArea(); int smallestDimension = Math.min(clientArea.width, clientArea.height); int radius = (smallestDimension / 2) - maxControlWidth; Point center = new Point(clientArea.width / 2, clientArea.height / 2); long radiusSquared = radius * radius; int stepXDistance = calculateStepDistance(radius * 2, stepsPerHemisphere); int signMultiplier = 1; int x = -radius; int y; Control[] controls = composite.getChildren(); for (int i = 0; i < controlCount; i++) { Point currSize = controls[i].getSize(); long xSquared = x * x; int sqrRoot = (int) Math.sqrt(radiusSquared - xSquared); y = signMultiplier * sqrRoot; int translatedX = x + center.x; int translatedY = y + center.y; positions[i] = new Point(translatedX - (currSize.x / 2), translatedY - (currSize.y / 2)); x = x + (signMultiplier * stepXDistance); //we"ve finished the upper hemisphere, now do the lower if (x >= radius) { x = radius - (x - radius); signMultiplier = -1; } } return positions; } private Point calculateMaxDimensions(Control[] controls) { Point maxes = new Point(0, 0); for (int i = 0; i < controls.length; i++) { Point controlSize = controls[i].ruputeSize(SWT.DEFAULT, SWT.DEFAULT); maxes.x = Math.max(maxes.x, controlSize.x); maxes.y = Math.max(maxes.y, controlSize.y); } return maxes; } private int stepsPerHemisphere(int totalObjects) { return (totalObjects / 2) - 1; } private int calculateStepDistance(int clientAreaDimensionSize, int stepCount) { return clientAreaDimensionSize / (stepCount + 1); }
}
</source>
Use of Radial Layout
<source lang="java">
/* SWT/JFace in Action GUI Design with Eclipse 3.0 Matthew Scarpino, Stephen Holder, Stanford Ng, and Laurent Mihalkovic ISBN: 1932394273 Publisher: Manning
- /
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Layout; public class Ch6RadialLayoutComposite extends Composite {
public Ch6RadialLayoutComposite(Composite parent) { super(parent, SWT.NONE); setLayout(new RadialLayout()); for (int i = 0; i < 8; i++) { Button b = new Button(this, SWT.NONE); b.setText("Cell " + (i + 1)); } }
} class RadialLayout extends Layout {
public RadialLayout() { super(); } protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { Point maxDimensions = calculateMaxDimensions(composite.getChildren()); int stepsPerHemisphere = stepsPerHemisphere(composite.getChildren().length); int maxWidth = maxDimensions.x; int maxHeight = maxDimensions.y; int dimensionMultiplier = (stepsPerHemisphere + 1); int controlWidth = maxWidth * dimensionMultiplier; int controlHeight = maxHeight * dimensionMultiplier; int diameter = Math.max(controlWidth, controlHeight); Point preferredSize = new Point(diameter, diameter); if (wHint != SWT.DEFAULT) { if (preferredSize.x > wHint) { preferredSize.x = wHint; } } if (hHint != SWT.DEFAULT) { if (preferredSize.y > hHint) { preferredSize.y = hHint; } } return preferredSize; } protected void layout(Composite composite, boolean flushCache) { Point[] positions = calculateControlPositions(composite); Control[] controls = composite.getChildren(); for (int i = 0; i < controls.length; i++) { Point preferredSize = controls[i].ruputeSize(SWT.DEFAULT, SWT.DEFAULT); controls[i].setBounds(positions[i].x, positions[i].y, preferredSize.x, preferredSize.y); } } private Point[] calculateControlPositions(Composite composite) { int controlCount = composite.getChildren().length; int stepsPerHemisphere = stepsPerHemisphere(controlCount); Point[] positions = new Point[controlCount]; Point maxControlDimensions = calculateMaxDimensions(composite .getChildren()); int maxControlWidth = maxControlDimensions.x; Rectangle clientArea = composite.getClientArea(); int smallestDimension = Math.min(clientArea.width, clientArea.height); int radius = (smallestDimension / 2) - maxControlWidth; Point center = new Point(clientArea.width / 2, clientArea.height / 2); long radiusSquared = radius * radius; int stepXDistance = calculateStepDistance(radius * 2, stepsPerHemisphere); int signMultiplier = 1; int x = -radius; int y; Control[] controls = composite.getChildren(); for (int i = 0; i < controlCount; i++) { Point currSize = controls[i].getSize(); long xSquared = x * x; int sqrRoot = (int) Math.sqrt(radiusSquared - xSquared); y = signMultiplier * sqrRoot; int translatedX = x + center.x; int translatedY = y + center.y; positions[i] = new Point(translatedX - (currSize.x / 2), translatedY - (currSize.y / 2)); x = x + (signMultiplier * stepXDistance); //we"ve finished the upper hemisphere, now do the lower if (x >= radius) { x = radius - (x - radius); signMultiplier = -1; } } return positions; } private Point calculateMaxDimensions(Control[] controls) { Point maxes = new Point(0, 0); for (int i = 0; i < controls.length; i++) { Point controlSize = controls[i].ruputeSize(SWT.DEFAULT, SWT.DEFAULT); maxes.x = Math.max(maxes.x, controlSize.x); maxes.y = Math.max(maxes.y, controlSize.y); } return maxes; } private int stepsPerHemisphere(int totalObjects) { return (totalObjects / 2) - 1; } private int calculateStepDistance(int clientAreaDimensionSize, int stepCount) { return clientAreaDimensionSize / (stepCount + 1); }
}
</source>