Giter VIP home page Giter VIP logo

Comments (10)

vlaaad avatar vlaaad commented on May 25, 2024 1

Hmm, that's a bit more involved: you'll need to create a prop for observing these changes:

(require '[cljfx.prop :as fx.prop]
         '[cljfx.mutator :as fx.mutator]
         '[cljfx.lifecycle :as fx.lifecycle])

(import '[javafx.scene.control Spinner])

(def ext-with-spinner-props
  (fx/make-ext-with-props
    {:on-spinner-text-changed (fx.prop/make (fx.mutator/property-change-listener
                                              #(.textProperty (.getEditor ^Spinner %)))
                                            fx.lifecycle/change-listener)}))

(fx/on-fx-thread
  (fx/create-component
    {:fx/type :stage
     :showing true
     :scene {:fx/type :scene
             :root {:fx/type ext-with-spinner-props
                    :props {:on-spinner-text-changed prn}
                    :desc {:fx/type :spinner
                           :value-factory {:fx/type :integer-spinner-value-factory
                                           :min 0
                                           :max 10}}}}}))

from cljfx.

vlaaad avatar vlaaad commented on May 25, 2024

Hi! Cell factories in examples you linked showcase functions that return component descriptions, but in case of a spinner, it's different, although it's also called a factory.

Value factory on a spinner is just a regular component, you can describe it like that:

{:fx/type :spinner
 :value-factory {:fx/type :integer-spinner-value-factory
                 :min 0 
                 :max 100}}

Thank you for your kind words and you are welcome :)

from cljfx.

kxygk avatar kxygk commented on May 25, 2024

I somehow missed that. And how do I then hook up an event to the track the changing value ? 😅

https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Spinner.html#editorProperty--

It seems I need to access the "Editor" Textfield and bind an event to the :on-action hook. But I don't understand how to access elements like that within the data-driven model of CLJFX

from cljfx.

kxygk avatar kxygk commented on May 25, 2024

Sorry for the slow reply. Thank you for this. I'd hack in something workable with the :text-input-field. I tried to switch to your spinner b/c it's a bit better at first blush, but it will still "passes through" everything you type in to the event handler (like numbers from outside the range). It also blows up if you input text and hit enter. So in the end either way I'm stuck massaging/validating input :)

But thank you for showing how it's done! Much appreciated

from cljfx.

vlaaad avatar vlaaad commented on May 25, 2024

Hey, I just had a look at Spinner's javadoc, and it has read-only value property that can be observed similarly, maybe that's what you want?

from cljfx.

vlaaad avatar vlaaad commented on May 25, 2024

Hey @geokon-gh!
I just wanted to let you know that 1.6.8 just got released that adds support for observing spinner value changes, like that:

{:fx/type :spinner
 :value-factory {:fx/type :integer-spinner-value-factory
                 :min 0
                 :max 10
                 :value 5
                 :on-value-changed prn}}

or alternatively, on :spinner itself:

{:fx/type :spinner
 :value-factory {:fx/type :integer-spinner-value-factory
                 :min 0
                 :max 10
                 :value 5}
 :on-value-changed println}

from cljfx.

kxygk avatar kxygk commented on May 25, 2024

Oh great! It's on my TODO list :))

from cljfx.

kxygk avatar kxygk commented on May 25, 2024

It works great and it's much better than what I had implemented manually. I still need an additional input validation step but it's fine

There is just one minor issue. If you enter a non-valid number (outside the range) then the spinner will gracefully handle the situation. But if you input a string then you get an uncatchable exception:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.text.ParseException: Unparseable number: "f118"
	at javafx.scene.control.SpinnerValueFactory$DoubleSpinnerValueFactory$1.fromString(SpinnerValueFactory.java:720)
	at javafx.scene.control.SpinnerValueFactory$DoubleSpinnerValueFactory$1.fromString(SpinnerValueFactory.java:692)

The app keeps running and my event isn't called, so it "works" but I do get a stack dump on the console

from cljfx.

vlaaad avatar vlaaad commented on May 25, 2024

Not sure I can do anything about that — this seems to be a JavaFX issue. I know there is another way to input numbers that might be useful: text formatter. It does not allow limiting numbers to a range, but I don't remember it throwing exceptions when I tried typing invalid numbers.

from cljfx.

kxygk avatar kxygk commented on May 25, 2024

I finally got around to using a text-formatter and a text-field and the result is much cleaner. Thank you for the pointer @vlaaad ! It does lack the little buttons and up/down arrow adjustments, but it's a small loss

Appreciate your insight and help as always

from cljfx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.