Using a ScalaFX InvalidationListener

by 5/30/2012 09:06:00 PM 0 comments
I am slowly working through Oracle's JavaFX tutorials at http://docs.oracle.com/javafx/ using ScalaFX. For those who don't know, ScalaFX is a Scala DSL for creating JavaFX applications. It's pretty outstanding. JavaFX is a nice UI toolkit, but ScalaFX makes the code very, very brief and easy to understand. As an example, below is a code sample from http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm that demonstrates using JavaFX InvalidationListeners. Below that I've also posted the ScalaFX version.

JavaFX

package bindingdemo;
 
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.binding.NumberBinding;
import javafx.beans.binding.Bindings;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
 
class Bill {
 
    // Define the property
    private DoubleProperty amountDue = new SimpleDoubleProperty();
 
    // Define a getter for the property's value
    public final double getAmountDue(){return amountDue.get();}
 
    // Define a setter for the property's value
    public final void setAmountDue(double value){amountDue.set(value);}
 
     // Define a getter for the property itself
    public DoubleProperty amountDueProperty() {return amountDue;}
 
}
 
public class Main {
 
    public static void main(String[] args) {
 
        Bill bill1 = new Bill();
        Bill bill2 = new Bill();
        Bill bill3 = new Bill();
 
        NumberBinding total =
          Bindings.add(bill1.amountDueProperty().add(bill2.amountDueProperty()),
              bill3.amountDueProperty());
        total.addListener(new InvalidationListener() {
 
        @Override public void invalidated(Observable o) {
                System.out.println("The binding is now invalid.");
            }
        });

        // First call makes the binding invalid
        bill1.setAmountDue(200.00);

        // The binding is now invalid
        bill2.setAmountDue(100.00);
        bill3.setAmountDue(75.00);

        // Make the binding valid...
        System.out.println(total.getValue());

        // Make invalid... 
        bill3.setAmountDue(150.00);

        // Make valid...
        System.out.println(total.getValue());
    }
}

ScalaFX

package bindingdemo

import scalafx.beans.BeanIncludes._
import scalafx.beans.Observable
import scalafx.beans.property.DoubleProperty

/**
 * http://docs.oracle.com/javafx/2/binding/jfxpub-binding.htm
 * 
 * @author Brian Schlining
 * @since 2012-05-30
 */

object InvalidationApp {

    class Bill {
        val amountDue: DoubleProperty = new DoubleProperty(this, "amount")
    }

    def main(args: Array[String]) {

        val bill1 = new Bill
        val bill2 = new Bill
        val bill3 = new Bill

        val total = bill1.amountDue + bill2.amountDue + bill3.amountDue
        total.onInvalidate { op: Observable =>
            println("The binding is now invalid")
        }

        // 1st call makes the binding invalid
        bill1.amountDue.value = 200

        // The binding is now invalid
        bill2.amountDue.value = 100
        bill3.amountDue.value = 75

        // Make the binding valid...
        println(total.value)

        // Make invalid...
        bill3.amountDue.value = 150

        // Make valid...
        println(total.value)

    }

}

hohonuuli

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

0 comments: