0

Does Swing have something similar to vaadin's LAZY value change mode?

As in "update on every change but after a short delay and cancel the event when the value changes again before the event has finished"?

We have a float-type text field that we bind to a property like

binder.bindProperty(MY_FOO).converted(floatToString()).to(view.myFoo, ON_KEY_TYPED);

where binder is a PresentationModelBinder and

public static BindingConverter<Float, String> floatToString() {
    return BindingConverters.<Float, String>fromModelAs(BindingConverters::toString).andFromViewAs(BindingConverters::parseFloat);
}

static <T> String toString(T anyValue) {
    return (anyValue == null) ? null : anyValue.toString();
}

static <T> String toString(Float floatValue) {
    return (floatValue == null) ? null : String.format("%.2f", floatValue);
}

Let's say you want to type 12

  • you type 1
  • Swing immediately exchanges that for 1.00 and sets the cursor back to the beginning of the field
  • you type 2
  • whoops, you set it to 21

But it gets worse ...

Let's say you want to write 1.2

  • you type 1
  • Swing immediately exchanges that for 1.00 and sets the cursor back to the beginning of the field
  • you type .
  • decimals are (in this app) defined to start with numbers, dot is not a number, so swing erases the invalid input
  • you type 2
  • whoops, you set it to 2

Which is a bad user experience and not what I want.

Looking at com.jgoodies.binding.binder.ValueModelBindingBuilder, I see only two options, ON_FOCUS_LOST -- which doesn't what we want -- and ON_KEY_TYPED -- which very clearly ALSO doesn't what we want.

Is there something (in Swing) that would provide us with the functionality we need or do we have to implement our own?

(And before you ask, YES, we're going to get rid of Swing in a few months.)

Frakcool
  • 10,915
  • 9
  • 50
  • 89
User1291
  • 7,664
  • 8
  • 51
  • 108

0 Answers0