Code Examples for

Programming in Scala, Fourth Edition

Return to chapter index

34 GUI Programming

  • 34.1 A first Swing application
  • 34.2 Panels and layouts
  • 34.3 Handling events
  • 34.4 Example: Celsius/Fahrenheit converter
  • 34.5 Conclusion
  • 34.1 A first Swing application


    import scala.swing._
    // In file gui-programming/FirstSwingApp.scala import scala.swing._ object FirstSwingApp extends SimpleSwingApplication { def top = new MainFrame { title = "First Swing App" contents = new Button { text = "Click me" } } }
    object FirstSwingApp extends SimpleSwingApplication {
    def top = new MainFrame {
    title = "First Swing App"
    def title: String
    def title_=(s: String)
    contents = new Button {
    text = "Click me"

    34.2 Panels and layouts


    // In file gui-programming/SecondSwingApp.scala import scala.swing._ object SecondSwingApp extends SimpleSwingApplication { def top = new MainFrame { title = "Second Swing App" val button = new Button { text = "Click me" } val label = new Label { text = "No button clicks registered" } contents = new BoxPanel(Orientation.Vertical) { contents += button contents += label border = Swing.EmptyBorder(30, 30, 10, 30) } } }
    val label = new Label { text = "No button clicks registered" }
    contents = new BoxPanel(Orientation.Vertical) {
    contents += button contents += label
    border = Swing.EmptyBorder(30, 30, 10, 30)

    34.3 Handling events


    listenTo(button)
    case class ButtonClicked(source: Button)
    var nClicks = 0 reactions += { case ButtonClicked(b) => nClicks += 1 label.text = "Number of button clicks: " + nClicks }
    // In file gui-programming/ReactiveSwingApp.scala import scala.swing._ import scala.swing.event._ object ReactiveSwingApp extends SimpleSwingApplication { def top = new MainFrame { title = "Reactive Swing App" val button = new Button { text = "Click me" } val label = new Label { text = "No button clicks registered" } contents = new BoxPanel(Orientation.Vertical) { contents += button contents += label border = Swing.EmptyBorder(30, 30, 10, 30) } listenTo(button) var nClicks = 0 reactions += { case ButtonClicked(b) => nClicks += 1 label.text = "Number of button clicks: " + nClicks } } }
    // In file gui-programming/TempConverter.scala import swing._ import event._ object TempConverter extends SimpleSwingApplication { def top = new MainFrame { title = "Celsius/Fahrenheit Converter" object celsius extends TextField { columns = 5 } object fahrenheit extends TextField { columns = 5 } contents = new FlowPanel { contents += celsius contents += new Label(" Celsius = ") contents += fahrenheit contents += new Label(" Fahrenheit") border = Swing.EmptyBorder(15, 10, 10, 10) } listenTo(celsius, fahrenheit) reactions += { case EditDone(`fahrenheit`) => val f = fahrenheit.text.toInt val c = (f - 32) * 5 / 9 celsius.text = c.toString case EditDone(`celsius`) => val c = celsius.text.toInt val f = c * 9 / 5 + 32 fahrenheit.text = f.toString } } }

    34.4 Example: Celsius/Fahrenheit converter


    import swing._ import event._
    import scala.swing._ import scala.swing.event._
    case EditDone(`celsius`)

    34.5 Conclusion

    For more information about Programming in Scala, Fourth Edition (the "Stairway Book"), please visit:

    http://www.artima.com/shop/programming_in_scala_4ed

    and:

    http://booksites.artima.com/programming_in_scala_4ed

    Copyright © 2007-2019 Artima, Inc. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.