Tag Archives: java

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.

Developing ImageJ plugins with SBT using sbt-imagej

ImageJ is open source image processing and analysis software. It is written primarily in Java. One of its killer features is the ability to be extended by adding plugins. Simple plugins are just a couple lines of macro code in a text file. Complex ones built from thousands lines of code a packaged in a Java jar with a special manifest. They can depend on external libraries or other plugins. Packaged plugins can be written in Java, Scala, or other JVM languages.

Developing ImageJ plugins is quite simple. Though, one annoyance comes when you want to run/debug your plugin from within ImageJ. This is due to the way ImageJ discovers plugins and dependencies. It looks into its runtime directory for subdirectory called `plugins`. Then searches the `plugins` directory (or its direct subdirectories) for jars and other files containing plugins. The annoyance is that you have to package the plugin you are working on into a jar and then copy it, and possibly also all dependencies, to ImageJ `plugins` directory.

Good thing is that you can run ImageJ from you project pointing a custom runtime directory you create. You just need to copy your plugin jars there every time you build you project and run ImageJ. It is handy to have a tool that does all the work, even better if it integrates with your IDE of choice.

sbt-imagej plugin for SBT

I am using SBT as my build system. It is works with Scala, Java, and other languages. It is much simpler than Ant or Maven, in a way it is similar to Gradle.

In the past I creating a couple of ImageJ plugins project in SBT. The setup for preparing ImageJ for testing a plugin was quite similar. I was just copying and pasting configuration to new projects. Since SBT plugins are relatively easy to create, I decided to write one for development of ImageJ plugins, called sbt-imagej. The idea was to have tool that is easy to setup that works in most cases with ability to customize, if needed. Using the sbt-imagej plugin is as simple as declaring and enabling it in your build. Here is a complete example of SBT build file (`build.sbt`) that can build, setup, and run ImageJ project:

// Your project information
name := "awesome-imagej-plugin"

version := "1.0"

// Add dependency on ImageJ
libraryDependencies += "net.imagej" % "ij" % "1.49k"

// Enable sbt-imagej plugin tasks

The only thing that needs to be added to `build.sbt` is the line containing `enablePlugins(SbtImageJ)`. This enables two tasks `ijRun`, `ijPrepareRun`, and a bunch of customization options.

To let SBT know where to find `SbtImageJ`, you will also need to add `sbt-imagej` declaration to `project/settings.sbt` file:

addSbtPlugin("net.sf.ij-plugins" % "sbt-imagej" % "2.0.0")

Though not strictly necessary, it is also recommended specifying which version of SBT you are using, so also add `project/build.properties` with a single line:


Those three files: `build.sbt`, `project/settings.sbt`, and `project/build.properties` constitute complete specification of a SBT build project. It can be used in IntelliJ IDEA, Eclipse, and NetBeans.

The sbt-imagej plugin provides two tasks:

  • `ijPrepareRun` – it builds your plugin and copies it to proper directory, but does not run ImageJ. It is intended to be run from your IDE before run/debug.
  • `ijRun` – to builds your plugin, copies it to proper directory (using `ijPrepareRun`) and runs ImageJ. It is intended for running from a command prompt.

There is also a bunch of configuration options, they are described on the sbt-imagej project page.

OK but where do you put your source code. SBT, by default, is using the same directory structure as Maven and Gradle. Your sources are in `src\main`. If you are using Java, the code will go into `src\main\java`. If you are using Scala, into `src\main\scala`, and so on. One important thing to keep in mind is that resources go into `src\main\resources`, so your plugin manifest needs to go into `src\main\resources\plugin.properties`. You can see that in the example project. It contains two plugins that add effects to color images “Sepia Tone”, and “Vignette”. The plugin manifest src\main\resources\plugin.properties looks as follows:

Plugins>Effects, "Sepia Tone", effects.SepiaTonePlugIn
Plugins>Effects, "Vignette", effects.VignettePlugIn

It indicates that the plugins will be install under ImageJ menu `Plugin > Effects`.

You can write ImageJ plugins in different languages. For the sake of illustration one plugin is written in Java (src/main/java/effects/VignettePlugIn.java) and the other in Scala (src/main/scala/effects/ SepiaTonePlugIn.scala)

You can find complete example project in the `sbt-imagej` example directory.

Using SBT from command line

To run the example code from SBT command prompt, start SBT (in the project directory) and type command `ijRun`. This will download needed dependencies, build the plugins, package them, copy jar to `plugins` directory, and start ImageJ with plugins loaded into menu Plugins > Effect.

ImageJ runing

Integrating with an IDE

The basic idea of integration is to execute ` ijPrepareRun` before run/debug. It can be done from all major IDEs: Eclipse, IntelliJ IDEA, and NetBeans. IntelliJ IDEA, and NetBeans support SBT projects, you can load them directly using Scala plugins for each IDE. Eclipse requires additional step of generating Eclipse project from SBT project.

IntelliJ IDEA

You will need to add two plugins to IDEA “Scala” (for loading SBT projects into IDE) and “SBT” (for executing SBT tasks). To add the plugins select File > Settings > Plugins > Browse Repositories. In the search box type “SBT” then install “Scala” and “SBT” plugins. Restart IDE.

IntelliJ plugins

Load/import the example projects. To run or debug ImageJ you will need to create new run configuration. Select Run > Edit Configurations. Then:

  1. Click on “+” in top left to create new configuration and select “Application”
  2. Give you configuration a name, say “ImageJ”
  3. As the main class select ij.ImageJ (The ImageJ’s main class)
  4. Select your working directory, here we use the default value “sandbox”
  5. Select the module, here it is called “example”
  6. Now the critical part. In the “Before launch” configuration are in the bottom, click on “+” and select “SBT”. Replace the content of the combo box typing “ijPrepareRun” hit Enter (it is important) then click OK.
    010 - IntelliJ SBT task

At his point you should have your run/debug configuration ready.

010 - IntelliJ Run Configuration

Click OK to save it.

Now you can run or debug ImageJ with your plugin. First you run can take a bit longer as SBT setups the build, after that, runs should be pretty quick. You can of cause also run in debug mode too, putting breakpoints in your plugin code.


Unlike IntelliJ IDEA or NetBeans, at the time of writing, Eclipse cannot directly read SBT projects. However you can generate Eclipse project using sbteclipse plugin. Then you load it into Eclipse as any other project.

Eclipse cannot directly run SBT tasks, but it can run Ant targets, so we use a simple Ant target to start SBT task. A sample Ant file is provided with the example. So right click on the `build.xml` and select “Run as Ant build…”. In the Edit Configuration dialog select “sbt-imagej-run” and click Run.

010 - Eclipse run configuration

Same thing works for debug.


Similar to IntelliJ IDEA, NetBeans can load SBT projects directly using its Scala plugin. So first install the plugin, actually it is divided in several smaller plugins, so you will need to install all the pieces. Go to Tools > Plugins > Available Plugins. In the search box type “Scala”, select all the plugins in “Scala” category and click “Install”. After you restart NetBeans you will be able to simply load SBT project as regular NetBeans project.

010 - NetBeans load project

To build and run ImageJ you will use SBT console provided by the Scala plugin. In the console type `ijRun` and press “Enter”

010 - NetBeans SBT console

For the debugging in NetBeans you will need to attach debugger to the running process.


Testing ImageJ plugins during development requires packaging and copying around plugin jars and dependencies. sbt-imagej plugin for SBT simplifies project setup. It can build and copy files as needed. It can also be integrated into your IDE run/debug process. You can find additional information about, including custom settings, on the sbt-imagej project page.