What is the best type of field? Textfield, slider, ...? For value above 0.1?

Asked

Viewed 32 times

1

I made this layout as an example of a problem I’m having in creating a system, well, in the first value field (TextField) I cannot let the user enter the value zero, will only be allowed 0.1 up, as I do to resolve this?

Wish it were allowed only 0.1 up, as it is currently causing system error.

I even thought of creating a Slider, as in the photo, where the user would just touch the Slider from 0.1, but another problem arose:

I wanted the figures to be as follows,

0.1 / 0.5 / 1 / 1.5 / 2 / ...

How could I do this using Slider? I’ve been changing in properties as it is in the picture, but without success.

In this case, what is the best type of field? TextField, Slider, ...? For value above 0.1? And preferably, 0.1 / 0.5 / 1 / 1.5 / 2 / ...

scene builder com slider, textfield

1 answer

0


You can achieve this by using the component Spinner. Look for it in the component tab of your Scenebuilder and configure it that way in your controller:

@FXML
private Spinner spinner;

@Override
public void initialize(URL url, ResourceBundle rb) {

    SpinnerValueFactory<Double> factory = new SpinnerValueFactory<Double>() {
        @Override
        public void decrement(int steps) {
            Double current = this.getValue();
            if(current - 0.5 > 0.1){
                this.setValue(current - 0.5);
            }else{
                this.setValue(0.1);
            }
        }

        @Override
        public void increment(int steps) {
            Double current = this.getValue();
            Double newValue = (current == 0.1) ? current + 0.4 : current + 0.5;
            this.setValue(newValue);
        }
    };
    // Define o valor inicial (sem essa linha ocorre NullPointerException)
    factory.setValue(0.1);
    // Define o factory de valores
    spinner.setValueFactory(factory);
}

The result is as follows:

Resultado

  • Wow, thank you so much, perfect..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.