Code Examples for

Programming in Scala

Return to chapter index

32 GUI Programming

  • 32.1 A first Swing application
  • 32.2 Panels and layouts
  • 32.3 Handling events
  • 32.4 Example: Celsius/Fahrenheit converter
  • 32.5 Conclusion
  • 32.1 A first Swing application


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

    32.2 Panels and layouts


    // In file swing/SecondSwingApp.scala import scala.swing._ object SecondSwingApp extends SimpleGUIApplication { 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)

    32.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 swing/ReactiveSwingApp.scala import scala.swing._ import scala.swing.event._ object ReactiveSwingApp extends SimpleGUIApplication { 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 swing/TempConverter.scala import swing._ import event._ object TempConverter extends SimpleGUIApplication { 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 } } }

    32.4 Example: Celsius/Fahrenheit converter


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

    32.5 Conclusion

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

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

    and:

    http://booksites.artima.com/programming_in_scala

    Copyright © 2007-2008 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.