• Home
  • Events
  • About
  • Using Open Dolphin with pure Swing

    Today I would like to show you how a pure Swing based application can benefit from using Open Dolphin even if you are not planning to migrate it to JavaFX.

    Although open-dolphin comes with many JavaFX examples it is not bound to JavaFX at all. In fact it does not require any specific UI toolkit.

    You can get the source code for this example ready to build from github:

    1. git clone https://github.com/svene/opendolphin_swing_example.git
    2. ./gradlew build (or ‘gradlew.bat build’ if you are on Windows)
    3. ./gradlew :combined:run

    The included example consists of just four simple widgets: a text field, a label and two buttons.
    I did not spend any effort in making it visually attractive since I want you to concentrate on the open-dolphin aspects.
    This is how the GUI looks like:

    dolphin_swing

    and here is a video demonstrating it’s behavior:

    dolphin_swing_demo

    The following picture depicts how the individual widgets of the GUI are connected to the single presentation model:

    dolphin_swing_with_pm

    Whenever a button is pressed a new value for the color is set on the presentation model (PM for short). And whenever the PM’s value changes both the label and the textfield are updated accordingly. In addition whenever the value in the textfield is changed by the user the value will be written back into the PM which in turn will update the label and so on.

    In other words: each widget only talks (is bound) to the PM and not to other widgets. This also means they are  only depending on the PM but not on other widgets. You can find more information about the PM concept on open-dolphin.org.

    Some things worth to mention about the code:

    When we invoke

    as decribed earlier the application starts up with ApplicationInMemoryStarter. If you have a look into it you find that it uses SwingUiThreadHandler so that Dolphin can make use of Swing’s thread handling using:

    The GUI (see ApplicationFrame) makes use of my preferred layoutcontainer: MigLayout.

    Widgets are bound using Open Dolphin’s Binder class. The label for example is bound to the PM with a convenient DSL as follows:

    which means: whenever the color of pm1 changes update the label’s text accordingly. The textfield is bound in the same way. Since text fields in Swing are a little uncooperative in regards to binding I used Jean-Marc Astesana’s CoolJTextField (see http://stackoverflow.com/questions/3953208/value-change-listener-to-jtextfield for the details).

    Summary

    This blog post gave you a starting point on how to apply the presentation model concept with Open Dolphin to a Swing application. But we only scratched the surface. If you would like to learn more about these intersting topics visit the Open Dolphin website or get in contact with Canoo.

    Sven Ehrke (@syendar)

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • DZone
    • Y!GG
    • Webnews
    • Digg
    • del.icio.us
    • DotNetKicks
    • Facebook
    • Google Bookmarks
    • Newsrider
    • Twitter
    • YahooBuzz

    Leave a Comment


    three × 8 =

    css.php