简体   繁体   English

JavaFX:有没有办法只禁用微调器的一个按钮?

[英]JavaFX: Is there a way to disable only one button of the Spinner?

I want to disable just one arrow-button of the JavaFX Spinner component , so that they cannot assume illegal values: I have 2 Spinner components "Min and "Max" with [2-6] as range of values, as in this picture ; the behaviour I want is that when they get to the same value (eg Min: 3, Max: 3) the up arrow of Min becomes disabled, aswell as the down arrow of Max.我只想禁用JavaFX Spinner 组件一个箭头按钮,这样它们就不能假定非法值:我有 2 个 Spinner 组件“Min 和“Max”,其值范围为 [2-6],如图所示;我想要的行为是,当它们达到相同的值(例如 Min:3,Max:3)时,Min 的向上箭头以及 Max 的向下箭头被禁用。

Wanted behaviour通缉行为

Anyone knows if this is possible or how can I achieve that in the smoothest way possible?任何人都知道这是否可能,或者我怎样才能以最顺利的方式实现这一目标?

Edit: Thank jewelsea for the suggestion.编辑:感谢jewelsea的建议。 I've added a listener to the valueProperty and set the valueFactory to change the range and it works as expected, even though it still doesn't disable and "gray out" the arrow, which is the behaviour I would like to achieve (but at this point I'm wondering if it is even possible).我已经向 valueProperty 添加了一个侦听器并将 valueFactory 设置为更改范围并且它按预期工作,即使它仍然没有禁用和“灰色”箭头,这是我想要实现的行为(但是在这一点上,我想知道这是否可能)。

spinnerMin.valueProperty().addListener((changed, oldval, newval) -> {
    spinnerMax.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(newval, 6, spinnerMax.getValue()));
});

spinnerMax.valueProperty().addListener((changed, oldval, newval) -> {
    spinnerMin.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(2, newval, spinnerMin.getValue()));
});

It is definitely possible to style the buttons as per the value in the spinner.绝对可以根据微调器中的值设置按钮样式。

Below is one way you can accomplish the required behavior.以下是您可以完成所需行为的一种方法。

The general idea is to set some pseudo states to the Spinner when the min/max values are reached.一般的想法是在达到最小/最大值时为Spinner设置一些伪状态。 And style the arrow buttons based on the pseudo states.并根据伪状态设置箭头按钮的样式。

Below is the sample demo of the above approach.下面是上述方法的示例演示。

在此处输入图像描述

import javafx.application.Application;
import javafx.css.PseudoClass;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SpinnerDemo extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        PseudoClass minPseudo = PseudoClass.getPseudoClass("minvalue");
        PseudoClass maxPseudo = PseudoClass.getPseudoClass("maxvalue");

        Spinner<Integer> spinner = new Spinner<>();
        spinner.getStyleClass().add(Spinner.STYLE_CLASS_SPLIT_ARROWS_VERTICAL);
        SpinnerValueFactory.IntegerSpinnerValueFactory valueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(2, 5);
        spinner.valueProperty().addListener((obs, old, val) -> {
            spinner.pseudoClassStateChanged(minPseudo, val == valueFactory.getMin());
            spinner.pseudoClassStateChanged(maxPseudo, val == valueFactory.getMax());
        });
        spinner.setValueFactory(valueFactory);

        StackPane root = new StackPane(spinner);
        root.setPadding(new Insets(15));
        Scene sc = new Scene(root, 250, 200);
        sc.getStylesheets().add(getClass().getResource("spinner.css").toString());
        stage.setScene(sc);
        stage.setTitle("Spinner");
        stage.show();
    }
}

CSS code: CSS 代码:

.spinner:maxvalue .increment-arrow-button {
    -fx-background-color: -fx-outer-border, #999999;
}
.spinner:minvalue .decrement-arrow-button {
     -fx-background-color: -fx-outer-border, #999999;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM