Skip to content

Commit 6d3a0d5

Browse files
Thomas Singermickaelistria
Thomas Singer
authored andcommitted
README.md: add some basic information
including a not too primitive example Change-Id: I4da4cecf857a71eac66937a7a1cc38e9fdfe485d
1 parent 07ac8aa commit 6d3a0d5

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

README.md

+76
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,81 @@
11
[![SWT Matrix Build](https://github.com/eclipse-platform/eclipse.platform.swt/actions/workflows/maven.yml/badge.svg)](https://github.com/eclipse-platform/eclipse.platform.swt/actions/workflows/maven.yml) ![SWT Matrix Tests](https://gist.githubusercontent.com/eclipse-releng-bot/78d110a601baa4ef777ccb472f584038/raw/71510599eb84e852f3e135aa7a3ddf33854ca716/badge.svg)
22

3+
# About
4+
5+
SWT is a cross-platform GUI library for JVM based desktop applications.
6+
The best known SWT-based application is [Eclipse](https://www.eclipse.org).
7+
8+
## Getting Started
9+
10+
SWT comes with platform-specific jar files.
11+
Download them from https://download.eclipse.org/eclipse/downloads/index.html and add the jar file to your classpath.
12+
13+
### Example
14+
![Example](example.png)
15+
```java
16+
import org.eclipse.swt.SWT;
17+
import org.eclipse.swt.layout.GridData;
18+
import org.eclipse.swt.layout.GridLayout;
19+
import org.eclipse.swt.widgets.Button;
20+
import org.eclipse.swt.widgets.Display;
21+
import org.eclipse.swt.widgets.Label;
22+
import org.eclipse.swt.widgets.Shell;
23+
import org.eclipse.swt.widgets.Text;
24+
25+
public class HelloWorld {
26+
27+
public static void main(String[] args) {
28+
final Display display = new Display();
29+
30+
final Shell shell = new Shell(display);
31+
shell.setText("Hello World");
32+
shell.setLayout(new GridLayout(2, false));
33+
34+
final Label label = new Label(shell, SWT.LEFT);
35+
label.setText("Your &Name:");
36+
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
37+
38+
final Text text = new Text(shell, SWT.BORDER | SWT.SINGLE);
39+
final GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
40+
data.minimumWidth = 120;
41+
text.setLayoutData(data);
42+
43+
final Button button = new Button(shell, SWT.PUSH);
44+
button.setText("Say Hello");
45+
shell.setDefaultButton(button);
46+
button.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false, 2, 1));
47+
48+
final Label output = new Label(shell, SWT.CENTER);
49+
output.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
50+
51+
button.addListener(SWT.Selection, event -> {
52+
String name = text.getText().trim();
53+
if (name.length() == 0) {
54+
name = "world";
55+
}
56+
output.setText("Hello " + name + "!");
57+
});
58+
59+
shell.setSize(shell.computeSize(SWT.DEFAULT, SWT.DEFAULT));
60+
shell.open();
61+
62+
while (!shell.isDisposed()) {
63+
if (!display.readAndDispatch()) {
64+
display.sleep();
65+
}
66+
}
67+
68+
display.dispose();
69+
}
70+
}
71+
```
72+
First, a `Display` is created which is something like the central place of all GUI-related code.
73+
Then a `Shell` is created which in our example is a top-level window.
74+
Then all child controls and listeners are created, including their layout information.
75+
Finally, we set the window's size determines by its child controls and open the window.
76+
The `while`-loop processes all GUI related events until the shell is disposed which happens when closing.
77+
Before exiting, any claimed GUI resources needs to be freed.
78+
379
Contributing to SWT
480
===================
581

example.png

5.3 KB
Loading

0 commit comments

Comments
 (0)