• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    Scala
  • License
    Apache License 2.0
  • Created almost 11 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Scala wrappers for Java's Swing API for desktop GUIs

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:

  • In Java Swing all components are containers per default. This does not make much sense for a number of components, like TextField, CheckBox, RadioButton, and so on. Our guess is that this architecture was chosen because Java lacks multiple inheritance. In scala-swing, components that can have child components extend the Container trait.
  • Layout managers and panels are coupled. There is no way to exchange the layout manager of a panel. As a result, the layout constraints for widgets can be typed. (Note that you gain more type-safety and do not loose much flexibility here. Besides being not a common operation, exchanging the layout manager of a panel in Java Swing almost always leads to exchanging the layout constraints for every of the panel's child component. In the end, it is not more work to move all children to a newly created panel.)
  • Widget hierarchies are built by adding children to their parent container's contents collection. The typical usage style is to create anonymous subclasses of the widgets to customize their properties, and nest children and event reactions.
  • The scala-swing event system follows a different approach than the underlying Java system. Instead of adding event listeners with a particular interface (such as java.awt.ActionListener), a Reactor instance announces the interest in receiving events by calling listenTo for a Publisher. Publishers are also reactors and listen to themselves per default as a convenience. A reactor contains an object reactions which serves as a convenient place to register observers by adding partial functions that pattern match for any event that the observer is interested in. This is shown in the examples section below.
  • For more details see SIP-8

The library comprises two main packages:

  • scala.swing: All widget classes and traits.
  • scala.swing.event: The event hierarchy.

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 1.0.x branch is compiled with JDK 6 and released for Scala 2.11 and 2.11. The 1.0.x releases can be used with both Scala versions on JDK 6 or newer.
  • The 2.0.x branch is compiled with JDK 8 and released for Scala 2.11 and 2.12.
    • When using Scala 2.11, you can use the Scala swing 2.0.x releases on JDK 6 or newer.
    • Scala 2.12 requires you to use JDK 8 (that has nothing to do with scala-swing).
  • The 2.1.x series adds support for Scala 2.13, while dropping Scala 2.10.
  • The 3.0.x series adds support for Scala 3.0.

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.

  • create a new local branch that will never be pushed remotely, e.g. git checkout -b release-3.0.0#3.0.0-RC2 (do not name the branch the same as a tag)
  • edit .travis.yml to reduce the matrix to only the Scala and JDK versions needed to publish the version (publishing JDK is 8)
  • git commit
  • add a tag, e.g. git tag -a 'v3.0.0#3.0.0-RC2' -m 'version 3.0.0 for 3.0.0-RC2'
  • push only the tag, e.g. git push upstream v3.0.0#3.0.0-RC2.
  • check CI status at https://travis-ci.com/github/scala/scala-swing. You should see a message Running ci-release. followed by e.g published scala-swing_3.0.0-RC2 to ... going into local sonatype-staging, followed by the Sonatype plugin uploading the artifacts.
  • Upon success, nudge Seth to release the bundle from Sonatype.

More Repositories

1

scala

Scala 2 compiler and standard library. Bugs at https://github.com/scala/bug; Scala 3 at https://github.com/scala/scala3
Scala
14,269
star
2

scala3

The Scala 3 compiler, also known as Dotty.
Scala
5,786
star
3

scala-async

An asynchronous programming facility for Scala
Scala
1,138
star
4

pickling

Fast, customizable, boilerplate-free pickling support for Scala
Scala
833
star
5

scala-parser-combinators

simple combinator-based parsing for Scala. formerly part of the Scala standard library, now a separate community-maintained module
Scala
642
star
6

docs.scala-lang

The Scala Documentation website
HTML
559
star
7

scala-java8-compat

A Java 8 (and up) compatibility kit for Scala.
Scala
436
star
8

legacy-svn-scala

OBSOLETE, we're over there:
Scala
366
star
9

scala3-example-project

An example sbt project that compiles using Dotty
Scala
332
star
10

scala-xml

The standard Scala XML library
Scala
295
star
11

scala-dist

sbt project that packages the Scala distribution
Scala
276
star
12

scala-lang

sources for the Scala language website
SCSS
268
star
13

scala-abide

obsolete; visit https://github.com/scalacenter/scalafix instead
Scala
232
star
14

bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
230
star
15

collection-strawman

Implementation of the new Scala 2.13 Collections
Scala
201
star
16

scala-collection-compat

makes some Scala 2.13 APIs (primarily collections, also some others) available on 2.11 and 2.12, to aid cross-building
Scala
200
star
17

scala-parallel-collections

Parallel collections standard library module for Scala 2.13+
Scala
194
star
18

scala-seed.g8

Giter8 template for a simple hello world app in Scala.
Scala
150
star
19

scala-dev

Scala 2 team issues. Not for user-facing bugs or directly actionable user-facing improvements. For build/test/infra and for longer-term planning and idea tracking. Our bug tracker is at https://github.com/scala/bug/issues
132
star
20

community-build

Scala 2 community build — a corpus of open-source repos built against Scala nightlies
Shell
129
star
21

scala3.g8

Scala
123
star
22

scala-collection-contrib

community-contributed additions to the Scala 2.13 collections
Scala
107
star
23

scala-module-dependency-sample

Depend on Scala modules like a pro
Scala
95
star
24

scala-continuations

the Scala delimited continuations plugin and library
Scala
89
star
25

toolkit

The batteries-included Scala
Scala
87
star
26

make-release-notes

The project that generates Scala release notes.
HTML
85
star
27

vscode-scala-syntax

Visual Studio Code extension for syntax highlighting Scala sources
Scala
72
star
28

slip

obsolete — archival use only
68
star
29

compiler-benchmark

Benchmarks for scalac
Scala
68
star
30

scala-library-next

backwards-binary-compatible Scala standard library additions
Scala
66
star
31

improvement-proposals

Scala Improvement Proposals
42
star
32

scala.epfl.ch

web site for the Scala Center @ EPFL in Switzerland
SCSS
37
star
33

scala-tool-support

XML
34
star
34

hello-world.g8

Scala
27
star
35

scala-collection-laws

partially-automatic generation of tests for the entire collections library
Scala
21
star
36

scala3-cross.g8

Scala
17
star
37

scabot

Scala's PR&CI automation bot
Scala
14
star
38

sbt-scala-module

sbt plugin for scala modules.
Scala
14
star
39

scala-jenkins-infra

A Chef cookbook that manages Scala's CI infrastructure.
Shell
14
star
40

scalatest-example.g8

Scala
13
star
41

scala3-mill-example-project

Shell
12
star
42

scala-asm

A fork of https://gitlab.ow2.org/asm/asm for the Scala compiler
12
star
43

compiler-interface

a binary contract between Zinc and Scala Compilers
Scala
10
star
44

scala-partest

Legacy repo for testing framework for Scala versions <= 2.12
Scala
9
star
45

scala-asm-legacy

A fork of asm.ow2.org for the Scala compiler
Java
8
star
46

scala3-staging.g8

Scala
7
star
47

scala-modules-build

Build support for the various Scala Modules
Shell
1
star
48

dotty.epfl.ch

The nightly documentation of scala 3
HTML
1
star
49

actors-migration

Scala
1
star
50

scala-partest-interface

SBT interface to partest
1
star
51

scala-library-all

Conglomerate pom file to pull in components of Scala standard library easily.
Scala
1
star
52

scala-dist-smoketest

Smoke Test for newly created Scala distributions
Shell
1
star
53

scala3-tasty-inspector.g8

Scala
1
star