|
5 | 5 | [<img src="https://img.shields.io/maven-central/v/org.scala-lang.modules/scala-swing_2.12.svg?label=latest%20release%20for%202.12"/>](http://search.maven.org/#search%7Cga%7C1%7Cg%3Aorg.scala-lang.modules%20a%3Ascala-swing_2.12)
|
6 | 6 | [](http://waffle.io/scala/scala-swing)
|
7 | 7 |
|
8 |
| -This is now community maintained by @benhutchison & @Sciss. If you're interested in helping then contact them or @adriaanm. |
| 8 | +This is now community maintained by @benhutchison & @Sciss. If you are interested in helping then contact them or @adriaanm. |
9 | 9 |
|
10 | 10 | This is a UI library that will wrap most of Java Swing for Scala in a straightforward manner.
|
11 | 11 | The widget class hierarchy loosely resembles that of Java Swing. The main differences are:
|
12 | 12 |
|
13 |
| -- In Java Swing all components are containers per default. This doesn't make much sense for |
| 13 | +- In Java Swing all components are containers per default. This does not make much sense for |
14 | 14 | a number of components, like TextField, CheckBox, RadioButton, and so on. Our guess is that
|
15 | 15 | this architecture was chosen because Java lacks multiple inheritance.
|
16 | 16 | In scala-swing, components that can have child components extend the Container trait.
|
17 |
| -- Layout managers and panels are coupled. There is no way to exchange the layout manager |
| 17 | +- Layout managers and panels are coupled. There is no way to exchange the layout manager |
18 | 18 | of a panel. As a result, the layout constraints for widgets can be typed.
|
19 | 19 | (Note that you gain more type-safety and don't loose much flexibility here. Besides
|
20 | 20 | being not a common operation, exchanging the layout manager of a panel in Java
|
21 | 21 | Swing almost always leads to exchanging the layout constraints for every of the panel's
|
22 | 22 | child component. In the end, it is not more work to move all children to a newly created
|
23 | 23 | panel.)
|
24 |
| - |
25 |
| - The event system. TODO. |
26 |
| - |
| 24 | +- Widget hierarchies are built by adding children to their parent container's `contents` |
| 25 | + collection. The typical usage style is to create anonymous subclasses of the widgets to |
| 26 | + customize their properties, and nest children and event reactions. |
| 27 | +- The scala-swing event system follows a different approach than the underlying Java system. |
| 28 | + Instead of add event listeners with a particular interface (such as `java.awt.ActionListener`), |
| 29 | + a `Reactor` instances announces the interest in receiving events by calling `listenTo` for |
| 30 | + a `Publisher`. Publishers are also reactors and listen to themselves per default as a convenience. |
| 31 | + A reactor contains an object `reactions` which serves as a convenient place to register observers |
| 32 | + by adding partial functions that pattern match for any event that the observer is interested in. |
| 33 | + This is shown in the examples section below. |
27 | 34 | - For more details see [SIP-8](docs/SIP-8.md)
|
28 | 35 |
|
29 | 36 | The library comprises two main packages:
|
30 | 37 |
|
31 | 38 | - `scala.swing`: All widget classes and traits.
|
32 | 39 | - `scala.swing.event`: The event hierarchy.
|
33 | 40 |
|
34 |
| - |
35 | 41 | ## Examples
|
36 | 42 |
|
37 | 43 | A number of examples can be found in the `examples` project.
|
38 |
| -A good place to start is `[12] scala.swing.examples.UIDemo` (_index number may be different for you_). This pulls in the all the other examples into a tabbed window. |
| 44 | +A good place to start is `[16] scala.swing.examples.UIDemo`. |
| 45 | +This pulls in the all the other examples into a tabbed window. |
39 | 46 |
|
40 | 47 | ```
|
41 | 48 | $ sbt examples/run
|
42 | 49 |
|
43 | 50 | Multiple main classes detected, select one to run:
|
44 | 51 |
|
45 | 52 | [1] scala.swing.examples.ButtonApp
|
46 |
| - [2] scala.swing.examples.Dialogs |
47 |
| - [3] scala.swing.examples.ComboBoxes |
48 |
| - [4] scala.swing.examples.CelsiusConverter2 |
49 |
| - [5] scala.swing.examples.ListViewDemo |
50 |
| - [6] scala.swing.examples.HelloWorld |
51 |
| - [7] scala.swing.examples.LabelTest |
52 |
| - [8] scala.swing.examples.PopupDemo |
53 |
| - [9] scala.swing.examples.ColorChooserDemo |
54 |
| - [10] scala.swing.examples.LinePainting |
55 |
| - [11] scala.swing.examples.GridBagDemo |
56 |
| - [12] scala.swing.examples.UIDemo |
57 |
| - [13] scala.swing.examples.TableSelection |
58 |
| - [14] scala.swing.examples.CelsiusConverter |
59 |
| - [15] scala.swing.examples.SwingApp |
60 |
| - [16] scala.swing.examples.CountButton |
| 53 | + [2] scala.swing.examples.CelsiusConverter |
| 54 | + [3] scala.swing.examples.CelsiusConverter2 |
| 55 | + [4] scala.swing.examples.ColorChooserDemo |
| 56 | + [5] scala.swing.examples.ComboBoxes |
| 57 | + [6] scala.swing.examples.CountButton |
| 58 | + [7] scala.swing.examples.Dialogs |
| 59 | + [8] scala.swing.examples.GridBagDemo |
| 60 | + [9] scala.swing.examples.HelloWorld |
| 61 | + [10] scala.swing.examples.LabelTest |
| 62 | + [11] scala.swing.examples.LinePainting |
| 63 | + [12] scala.swing.examples.ListViewDemo |
| 64 | + [13] scala.swing.examples.PopupDemo |
| 65 | + [14] scala.swing.examples.SwingApp |
| 66 | + [15] scala.swing.examples.TableSelection |
| 67 | + [16] scala.swing.examples.UIDemo |
61 | 68 |
|
62 | 69 | Enter number:
|
63 | 70 | ```
|
64 | 71 |
|
| 72 | +### Frame with a Button |
| 73 | + |
| 74 | +The following example shows how to plug components and containers together and react to a |
| 75 | +mouse click on a button: |
| 76 | + |
| 77 | +```scala |
| 78 | +import scala.swing._ |
| 79 | + |
| 80 | +new Frame { |
| 81 | + title = "Hello world" |
| 82 | + |
| 83 | + contents = new FlowPanel { |
| 84 | + contents += new Label("Launch rainbows:") |
| 85 | + contents += new Button("Click me") { |
| 86 | + reactions += { |
| 87 | + case event.ButtonClicked(_) => |
| 88 | + println("All the colours!") |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + pack() |
| 94 | + centerOnScreen() |
| 95 | + open() |
| 96 | +} |
| 97 | +``` |
65 | 98 |
|
66 | 99 | ## Versions
|
67 | 100 |
|
68 | 101 | - The `1.0.x` branch is compiled with JDK 6 and released for Scala 2.10, 2.11. The 1.0.x releases can be used with both Scala versions on JDK 6 or newer.
|
69 | 102 | - The `2.0.x` branch is compiled with JDK 8 and released for Scala 2.11 and 2.12.
|
70 | 103 | - When using Scala 2.11, you can use the Scala swing 2.0.x releases on JDK 6 or newer.
|
71 | 104 | - Scala 2.12 requires you to use JDK 8 (that has nothing to do with scala-swing).
|
| 105 | +- Version `2.1.0` adds support for Scala 2.13, while dropping Scala 2.10. |
72 | 106 |
|
73 |
| -The reason to have two versions is to allow for binary incompatible changes. Also, some java-swing classes were generified in JDK 7 (see [SI-3634](https://issues.scala-lang.org/browse/SI-3634)) and require the scala-swing soruces to be adjusted. |
| 107 | +The reason to have different major versions is to allow for binary incompatible changes. Also, some java-swing classes were |
| 108 | +generified in JDK 7 (see [SI-3634](https://issues.scala-lang.org/browse/SI-3634)) and require the scala-swing sources to be adjusted. |
74 | 109 |
|
75 | 110 |
|
76 | 111 | ## API documentation (Scaladoc)
|
77 | 112 |
|
78 |
| -The API documentation for scala-swing can be found at [http://www.scala-lang.org/documentation/api.html](http://www.scala-lang.org/documentation/api.html). |
| 113 | +The API documentation for scala-swing can be found |
| 114 | +at [http://www.scala-lang.org/documentation/api.html](http://www.scala-lang.org/documentation/api.html). |
79 | 115 |
|
80 | 116 |
|
81 | 117 | ## Current Work
|
82 | 118 |
|
83 |
| -Current changes are being made on the `2.0.x` branch. |
| 119 | +Current changes are being made on the `work` branch. |
0 commit comments