scala / scala-swing

Scala wrappers for Java's Swing API for desktop GUIs
Apache License 2.0
127 stars 46 forks source link
desktop gui scala swing

scala-swing

Maintenance status

Scala-swing is community-maintained by @Sciss and @benhutchison, with an occasional assist from @SethTisue. If you are interested in helping, then start a thread in the Discussions section, or contact the maintainers.

Adding an sbt dependency

To use scala-swing from sbt, add this to your build.sbt:

libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "3.0.0"

About scala-swing

This is a UI library that wraps most of Java Swing for Scala in a straightforward manner. The widget class hierarchy loosely resembles that of Java Swing. The main differences are:

The library comprises two main packages:

Examples

A number of examples can be found in the examples project. A good place to start is [16] scala.swing.examples.UIDemo. This pulls in the all the other examples into a tabbed window.

$ sbt examples/run

Multiple main classes detected, select one to run:

 [1] scala.swing.examples.ButtonApp
 [2] scala.swing.examples.CelsiusConverter
 [3] scala.swing.examples.CelsiusConverter2
 [4] scala.swing.examples.ColorChooserDemo
 [5] scala.swing.examples.ComboBoxes
 [6] scala.swing.examples.CountButton
 [7] scala.swing.examples.Dialogs
 [8] scala.swing.examples.GridBagDemo
 [9] scala.swing.examples.HelloWorld
 [10] scala.swing.examples.LabelTest
 [11] scala.swing.examples.LinePainting
 [12] scala.swing.examples.ListViewDemo
 [13] scala.swing.examples.PopupDemo
 [14] scala.swing.examples.SwingApp
 [15] scala.swing.examples.TableSelection
 [16] scala.swing.examples.UIDemo

Enter number:

Frame with a Button

The following example shows how to plug components and containers together and react to a mouse click on a button:

import scala.swing._

new Frame {
  title = "Hello world"

  contents = new FlowPanel {
    contents += new Label("Launch rainbows:")
    contents += new Button("Click me") {
      reactions += {
        case event.ButtonClicked(_) =>
          println("All the colours!")
      }
    }
  }

  pack()
  centerOnScreen()
  open()
}

Versions

The reason to have different major versions is to allow for binary incompatible changes. Also, some java-swing classes were generified in JDK 7 (see SI-3634) and require the scala-swing sources to be adjusted.

Versions now follow classic Scala PVP style with binary compatibility within x.y.* but not x.*.*.

API documentation (Scaladoc)

The API documentation for scala-swing can be found at https://javadoc.io/doc/org.scala-lang.modules/scala-swing_2.13/latest/index.html for scala 2.13 and https://javadoc.io/doc/org.scala-lang.modules/scala-swing_3/latest/index.html for scala 3.

Current Work

Current changes are being made on the work branch. Last published version is found on the main branch.

Publishing New Versions

We assume that you are working on a fork of remote upstream with remote origin.