Java/SWT JFace Eclipse/Shell Display
Содержание
- 1 Child Shell Example
- 2 Child Shell Example 2
- 3 Child Shell Example 3
- 4 Complex Shell Example
- 5 Composite Shell Example 2
- 6 Create and dispose children of a composite
- 7 Create a non-rectangular shell to simulate transparency
- 8 Create a splash screen in SWT
- 9 Create one repeating timer (every 500 ms)
- 10 Create two one shot timers (5000 ms, 2000 ms)
- 11 Display example snippet: get the bounds and client area of a display
- 12 Display: stop a repeating timer when a button is pressed
- 13 HelloWorld Display
- 14 Non Rectangular Window in SWT
- 15 Open a shell maximized (full screen)
- 16 Open a shell minimized (iconified)
- 17 Prevent a shell from closing (prompt the user)
- 18 Professional Shell
- 19 Ring Shell
- 20 Shell Style
- 21 Shell Styles Example
- 22 Shows the various styles of Decorations
- 23 Simplest SWT
- 24 SWT Composite Class
- 25 SWT Shell : create a non-rectangular window
- 26 Widget Test
Child Shell Example
// java -Djava.library.path=D:\Java_Dev\eclipse\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86.library.path=D:\Java_Dev\eclipse\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86 ClassName
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ChildShellExample {
Display d = new Display();
ChildShellExample() {
Shell s = new Shell(d);
s.setSize(500, 500);
s.open();
ChildShell cs = new ChildShell(s);
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] args){
new ChildShellExample();
}
}
class ChildShell {
ChildShell(Shell parent) {
Shell child = new Shell(parent);
child.setSize(200, 200);
child.open();
}
}
Child Shell Example 2
// java -Djava.library.path=D:\Java_Dev\eclipse\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86.library.path=D:\Java_Dev\eclipse\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86 ClassName
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ChildShellExample2 {
Display d = new Display();
ChildShellExample2() {
Shell s = new Shell(d);
s.setSize(500, 500);
s.open();
ChildShell cs1 = new ChildShell(s);
ChildShell cs2 = new ChildShell(s);
ChildShell cs3 = new ChildShell(s);
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv){
new ChildShellExample2();
}
}
class ChildShell {
ChildShell(Shell parent) {
Shell child = new Shell(parent);
child.setSize(200, 200);
child.open();
}
}
Child Shell Example 3
// java -Djava.library.path=D:\Java_Dev\eclipse\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86.library.path=D:\Java_Dev\eclipse\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86 ClassName
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ChildShellExample3 {
Display d = new Display();
ChildShellExample3() {
Shell s = new Shell(d);
s.setSize(500, 500);
s.open();
ChildShell cs1 = new ChildShell(s);
System.out.println("Execution Continues");
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new ChildShellExample3();
}
}
class ChildShell {
ChildShell(Shell parent) {
Shell child = new Shell(parent);
child.setSize(200, 200);
child.open();
}
}
Complex Shell Example
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class ComplexShellExample {
Display d;
Shell s;
ComplexShellExample() {
d = new Display();
s = new Shell(d);
s.setSize(250, 275);
s.setText("A Shell Composite Example");
GridLayout gl = new GridLayout();
gl.numColumns = 4;
s.setLayout(gl);
s.setLayout(gl);
GridComposite gc = new GridComposite(s);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 4;
gc.setLayoutData(gd);
gd = new GridData();
RowComposite rc = new RowComposite(s);
gd = new GridData(GridData.FILL_HORIZONTAL);
rc.setLayoutData(gd);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] arg) {
new ComplexShellExample();
}
}
class RowComposite extends Composite {
final Button okBtn;
final Button cancelBtn;
public RowComposite(Composite c) {
super(c, SWT.NO_FOCUS);
RowLayout rl = new RowLayout();
rl.wrap = false;
rl.pack = false;
this.setLayout(rl);
okBtn = new Button(this, SWT.BORDER | SWT.PUSH);
okBtn.setText("OK");
okBtn.setSize(30, 20);
cancelBtn = new Button(this, SWT.BORDER | SWT.PUSH);
cancelBtn.setText("Cancel");
cancelBtn.setSize(30, 20);
cancelBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
System.out.println("Cancel was clicked");
}
});
}
public static void main(String[] arg) {
}
}
class GridComposite extends Composite {
public GridComposite(Composite c) {
super(c, SWT.BORDER);
GridLayout gl = new GridLayout();
gl.numColumns = 3;
this.setLayout(gl);
final Label l1 = new Label(this, SWT.BORDER);
l1.setText("Column One");
final Label l2 = new Label(this, SWT.BORDER);
l2.setText("Column Two");
final Label l3 = new Label(this, SWT.BORDER);
l3.setText("Column Three");
final Text t1 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t2 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t3 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t4 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t5 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t6 = new Text(this, SWT.SINGLE | SWT.BORDER);
GridData gd = new GridData();
gd.horizontalAlignment = GridData.CENTER;
l1.setLayoutData(gd);
gd = new GridData();
gd.horizontalAlignment = GridData.CENTER;
l2.setLayoutData(gd);
gd = new GridData();
gd.horizontalAlignment = GridData.CENTER;
l3.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t1.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t2.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t3.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t4.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t5.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t6.setLayoutData(gd);
}
}
Composite Shell Example 2
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CompositeShellExample2 {
Display d;
Shell s;
CompositeShellExample2() {
d = new Display();
s = new Shell(d);
GridLayout gl = new GridLayout();
gl.numColumns = 4;
s.setLayout(gl);
s.setSize(250, 275);
s.setText("A Shell Composite Example");
s.setLayout(gl);
GridComposite gc = new GridComposite(s);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 4;
gc.setLayoutData(gd);
gd = new GridData();
Composite c1 = new Composite(s, SWT.NO_FOCUS);
gd = new GridData(GridData.FILL_HORIZONTAL);
c1.setLayoutData(gd);
Composite c2 = new Composite(s, SWT.NO_FOCUS);
gd = new GridData(GridData.FILL_HORIZONTAL);
c2.setLayoutData(gd);
Composite c = new Composite(s, SWT.NO_FOCUS);
c.setLayout(new RowLayout());
Button b1 = new Button(c, SWT.PUSH | SWT.BORDER);
b1.setText("OK");
Button b2 = new Button(c, SWT.PUSH | SWT.BORDER);
b2.setText("Cancel");
gd = new GridData(GridData.FILL_HORIZONTAL);
c.setLayoutData(gd);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] arg) {
new CompositeShellExample2();
}
}
class GridComposite extends Composite {
public GridComposite(Composite c) {
super(c, SWT.BORDER);
GridLayout gl = new GridLayout();
gl.numColumns = 3;
this.setLayout(gl);
final Label l1 = new Label(this, SWT.BORDER);
l1.setText("Column One");
final Label l2 = new Label(this, SWT.BORDER);
l2.setText("Column Two");
final Label l3 = new Label(this, SWT.BORDER);
l3.setText("Column Three");
final Text t1 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t2 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t3 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t4 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t5 = new Text(this, SWT.SINGLE | SWT.BORDER);
final Text t6 = new Text(this, SWT.SINGLE | SWT.BORDER);
GridData gd = new GridData();
gd.horizontalAlignment = GridData.CENTER;
l1.setLayoutData(gd);
gd = new GridData();
gd.horizontalAlignment = GridData.CENTER;
l2.setLayoutData(gd);
gd = new GridData();
gd.horizontalAlignment = GridData.CENTER;
l3.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t1.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t2.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t3.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t4.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t5.setLayoutData(gd);
gd = new GridData(GridData.FILL_HORIZONTAL);
t6.setLayoutData(gd);
}
}
Create and dispose children of a composite
/*
* Composite example snippet: create and dispose children of a composite
*
* 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.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class Snippet98 {
static int pageNum = 0;
static Composite pageComposite;
public static void main(String args[]) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Push");
pageComposite = new Composite(shell, SWT.NONE);
pageComposite.setLayout(new GridLayout());
pageComposite.setLayoutData(new GridData());
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if ((pageComposite != null) && (!pageComposite.isDisposed())) {
pageComposite.dispose();
}
pageComposite = new Composite(shell, SWT.NONE);
pageComposite.setLayout(new GridLayout());
pageComposite.setLayoutData(new GridData());
if (pageNum++ % 2 == 0) {
Table table = new Table(pageComposite, SWT.BORDER);
table.setLayoutData(new GridData());
for (int i = 0; i < 5; i++) {
new TableItem(table, SWT.NONE).setText("table item "
+ i);
}
} else {
new Button(pageComposite, SWT.RADIO).setText("radio");
}
shell.layout(true);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Create a non-rectangular shell to simulate transparency
/*
* Create a non-rectangular shell to simulate transparency
*
* 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.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet180 {
public static void main(String[] args) {
Display display = new Display();
final Image image = display.getSystemImage(SWT.ICON_WARNING);
// Shell must be created with style SWT.NO_TRIM
final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
// define a region
Region region = new Region();
Rectangle pixel = new Rectangle(0, 0, 1, 1);
for (int y = 0; y < 200; y += 2) {
for (int x = 0; x < 200; x += 2) {
pixel.x = x;
pixel.y = y;
region.add(pixel);
}
}
// define the shape of the shell using setRegion
shell.setRegion(region);
Rectangle size = region.getBounds();
shell.setSize(size.width, size.height);
shell.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
Rectangle bounds = image.getBounds();
Point size = shell.getSize();
e.gc.drawImage(image, 0, 0, bounds.width, bounds.height, 10,
10, size.x - 20, size.y - 20);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
region.dispose();
display.dispose();
}
}
Create a splash screen in SWT
/*
* Shell example snippet: create a splash screen
*
* 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.Image;
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.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
public class Snippet104 {
public static void main(String[] args) {
final Display display = new Display();
final int[] count = new int[] { 4 };
final Image image = new Image(display, 300, 300);
final Shell splash = new Shell(SWT.ON_TOP);
final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
bar.setMaximum(count[0]);
Label label = new Label(splash, SWT.NONE);
label.setImage(image);
FormLayout layout = new FormLayout();
splash.setLayout(layout);
FormData labelData = new FormData();
labelData.right = new FormAttachment(100, 0);
labelData.bottom = new FormAttachment(100, 0);
label.setLayoutData(labelData);
FormData progressData = new FormData();
progressData.left = new FormAttachment(0, 5);
progressData.right = new FormAttachment(100, -5);
progressData.bottom = new FormAttachment(100, -5);
bar.setLayoutData(progressData);
splash.pack();
Rectangle splashRect = splash.getBounds();
Rectangle displayRect = display.getBounds();
int x = (displayRect.width - splashRect.width) / 2;
int y = (displayRect.height - splashRect.height) / 2;
splash.setLocation(x, y);
splash.open();
display.asyncExec(new Runnable() {
public void run() {
Shell[] shells = new Shell[count[0]];
for (int i = 0; i < count[0]; i++) {
shells[i] = new Shell(display);
shells[i].setSize(300, 300);
shells[i].addListener(SWT.Close, new Listener() {
public void handleEvent(Event e) {
--count[0];
}
});
bar.setSelection(i + 1);
try {
Thread.sleep(1000);
} catch (Throwable e) {
}
}
splash.close();
image.dispose();
for (int i = 0; i < count[0]; i++) {
shells[i].open();
}
}
});
while (count[0] != 0) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Create one repeating timer (every 500 ms)
/*
* Display example snippet: create one repeating timer (every 500 ms)
*
* 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.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet16 {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
final int time = 500;
Runnable timer = new Runnable() {
public void run() {
Point point = display.getCursorLocation();
Rectangle rect = shell.getBounds();
if (rect.contains(point)) {
System.out.println("In");
} else {
System.out.println("Out");
}
display.timerExec(time, this);
}
};
display.timerExec(time, timer);
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Create two one shot timers (5000 ms, 2000 ms)
/*
* Display example snippet: create two one shot timers (5000 ms, 2000 ms)
*
* 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.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet60 {
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setSize (200, 200);
shell.open ();
display.timerExec (5000, new Runnable () {
public void run () {
System.out.println ("5000");
}
});
display.timerExec (2000, new Runnable () {
public void run () {
System.out.println ("2000");
}
});
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Display example snippet: get the bounds and client area of a display
/*
* Display example snippet: get the bounds and client area of a display
*
* 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.widgets.Display;
public class Snippet42 {
public static void main(String[] args) {
Display display = new Display();
System.out.println("BOUNDS=" + display.getBounds() + " CLIENT="
+ display.getClientArea());
display.dispose();
}
}
Display: stop a repeating timer when a button is pressed
/*
* Display example snippet: stop a repeating timer when a button is pressed
*
* 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.Color;
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.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class Snippet68 {
public static void main(String[] args) {
final Display display = new Display();
final Color red = display.getSystemColor(SWT.COLOR_RED);
final Color blue = display.getSystemColor(SWT.COLOR_BLUE);
Shell shell = new Shell(display);
shell.setLayout(new RowLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Stop Timer");
final Label label = new Label(shell, SWT.BORDER);
label.setBackground(red);
final int time = 500;
final Runnable timer = new Runnable() {
public void run() {
if (label.isDisposed())
return;
Color color = label.getBackground().equals(red) ? blue : red;
label.setBackground(color);
display.timerExec(time, this);
}
};
display.timerExec(time, timer);
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
display.timerExec(-1, timer);
}
});
button.pack();
label.setLayoutData(new RowData(button.getSize()));
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
HelloWorld Display
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 15, 2004 1:22:06 AM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HelloWorldDisplay {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Hello, world!");
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
}
Non Rectangular Window in SWT
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Region;
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;
public class NonRectangularClass {
static int[] createCircle(int xOffset, int yOffset, int radius) {
int[] circlePoints = new int[10 * radius];
for (int loopIndex = 0; loopIndex < 2 * radius + 1; loopIndex++) {
int xCurrent = loopIndex - radius;
int yCurrent = (int) Math.sqrt(radius * radius - xCurrent
* xCurrent);
int doubleLoopIndex = 2 * loopIndex;
circlePoints[doubleLoopIndex] = xCurrent + xOffset;
circlePoints[doubleLoopIndex + 1] = yCurrent + yOffset;
circlePoints[10 * radius - doubleLoopIndex - 2] = xCurrent
+ xOffset;
circlePoints[10 * radius - doubleLoopIndex - 1] = -yCurrent
+ yOffset;
}
return circlePoints;
}
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, SWT.NO_TRIM);
Region region = new Region();
region.add(createCircle(50, 50, 50));
region.subtract(createCircle(50, 50, 20));
shell.setRegion(region);
shell.setSize(region.getBounds().width, region.getBounds().height);
shell.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
Button button = new Button(shell, SWT.PUSH);
button.setText("Exit");
button.setBounds(35, 6, 35, 20);
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
shell.close();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
region.dispose();
display.dispose();
}
}
Open a shell maximized (full screen)
/*
* Shell example snippet: open a shell maximized (full screen)
*
* 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.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet28 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setMaximized(true);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Open a shell minimized (iconified)
/*
* Shell example snippet: open a shell minimized (iconified)
*
* 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.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet27 {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setMinimized(true);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Prevent a shell from closing (prompt the user)
/*
* Shell example snippet: prevent a shell from closing (prompt the user)
*
* 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.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class Snippet99 {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.addListener(SWT.Close, new Listener() {
public void handleEvent(Event event) {
int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO;
MessageBox messageBox = new MessageBox(shell, style);
messageBox.setText("Information");
messageBox.setMessage("Close the shell?");
event.doit = messageBox.open() == SWT.YES;
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Professional Shell
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class ProfessionalShell {
ProfessionalShell() {
Display d = new Display();
Shell s = new Shell(d);
s.setSize(500,500);
s.setImage(new Image(d, "jexp.gif"));
s.setText("A Shell Example");
s.open();
while(!s.isDisposed()){
if(!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main(String[] argv) {
new ProfessionalShell();
}
}
Ring Shell
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 25, 2004 7:58:06 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class RingShell {
int[] createCircle(int radius, int centerX, int centerY) {
int[] points = new int[360 * 2];
for(int i=0; i<360; i++) {
points[i*2] = centerX + (int)(radius * Math.cos(Math.toRadians(i)));
points[i*2+1] = centerY + (int)(radius * Math.sin(Math.toRadians(i)));
}
return points;
}
Point originalPosition = null;
public RingShell() {
final Display display = new Display();
final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
shell.setBackground(display.getSystemColor(SWT.COLOR_DARK_MAGENTA));
Region region = new Region();
region.add(createCircle(100, 100, 100));
region.subtract(createCircle(50, 100, 100));
shell.setRegion(region);
// Make the shell movable by using the mouse pointer.
shell.addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent e) {
shell.dispose(); // Double click to dispose the shell.
}
public void mouseDown(MouseEvent e) {
originalPosition = new Point(e.x, e.y);
}
public void mouseUp(MouseEvent e) {
originalPosition = null;
}
});
shell.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent e) {
if(originalPosition == null)
return;
Point point = display.map(shell, null, e.x, e.y);
shell.setLocation(point.x - originalPosition.x, point.y - originalPosition.y);
System.out.println("Moved from: " + originalPosition + " to " + point);
}
});
Rectangle regionBounds = region.getBounds();
shell.setSize(regionBounds.width, regionBounds.height);
shell.open();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
region.dispose();
}
public static void main(String[] args) {
new RingShell();
}
}
Shell Style
/******************************************************************************
* Copyright (c) 1998, 2004 Jackwind Li Guojie
* All right reserved.
*
* Created on Jan 25, 2004 4:28:59 PM by JACK
* $Id$
*
* visit: http://www.asprise.ru/swt
*****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ShellTrim {
Display display = new Display();
Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.TOOL);
//Shell shell = new Shell(display, SWT.NO_TRIM);
// Shell shell = new Shell(display, SWT.MAX);
public ShellTrim() {
init();
shell.setText("Style: SHELL_TRIM | TOOL");
shell.pack();
shell.open();
Shell shell2 = new Shell();
shell2.pack();
shell2.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 ShellTrim();
}
}
Shell Styles Example
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ShellStylesExample {
ShellStylesExample() {
Display d = new Display();
Shell s = new Shell(d, SWT.CLOSE | SWT.RESIZE);
s.setSize(300, 300);
s.open();
while (!s.isDisposed()) {
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}
public static void main() {
new ShellStylesExample();
}
}
Shows the various styles of Decorations
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This application shows the various styles of Decorations
*/
public class DecorationsExample {
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("Decorations Example");
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Creates the various Decorations
*
* @param composite the parent composite
*/
public void createContents(Composite composite) {
// There are nine distinct styles, so create
// a 3x3 grid
composite.setLayout(new GridLayout(3, true));
// The SWT.BORDER style
Decorations d = new Decorations(composite, SWT.BORDER);
d.setLayoutData(new GridData(GridData.FILL_BOTH));
d.setLayout(new FillLayout());
new Label(d, SWT.CENTER).setText("SWT.BORDER");
// The SWT.CLOSE style
d = new Decorations(composite, SWT.CLOSE);
d.setLayoutData(new GridData(GridData.FILL_BOTH));
d.setLayout(new FillLayout());
new Label(d, SWT.CENTER).setText("SWT.CLOSE");
// The SWT.MIN style
d = new Decorations(composite, SWT.MIN);
d.setLayoutData(new GridData(GridData.FILL_BOTH));
d.setLayout(new FillLayout());
new Label(d, SWT.CENTER).setText("SWT.MIN");
// The SWT.MAX style
d = new Decorations(composite, SWT.MAX);
d.setLayoutData(new GridData(GridData.FILL_BOTH));
d.setLayout(new FillLayout());
new Label(d, SWT.CENTER).setText("SWT.MAX");
// The SWT.NO_TRIM style
d = new Decorations(composite, SWT.NO_TRIM);
d.setLayoutData(new GridData(GridData.FILL_BOTH));
d.setLayout(new FillLayout());
new Label(d, SWT.CENTER).setText("SWT.NO_TRIM");
// The SWT.RESIZE style
d = new Decorations(composite, SWT.RESIZE);
d.setLayoutData(new GridData(GridData.FILL_BOTH));
d.setLayout(new FillLayout());
new Label(d, SWT.CENTER).setText("SWT.RESIZE");
// The SWT.TITLE style
d = new Decorations(composite, SWT.TITLE);
d.setLayoutData(new GridData(GridData.FILL_BOTH));
d.setLayout(new FillLayout());
new Label(d, SWT.CENTER).setText("SWT.TITLE");
// The SWT.ON_TOP style
d = new Decorations(composite, SWT.ON_TOP);
d.setLayoutData(new GridData(GridData.FILL_BOTH));
d.setLayout(new FillLayout());
new Label(d, SWT.CENTER).setText("SWT.ON_TOP");
// The SWT.TOOL style
d = new Decorations(composite, SWT.TOOL);
d.setLayoutData(new GridData(GridData.FILL_BOTH));
d.setLayout(new FillLayout());
new Label(d, SWT.CENTER).setText("SWT.TOOL");
}
/**
* The entry point for the application
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new DecorationsExample().run();
}
}
Simplest SWT
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class HelloSWT {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("My First SWT GUI");
shell.setSize(200, 100);
Text helloText = new Text(shell, SWT.CENTER);
helloText.setText("Hello SWT!");
helloText.setBounds(47, 20, 100, 20);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
SWT Composite Class
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class CompositeClass {
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(300, 300);
shell.setLayout(new RowLayout());
shell.setText("Composite Example");
final Composite composite = new Composite(shell, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 4;
composite.setLayout(gridLayout);
for (int loopIndex = 0; loopIndex < 28; loopIndex++) {
Label label = new Label(composite, SWT.SHADOW_NONE);
label.setText("Label " + loopIndex);
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
SWT Shell : create a non-rectangular window
/*
* Shell example snippet: create a non-rectangular window
*
* 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.graphics.Region;
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;
public class Snippet134 {
static int[] circle(int r, int offsetX, int offsetY) {
int[] polygon = new int[8 * r + 4];
// x^2 + y^2 = r^2
for (int i = 0; i < 2 * r + 1; i++) {
int x = i - r;
int y = (int) Math.sqrt(r * r - x * x);
polygon[2 * i] = offsetX + x;
polygon[2 * i + 1] = offsetY + y;
polygon[8 * r - 2 * i - 2] = offsetX + x;
polygon[8 * r - 2 * i - 1] = offsetY - y;
}
return polygon;
}
public static void main(String[] args) {
final Display display = new Display();
// Shell must be created with style SWT.NO_TRIM
final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
// define a region that looks like a key hole
Region region = new Region();
region.add(circle(67, 67, 67));
region.subtract(circle(20, 67, 50));
region.subtract(new int[] { 67, 50, 55, 105, 79, 105 });
// define the shape of the shell using setRegion
shell.setRegion(region);
Rectangle size = region.getBounds();
shell.setSize(size.width, size.height);
// add ability to move shell around
Listener l = new Listener() {
Point origin;
public void handleEvent(Event e) {
switch (e.type) {
case SWT.MouseDown:
origin = new Point(e.x, e.y);
break;
case SWT.MouseUp:
origin = null;
break;
case SWT.MouseMove:
if (origin != null) {
Point p = display.map(shell, null, e.x, e.y);
shell.setLocation(p.x - origin.x, p.y - origin.y);
}
break;
}
}
};
shell.addListener(SWT.MouseDown, l);
shell.addListener(SWT.MouseUp, l);
shell.addListener(SWT.MouseMove, l);
// add ability to close shell
Button b = new Button(shell, SWT.PUSH);
b.setBackground(shell.getBackground());
b.setText("close");
b.pack();
b.setLocation(10, 68);
b.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
shell.close();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
region.dispose();
display.dispose();
}
}
Widget Test
/*******************************************************************************
* Copyright (c) 2004 Berthold Daum. All rights reserved. This program and the
* accompanying materials are made available under the terms of the Common
* Public License v1.0 which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors: Berthold Daum
******************************************************************************/
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class widgetTest {
public static void main(String[] args) {
// Create Display instance
final Display display = new Display();
// Create top level Shell (pass display as parent)
final Shell toplevelShell = new Shell(display);
// Set title line
toplevelShell.setText("TopLevel.Titelzeile");
// Display shell
toplevelShell.open();
// Create a dialog shell (pass toplevelShell as parent)
final Shell dialogShell = new Shell(toplevelShell);
// Set title line
dialogShell.setText("Dialog.Titelzeile");
// Display shell
dialogShell.open();
// Wait until top level shell is closed
while (!toplevelShell.isDisposed()) {
// Check for waiting events
if (!display.readAndDispatch())
display.sleep();
}
}
}