通过使用其他控制器来操纵JavaFX控制器

时间:2018-07-25 10:08:25

标签: java javafx

我的JavaFX应用程序中有这种情况。当用户操作它们时,有2个SliderToggleGroupRadioButton中的一个在acquisitionTimeValueLabel中返回值。我的问题是:是否有办法在获取时间中也使用Slider?因此,我可以直接设置它,其他控制器也会受到它的影响。更清楚地说,我想选择也具有逆函数。

我也不太确定问题的标题,因此非常欢迎编辑。

我的代码示例:

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];
}
}

1 个答案:

答案 0 :(得分:3)

如果我理解您的问题,您想从3个不同的来源编辑标签值,第三个是直接编辑会影响前两个标签的标签值:
因此,您可以像这样使第三个滑块和listen改变其值:

    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
        }
    });

在这种方法中,您可以执行逻辑来编辑其他滑块的值。
希望对您有帮助