Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulate JavaFX controllers from the use of another controller

Tags:

java

javafx

I have this situation in a part of my JavaFX application. There are 2 Sliders and a ToggleGroup of RadioButtons that are returning a value in acquisitionTimeValueLabel when they are manipulated by the user. My question is: is there a way to have a Slider also for the acquistion time? So I can set it directly and the other controllers will be influenced from it. To be more clear, I would like to have the option to have also the inverse function.

Also I am not very sure about the title of the question, so edits are very welcomed.

An example of my code:

public class dasd {

private RadioButton first;
private RadioButton second;
private RadioButton third;
private ToggleGroup group;


private Slider ramWidthSlider = new Slider(0, 255, 0);
private Label ramWidthValueLabel = new Label("256 bytes (64 words)");

private Slider sampleRateSlider = new Slider (0, 15, 0);
private Label sampleRateValueLabel = new Label("13560 KHz");

private Label acquisitionTimeValueLabel = new Label ("some value");

private static final String[] STRINGS_FREQ = new String[16];
private static final String[] STRINGS_MEM = new String[256];



@FXML
private void initialize(){

    first.setToggleGroup(group);
    second.setToggleGroup(group);
    third.setToggleGroup(group);


    group.selectedToggleProperty().addListener((ov, old_toggle, new_toggle) -> {

        if (group.getSelectedToggle() != null) {
                        }
    });

    fillTheClock();
    fillTheRam();

    ramWidthValueLabel.textProperty().bind(
            Bindings.createStringBinding(
                    () -> getDisplayMemory((int) Math.round(ramWidthSlider.getValue())),
                    ramWidthSlider.valueProperty()
            )
    );


    sampleRateValueLabel.textProperty().bind(
            Bindings.createStringBinding(
                    () -> getDisplayFrequency((int) Math.round(sampleRateSlider.getValue())),
                    sampleRateSlider.valueProperty()
            )
    );

    acquisitionTimeValueLabel.textProperty().bind(
            Bindings.createStringBinding(
                    () -> String.format("%.3f", (pow(2, (group.getToggles().indexOf(group.getSelectedToggle()))))
                                    * (pow(2, sampleRateSlider.getValue()) / 13.56) * 64 * (ramWidthSlider.getValue() + 1) ) + " µs of acquisition",
                            group.selectedToggleProperty(), ramWidthSlider.valueProperty(), sampleRateSlider.valueProperty()
                    )
            ); 
}        




/**\
 * Methods for displaying the values of the Sample Rate Slider
 *
 */
private static void fillTheClock() {
    double frequency = 13560;
    for (int i = 0; i <= 15; i++) {
        STRINGS_FREQ[i] = Double.toString(frequency) + " KHz";
        frequency = frequency / 2;
    }
}

private static String getDisplayFrequency(int value) {
    return STRINGS_FREQ[value];
}


/**
 * Methods for displaying the values of Memory Width Slider
 *
 */
private static void fillTheRam() {
    int memory = 256 ;
    for (int i = 0; i <= 255; i++) {
        STRINGS_MEM[i] = Integer.toString(memory) + " bytes (" + Integer.toString(memory/4) + " words)";
        memory = memory + 256;
    }
}

private static String getDisplayMemory(int value){
    return STRINGS_MEM[value];
}
}
like image 909
dadadima Avatar asked Jul 25 '18 10:07

dadadima


1 Answers

If I understand your question will you want to edit a label value from 3 different sources the third one is directly edit the label value which will affect the first two:
So you can make the third slider and listen to the changes of it's value like so:

slider.valueProperty().addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
        //write your logic here where newValue if the current value of the slider
    }
});

in this method you can do your logic to edit the values of the other sliders.
I hope it helps

like image 58
Ahmed Emad Avatar answered Dec 01 '22 00:12

Ahmed Emad