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

Scala Swing smallest possible application

In comparison to the smallest possible Java Swing application posted before here, this is the Scala way of doing it. Scala is a modular language that is not shipping with one big jdk like Java does but with rather independent jars per functionality like scala-swing, scala-actors. The scala-swing is a light wrapper on Java Swing providing Scala-esque feel to it.

import swing._

object HelloWorld extends SimpleSwingApplication {
  def top = new MainFrame {
    title = "Hello, World!"
    this.peer.setSize(200,100)
    this.peer.setLocationRelativeTo(null)
  }
}

Things to note:
– We don’t need to start on independent thread or start with main method. The provided SimpleSwingApplication is doing all these for us.
MainFrame is a wrapper around JFrame
– By calling peer we are getting access on the methods of the underlying JFrame.
– Notice that we don’t have to set visible the frame or shutdown the application on close because it is done for us from the underlying MainFrame.

Guava Splitter

Guava Splitter is a utility class that is offering roughly speaking the opposite of what the Joiner utility class does.

You can have a read at my previous quick article about Guava Joiner.

Splitter is offering similar look-and-feel as Joiner: the on method serves as a static factory method, subsequent method calls are following the builder pattern returning a wrapped this (therefore need to be packed prior the final split method call). Finally it doesn’t try to be complicated returning an Iterable of Strings. It is aspiring in replacing the usage of String.split method.

Its usage when we want to get back an Iterable of Strings looks like:

 
Splitter.on(",").trimResults().split("my,string,to,split")

When we want to perform the operation retrieving a Map out of its String representation:

Splitter.on(",").trimResults().omitEmptyStrings().withKeyValueSeparator("->").split("one->1,two->2,three->3")

In this instance the call to withKeyValueSeparator is yielding a Splitter.MapSplitter inner class that is adding the map context to the splitter.

Guava Joiner

Guava Joiner is a nice utility class that is String-ing data structure contents, useful for debugging, logging, reporting or toString-ing activities in a flexible, builder pattern-esque and fluent way.

What we used to achieve with this conventional code:

public static final String joinIterable(final Iterable iterable, String delimiter){
   if(iterable == null)
      throw new IllegalArgumentException();
   StringBuilder result = new StringBuilder("Conventional iterable join: ");
   for(T elem : iterable){
      result.append(elem).append(delimiter);
   }
   result.setLength(result.length() - 1);
   return result.toString();
}

public static final String join(final List list, String delimiter){
   return joinIterable(list, delimiter);
}

can now be achieved with:

Joiner.on(",").useForNull("null").join(list)

The Joiner class is a utility class and the on method represents a static factory method that returns a new Joiner. The useForNull method follows the builder pattern returning a wrapped version of this (therefore need to be packed prior the final join method call). Be carefull that this call is needed otherwise null elements will throw a NPE. The join method is passing the Iterable in this case data structure.

Similarly for maps, what we used to achieve with this:

    public static final <K,V> String join(final Map<K,V> map, String delimiter){
        if(map == null)
            throw new IllegalArgumentException();
        StringBuilder result = new StringBuilder("Conventional map join: ");
        for(K key : map.keySet()){
            result.append(key).append("->").append(map.get(key)).append(delimiter);
        }
        result.setLength(result.length() - 1);
        return result.toString();
    }

can now be achieved by:

Joiner.on(",").withKeyValueSeparator("->").useForNull("null").join(map)

In this instance, the withKeyValueSeparator method call is returning a Joiner.MapJoiner that is a static class that is adding the map context to the Joiner class.