| 1 |
////////////////////////////////////////// |
| 2 |
|
| 3 |
Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
or more contributor license agreements. See the NOTICE file |
| 5 |
distributed with this work for additional information |
| 6 |
regarding copyright ownership. The ASF licenses this file |
| 7 |
to you under the Apache License, Version 2.0 (the |
| 8 |
"License"); you may not use this file except in compliance |
| 9 |
with the License. You may obtain a copy of the License at |
| 10 |
|
| 11 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
|
| 13 |
Unless required by applicable law or agreed to in writing, |
| 14 |
software distributed under the License is distributed on an |
| 15 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
KIND, either express or implied. See the License for the |
| 17 |
specific language governing permissions and limitations |
| 18 |
under the License. |
| 19 |
|
| 20 |
////////////////////////////////////////// |
| 21 |
|
| 22 |
[[swingbuilder]] |
| 23 |
= SwingBuilder |
| 24 |
|
| 25 |
`SwingBuilder` allows you to create full-fledged Swing GUIs in a declarative and concise fashion. It accomplishes this by employing a common idiom in Groovy, builders. |
| 26 |
Builders handle the busywork of creating complex objects for you, such as instantiating children, calling Swing methods, and attaching these children to their parents. |
| 27 |
As a consequence, your code is much more readable and maintainable, while still allowing you access to the full range of Swing components. |
| 28 |
|
| 29 |
Here's a simple example of using `SwingBuilder`: |
| 30 |
[source,groovy] |
| 31 |
---- |
| 32 |
include::{rootProjectDir}/subprojects/groovy-swing/src/spec/test/SwingBuilderTest.groovy[tags=simple_example,indent=0] |
| 33 |
---- |
| 34 |
|
| 35 |
Here is what it will look like: |
| 36 |
|
| 37 |
image::assets/img/SwingBuilder001.gif[] |
| 38 |
|
| 39 |
This hierarchy of components would normally be created through a series of repetitive instantiations, setters, and finally attaching this child to its respective parent. |
| 40 |
Using `SwingBuilder`, however, allows you to define this hierarchy in its native form, which makes the interface design understandable simply by reading the code. |
| 41 |
|
| 42 |
The flexibility shown here is made possible by leveraging the many programming features built-in to Groovy, such as closures, implicit constructor calling, import aliasing, and string interpolation. |
| 43 |
Of course, these do not have to be fully understood in order to use `SwingBuilder`; as you can see from the code above, their uses are intuitive. |
| 44 |
|
| 45 |
Here is a slightly more involved example, with an example of `SwingBuilder` code re-use via a closure. |
| 46 |
|
| 47 |
[source,groovy] |
| 48 |
---- |
| 49 |
include::{rootProjectDir}/subprojects/groovy-swing/src/spec/test/SwingBuilderTest.groovy[tags=more_involved_example,indent=0] |
| 50 |
---- |
| 51 |
|
| 52 |
Here's another variation that relies on observable beans and binding: |
| 53 |
|
| 54 |
[source,groovy] |
| 55 |
---- |
| 56 |
include::{rootProjectDir}/subprojects/groovy-swing/src/spec/test/SwingBuilderTest.groovy[tags=observable_binding_example,indent=0] |
| 57 |
---- |
| 58 |
|
| 59 |
<<core-metaprogramming.adoc#xform-Bindable,@Bindable>> is one of the core AST Transformations. It generates all the required boilerplate code to turn a simple bean into an observable one. The `bind()` node creates appropriate `PropertyChangeListeners` that will update the interested parties whenever a `PropertyChangeEvent` is fired. |