Geeking spree

Michael Flowersky's Output

Hello Scala Swing World

Building a Swing application in Scala is even easier that building the same app in Java. To write a Swing application you need to import classes from scala.swing package and create an object which extends SimpleGUIApplication, for Scala version above 2.8 SimpleSwingApplication. Let’s take a look at the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import scala.swing._
import java.awt.Dimension

/**
  * Extends SimpleGUIApplication in Scala <= 2.8.
  */
object HelloSwingWorldApp extends SimpleSwingApplication {
    def top = new MainFrame {
        title = "Swing in Scala"
        preferredSize = new Dimension(320, 100)
        contents = new Label {
            text = "Hello, Swing programming in Scala!"
        }
    }
}

You can compile and run this example to get the following result:

Hello, Scala World!

A step by step explanation

Let’s take a look at this swing application. In the first line Scala imports a swing package. It contains layouts, buttons, grids and all the other things which are needed to build a window application. The second line contains an import of Dimension class from java.awt package. Dimension is used to specify the size of a window. It contains two values: window width and window height. In our case, window width is 320 and window height is 100. The main swing application class called HelloSwingWorldApp is defined in line number 7. As I mentioned earlier, a swing application should extend either SimpleSwingApplication or SimpleGUIApplication, accordingly to the version of Scala you are using. Every Swing application class contains top method which is invoked by default to execute an app. This method should return top-level component of your UI. In our case it’s an object of the MainFrame class. Alternatively, we could return Frame object but then we would have to implement the closing of our application. To our advantage, MainFrame already provides this functionality. In lines 9-13 we specify that our frame should have one label with “Hello, Swing programming in Scala!” text, the window’s title is Swing in Scala and we want the prefered size to be set to 320 pixels width and 100 pixels height.

Additional information

I hope this post was useful for you! If you want to get regular updates of GeekingSpree.com follow me at Twitter. If you have questions just write to me at michaelflowersky at geekingspree dot com or put a comment below. If you are searching for something compact about Scala I definitely recommend Scala for the Impatient by Cay Horstmann. You can find the source code of today’s example here.

References

  1. Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition by Martin Odersky, Lex Spoon and Bill Venners

Scala Functions With Default Parameters

You can easily write functions with default arguments in Scala. Let’s take a look at the following example:

1
2
3
4
5
def connect(host: String = "http://localhost", port: Int = 80): Unit = {
    /* connecting to a http server ... */
    println("Connecting to: " + host + ":" + port)
    /* the rest of the function code ... */
}

In the above example I’m using default arguments for both host and port. If the client doesn’t provide argument while calling this function, default will be used:

1
2
scala> connect()
"Connecting to: http://localhost:80"

Also, the client can provide only one parameter:

1
2
scala> connect("http://www.geekingspree.com")
"Connecting to: http://www.geekingspree.com:80"

If both arguments are default and you want to provide the second argument and leave the first as default, you need to use a named argument like this:

1
2
scala> connect(port=8080)
"Connecting to: http://localhost:8080"

Additional information

I hope this post was useful for you! If you want to get regular updates of GeekingSpree.com follow me at Twitter. If you have questions just write to me at michaelflowersky at geekingspree dot com or put a comment below. If you are searching for something compact about Scala I definitely recommend Scala for the Impatient by Cay Horstmann.

References

  1. Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition by Martin Odersky, Lex Spoon and Bill Venners

How to Use Named Function Arguments in Scala?

A function call with named arguments is an alternative to a normal function call. In the case of a normal function call you just specify function arguments one after another in order that is specified by arguments list in a function’s definition. A function call with named arguments gives you the possibility to name an argument to which you are assigning a value. I will explain this on two examples - one will show a normal function call and another, a function call with named arguments.

First, let’s define a function which computes volume of an ellipsoid. I will use this function as an example:

1
def ellipsoidArea(a: Double, b: Double, c: Double) = (4/3) * math.Pi * a * b * c

This function results in volume of an ellipsoid of given a, b and c values.

A normal function call

To call our function in a normal way you would just write the following code:

1
ellipsoidArea(20, 30, 40) // res0: Double = 75398.22368615503

Parameters are set in order, that was defined in the function’s definition, so a was set to 20, b to 30 and c to 40.

A named arguments function call

If you want to change default order of function parameters you can use named arguments:

1
2
ellipsoidArea(a=20, b=30, c=40) // default order
ellipsoidArea(c=40, b=30, a=20) // reverse order

A mix of normal (positional) and named arguments

If you want to mix positional and named arguments you need to specify positional arguments first:

1
ellipsoidArea(20, c=40, b=30) // a mix of positional and named arguments function call

In this case a=20, b=30 and c=40.

Additional information

I hope this post was useful for you! If you want to get regular updates of GeekingSpree.com follow me at Twitter. If you have questions just write to me at michaelflowersky at geekingspree dot com or put a comment below. If you are searching for something compact about Scala I definitely recommend Scala for the Impatient by Cay Horstmann.

References

  1. Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition by Martin Odersky, Lex Spoon and Bill Venners