JavaFX微调器禁用空编辑器上的按钮

JavaFX微调器禁用空编辑器上的按钮,javafx,spinner,Javafx,Spinner,好的,这是我的情况。当编辑器为空时,我需要禁用两个微调器按钮,这是完成此“自定义”组件所需的最后一部分。这是我的护照 焦点丢失时:默认值设置为零,文本更新 它只接受小数点后2位的十进制值,意味着只接受货币或百分比值 没有其他补充 import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; im

好的,这是我的情况。当编辑器为空时,我需要禁用两个微调器按钮,这是完成此“自定义”组件所需的最后一部分。这是我的护照

焦点丢失时:默认值设置为零,文本更新

它只接受小数点后2位的十进制值,意味着只接受货币或百分比值

没有其他补充

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class test extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception {
        VBox v = new VBox();
        v.setPadding(new Insets(20));
        Spinner<Double> spinner = new Spinner<>();
        spinner.setEditable(true);
        Button dummy = new Button("dummy focus");
        v.getChildren().addAll(spinner,dummy);


        //----------------------------------HERE IS EVERYTHING RELATED TO THE SPINNER---------------------------------------------
        spinner.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0, 100));
        spinner.getValueFactory().setValue(0.0);
        spinner.getEditor().textProperty().addListener((obs,old,gnu)->{

            if(gnu.isEmpty()) {
                System.out.println("empty, buttons should be disabled here, they will be disabled after this ");
                spinner.getValueFactory().setValue(0.0);
                return;
            }
            System.out.println("enabling buttons");
            if(!gnu.matches("^\\d*\\.?\\d*$")) {
                try {
                    spinner.getEditor().setText(old);
                    spinner.getValueFactory().setValue(Double.parseDouble(old));
                }catch (NumberFormatException e) {
                    System.out.println("invalid string, previous value was empty, no biggie you are safe: Current value : "+spinner.getValueFactory().getValue());
                }
            } else {
                if((Double.parseDouble(gnu)*100)%1!=0) {
                    spinner.getEditor().setText(old);
                    spinner.getValueFactory().setValue(Double.parseDouble(old));                    
                }
                /*
                 * You can use this to validate inside a range, for example. PERCENTAGES : 0 ~ 100
                 *
                double val = Double.parseDouble(gnu)*100;
                if(val%1!=0 || val>10000 || val<0) {
                    spinner.getEditor().setText(old);
                    spinner.getValueFactory().setValue(Double.parseDouble(old));                    
                }
                */
            }
        });
        spinner.getEditor().setOnKeyPressed(e->{            
            switch (e.getCode()) {
                case UP:
                    spinner.increment(1);
                    break;
                case DOWN:
                    spinner.decrement(1);
                    break;

                default:
                    break;
            }
        });
        spinner.setOnScroll(e->{
            if(e.getDeltaY()>0)
                spinner.increment(1);
            else
                spinner.decrement(1);
        });
        spinner.getEditor().focusedProperty().addListener((obs,old,niu)->{
            if(!niu && spinner.getEditor().getText().isEmpty()) {
                spinner.getEditor().setText("0");
                spinner.getValueFactory().setValue(0.0);
            }
        });     
        //-----------------------------------------------------------------------------------------------------------------------------------------




        Scene sc = new Scene(v);
        primaryStage.setScene(sc);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
