Scala Swing Components and Basic Layout example

After having completed a basic Scala Swing example in this post, we continue by adding a basic reactive functionality similar to its Java Swing counterpart presented in this post:

object HelloWorld extends SimpleSwingApplication {

  def top = new MainFrame {

    val button = new Button("Click me!")
    val textArea = new TextArea {
      listenTo(button)
      reactions += { case ButtonClicked(_)  => text = text.concat("Button Clicked!\n") }
    }

    contents = new BorderPanel{
      layout(button) = BorderPanel.Position.North
      layout(textArea) = BorderPanel.Position.South
    }

    title = "Hello, World!"
    this.peer.setSize(200,100)
    this.peer.setLocationRelativeTo(null)

  }
}

Notes:
– The TextArea component listensTo the Button as well as defines the reactive functionality by adding case function to the reactions.
– The BorderPanel represents the BorderLayout and is added under the contents variable.
– Lastly, as also mentioned in the previous article, by calling peer the MainFrame has access to the methods of the underlying JFrame.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s