Tag Archives: software

ScalaFX 8u40 – TextFormatter – part 1

Upcoming release of JavaFX 8u40 will bring several enhancements, including dialogs and new spinner control. ScalaFX started new branch, SFX8u40, to support JavaFX 8u40. Earlier post described Spinner control. In this two-part post we will cover TextFormatter and related features.

TextFormatter lets you edit custom values in text controls, like TexFIeld, Text Area, and other derived from TextInputControl. Say that you want to use a TextField to enter numbers. The new `textFormatter` property lets you directly interact with a number in a TextInputControl rather than its textual representation. For instance, you can bind other properties to that number.

Let’s look at a simple example where a TextField represent some amount of a currency, say US dollars. First thing is to create a StringConverter for converting between string like “$67.98” and a number. We can use number format provide by Java’s NumberFormat:

val currencyFormat = NumberFormat.getCurrencyInstance(Locale.US)
val converter = new FormatStringConverter[Number](currencyFormat)

To see how binding works, lets create a simple slider:

val slider = new Slider(0, 10000, 1000)

TextField can bind it’s a numeric value to the slider. The numeric value is accessible though textFormatter’s value property:

new TextField {
  textFormatter = new TextFormatter (converter) {
    value <==> slider.value

Here is ScalaFX demo application that displays text field (formatted as a currency) and a slider that can modify value of the text field.

011 - ScalaFX 8u40 - TextFormatter

import java.text.NumberFormat
import java.util.Locale

import scala.language.implicitConversions
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.scene.control.{Slider, TextField, TextFormatter}
import scalafx.scene.layout.{Region, VBox}
import scalafx.util.converter.FormatStringConverter

object TextFormatterDemo extends JFXApp {

  val slider = new Slider(0, 10000, 1000)

  val textField = {
    val currencyFormat = NumberFormat.getCurrencyInstance(Locale.US)
    val converter = new FormatStringConverter[Number](currencyFormat)
    new TextField {
      textFormatter = new TextFormatter(converter) {
        value <==> slider.value
      maxWidth = 140
      maxHeight = Region.USE_COMPUTED_SIZE

  stage = new PrimaryStage {
    scene = new Scene(300, 200) {
      title = "TextFormatter Demo"
      root = new VBox {
        content = Seq(textField, slider)
        padding = Insets(20)
        spacing = 12


The TextFormatter support is available in latest SNAPSHOT releases of ScalaFX 8u40. To use it add following to your SBT build file:

libraryDependencies += "org.scalafx" %% "scalafx" % "8.0.40-SNAPSHOT"

Above we showed a simpler scenario of using TextFormatter where we specified string converter. There are another ways, with higher degree of control. TextFormatter can have a filter that monitors and modifies changes to the text, caret, and selection before they are displayed on the screen. This will be subject of the part 2 of this blog post.

What is New in ScalaFX 8 Milestone 1

ScalaFX helps you simplify creation of JavaFX-based user interfaces in Scala. ScalaFX uses a simple, hierarchical pattern for creating new objects and building up the scene graph. ScalaFX supports full interoperability with Java and can run anywhere the Java Virtual Machine (JVM) and JavaFX are supported.

ScalaFX 8 provides support for JavaFX that is part of the upcoming release of Java 8. Wherever possible ScalaFX 8 shares code with ScalaFX 1, in particular, ScalaFX 8 M1 incorporates improvements from ScalaFX 1 M5.

JavaFX 8, beside many new features, introduced some backward compatibility breaking changes. The two main goals of this first milestone of ScalaFX 8 were: 1) make existing code work with Java 8, fix and issues related to failing tests, 2) add some key wrappers for the new 3D functionality. For instance, JavaFX 8 deprecated Builders and actually removed some of them. ScalaFX 8 code was modified not to use Builders where appropriate. This was a particular problem for many tests that relied on Builders.

ScalaFX 8 was tested with Java 8 early access build 106, more info can be found on the ScalaFX 8 Development page.

Summary of changes compared to ScalaFX 1.0 Milestone 5

  • Tested against Java 8 early access build 106 (September 3, 2013)
  • Ported existing code from ScalaFX 1 Milestone 5
  • Some class hierarchies were changed to match changes in JavaFX 8
  • About 27 new rappers were added, initially mostly for the new 3D functionality.
  • 4 new demos added to illustrate the new 3D API.
  • Over 30 existing classes were modified to add new properties and methods.

Changes in inheritance hierarchy to match changes in JavaFX 8

  • `Camera` extends `Node`
  • `Control` extends `Region`

Many new wrappers added with corresponding implicit conversions and tests

  • scalafx.collections
    • ObservableArray
    • ObverservableFloatArray
    • ObservableIntegerArray
  • scalafx.geometry.NodeOrientation
  • scalafx.scene
    • AmbientLight
    • LightBase
    • PointLight
    • SubScene
  • scalafx.scene.control.TableColumnBase
  • scalafx.scene.input.PickResult
  • scalafx.scene.layout
    • Background
    • Border
  • scalafx.scene.paint
    • Material
    • PhongMaterial
  • scalafx.scene.shape
    • Box
    • CullFace
    • Cylinder
    • DrawMode
    • Mesh
    • MeshView
    • Shape3D
    • Sphere
    • TriangleMesh
  • scalafx.scene.transform
    • MatrixType
    • NonInvertibleTransformException
    • TransformChangedEvent
  • scalafx.stage.Stage

New properties and `enum`s added to existing wrappers

AreaChart, ComboBox, ConditionalFeature, EventType, FileChooser, Font, Image, Labelled, ListView, MediaPlayer, MouseEvent, Node, Region, PerspectiveCamera, PopupWindow, Scene, SceneProperty, StackedAreaChart, Stage, StageStyle, Tab, TableView, TreeItem, TreeView, TextBoundsType, TextInputControl, Text, Transform, WebEngine, WebView.

New demos for 3D functionality

Where to Get It

You can download ScalaFX binaries and source code from the project website.

Artifacts are also published on the Maven Central, so you can use them from you favorite build system SBT, Gradle, Maven, etc.