Home   Notes   Contact Me

SWT

Local

External


Example Tiny SWT Program

package com.erdas.experimental; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Layout; import org.eclipse.swt.widgets.Shell; public class JavaWithNative { /** * @param args */ public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("Hello world"); Layout layout = new RowLayout(); shell.setLayout(layout); Button button = new Button(shell, SWT.NONE); button.setText("Button"); shell.pack(); shell.open(); while(!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } }

MessageBox

// often a shell can be gotton by: Shell shell = this.getShell(); MessageBox mb = new MessageBox(shell); mb.setMessage("Invalid choice"); mb.open();

See examples here


Layouts

(as of SWT 3.3)

NameDescription
ColumnLayoutarranges children in VERTICAL columns, columns are IDENTICAL SIZED, and children STRETCHED HORIZONTALLY, ColumnLayoutData has width and height hints.
FillLayout
FormLayout
GridLayout
RowLayout
SourceViewer.RulerLayout
StackLayout All children are at the same place and size - BUT only the topmost one is visible.
Set topmost and call layout to show a new top one.
TableLayout
TableWrapLayout
WizardDialog.PageContainerFillLayout

Colors

control.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_MAGENTA))

Size, setting a Minimum Size

Use the Layout GridLayout and when adding a child set its GridData's widthHint and/or heightHint

Composite panel = new Composite(toAddPanel, SWT.NONE); GridLayout grid = new GridLayout(); grid.numColumns = 1; panel.setLayout(grid); Text text = new Text(panel, SWT.SINGLE); GridData gridData = new GridData(GridData.FILL_HORIZONTAL); gridData.widthHint = 500; text.setLayoutData(gridData);