0

I'm using Griffon 1.2.0 and JavaFX, and I'd like to bind a textfield to a number. I thought I'd be able to do it simply by defining the model property as a Float, but it doesn't seem to like that, even if I define a converter. I looked into the Validator plugin but that only seems to work for Swing (not JavaFX) - which is a shame because this presentation makes it look really good: http://www.slideshare.net/ecspike/introduction-to-griffon (page 67 shows exactly the kind of functionality I'd like).

In the meantime, I've just added a property change listener in the noparent block of my view as suggested here What is the recommended way to make a numeric TextField in JavaFX?:

amount2.textProperty().addListener({ ObservableValue<? extends String> observable, String oldValue, String newValue ->
    try {
        Integer.parseInt(newValue);
    } catch (Exception e) {
        observable.setValue(oldValue);
    }
} as ChangeListener<String>)

This ensures the user can only enter numbers, but are there better options? I haven't found anything in the JavaFX space that rivals the Validation plugin functionality - should I give up on JavaFX and go back to Swing?

Community
  • 1
  • 1
prule
  • 2,536
  • 31
  • 32

1 Answers1

0

I thought this functionality would be already provided by GroovyFX's @FXBindable and bind() node, that is

 class SampleModel {
     @FxBindable float number
 }

 // View
 textField(text:bind(target: model, 'number', converter: { v -> /* insert converter code here */))

considering that GroovyFX's bind() is a copy of SwingBuilder's bind().

Andres Almiray
  • 3,236
  • 18
  • 28
  • I tried this // model @FXBindable float test // view textField(text:bind(target: model, 'test'), converter: model.convertFloat) But it dies violently with 2013-02-25 11:33:46,614 [AWT-AppKit] ERROR org.codehaus.griffon.runtime.builder.UberBuilder - An error occurred while building moneytracker.MoneytrackerView@276acd3b groovy.lang.MissingPropertyException: No such property: target for class: groovyx.javafx.binding.BindingHolder # # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x000000010d9a5425, pid=6373, tid=1799 # # JRE version: 7.0_07-b10 – prule Feb 25 '13 at 00:36