导入javafx.application.application;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.Spinner;
导入javafx.scene.control.SpinnerValueFactory;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类测试扩展了应用程序{
@凌驾
public void start(Stage primaryStage)引发异常{
VBox v=新的VBox();
v、 设置填充(新插图(20));
微调器微调器=新微调器();
微调器.setEditable(true);
按钮虚拟=新按钮(“虚拟焦点”);
v、 getChildren().addAll(微调器,虚拟);
//----------------------------------这是与微调器相关的所有内容---------------------------------------------
spinner.setValueFactory(新的SpinnerValueFactory.DoubleSpinnerValueFactory(01100));
spinner.getValueFactory().setValue(0.0);
spinner.getEditor().textProperty().addListener((obs,旧,gnu)->{
if(gnu.isEmpty()){
System.out.println(“空,此处应禁用按钮,在此之后将禁用它们”);
spinner.getValueFactory().setValue(0.0);
返回;
}
System.out.println(“启用按钮”);
如果(!gnu.matches(“^\\d*\.?\\d*$”){
试一试{
spinner.getEditor().setText(旧);
spinner.getValueFactory().setValue(Double.parseDouble(old));
}捕获(数字格式){
System.out.println(“无效字符串,上一个值为空,没有什么大不了的,您是安全的:当前值:”+spinner.getValueFactory().getValue());
}
}否则{
如果((Double.parseDouble(gnu)*100)%1!=0){
spinner.getEditor().setText(旧);
spinner.getValueFactory().setValue(Double.parseDouble(old));
}
/*
*例如,您可以使用它在一个范围内进行验证。百分比:0~100
*
double val=double.parseDouble(gnu)*100;
如果(val%1!=0 | | val>10000 | | val{
开关(如getCode()){
个案:
微调器增量(1);
打破
按大小写:
旋转减量(1);
打破
违约:
打破
}
});
spinner.setOnScroll(e->{
如果(如getDeltaY()>0)
微调器增量(1);
其他的
旋转减量(1);
});
spinner.getEditor().focusedProperty().addListener((obs,old,niu)->{
if(!niu&&spinner.getEditor().getText().isEmpty()){
spinner.getEditor().setText(“0”);
spinner.getValueFactory().setValue(0.0);
}
});     
//-----------------------------------------------------------------------------------------------------------------------------------------
场景sc=新场景(v);
初生阶段:西新统(sc);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}
编辑


按键和滚动事件也会发生这种情况。

恐怕您不能仅禁用微调器的按钮。但是在
编辑器之后设置值(实际上是
文本字段
)怎么样是空的?通过使用这样的解决方案,您在单击按钮后不会出现任何异常-值只是从
0
递增。我稍微修改了您的
gnu.isEmpty()
代码

if(gnu.isEmpty()) {
  System.out.println("empty, buttons should be disabled here, they will be disabled after this ");
  double valueToSet = 0.0;
  spinner.getValueFactory().setValue(valueToSet);
  Platform.runLater(() -> spinner.getEditor().setText(Double.toString(valueToSet)));
  return;
}
另一件事是,您的代码允许将“0”作为第一个数字,即使后面还有其他数字。检查该代码,应该可以解决问题(将其与以
if(!gnu.matches(“^\\d*\\.?\\d*$”)开头的整个if/else语句交换)
):

其中,
isDouble
是一种方法:

private boolean isDouble(String string) {
    boolean startsWithZero =
            string.startsWith("0") &&
                    (string.length() > 1) &&
                    (!string.startsWith("0."));
    boolean minusZeroCondition =
            string.startsWith("-0") &&
                    (string.length() > 2) &&
                    (!string.startsWith("-0."));
    boolean containsTypeSpecificLetters =
            Pattern.matches(".*[a-zA-Z].*", string);
    boolean isEmpty = string.equals("");
    boolean isMinus = string.equals("-");
    try {
        Double.parseDouble(string);
        return !(startsWithZero || minusZeroCondition || containsTypeSpecificLetters);
    } catch (IllegalArgumentException exception) {
        return isEmpty || isMinus;
    }
}

恐怕您不能仅禁用微调器的按钮。但是,在
编辑器
(实际上是
文本字段
)为空之后设置值如何?使用这种解决方案,您在单击按钮后不会出现任何异常-值只是从
0
递增。我修改了您的
gnu.isEmpty()
编码一点

if(gnu.isEmpty()) {
  System.out.println("empty, buttons should be disabled here, they will be disabled after this ");
  double valueToSet = 0.0;
  spinner.getValueFactory().setValue(valueToSet);
  Platform.runLater(() -> spinner.getEditor().setText(Double.toString(valueToSet)));
  return;
}
另一件事是,您的代码允许将“0”作为第一个数字,即使后面还有其他数字。检查该代码,应该可以解决问题(将其与以
if(!gnu.matches(“^\\d*\\.?\\d*$”)开头的整个if/else语句交换)
):

其中,
isDouble
是一种方法:

private boolean isDouble(String string) {
    boolean startsWithZero =
            string.startsWith("0") &&
                    (string.length() > 1) &&
                    (!string.startsWith("0."));
    boolean minusZeroCondition =
            string.startsWith("-0") &&
                    (string.length() > 2) &&
                    (!string.startsWith("-0."));
    boolean containsTypeSpecificLetters =
            Pattern.matches(".*[a-zA-Z].*", string);
    boolean isEmpty = string.equals("");
    boolean isMinus = string.equals("-");
    try {
        Double.parseDouble(string);
        return !(startsWithZero || minusZeroCondition || containsTypeSpecificLetters);
    } catch (IllegalArgumentException exception) {
        return isEmpty || isMinus;
    }
}

我还在寻找一种只禁用按钮的方法。我需要禁用更改
微调器的值,同时仍然允许复制/粘贴,所以我在源代码中做了一些挖掘

我发现,虽然按钮实际上不是
微调器
对象本身的一部分,但它们是
微调器
微调器皮肤
(它们也不是
按钮
,而是
堆栈窗格
的一部分),因此我仅通过以下方式禁用了按钮(在Kotlin中):

val editing=SimpleBoleAnProperty(false)
val微调器=微调器()
spinner.skinProperty().addListener{可观察,oldValue,newValue->
//仅当蒙皮是“SpinnerSkin”的实例时绑定`
如果(newValue!=null&&newValue为喷丝头皮肤)