如何从Spinner控制箭头向上和向下按钮?
当我点击向上/向下按钮并且文本为空/空
时,我想防止出现这种情况答案 0 :(得分:0)
好的,ManoDestra解决了问题。这是解决方案 - https://stackoverflow.com/a/36550660/5885019
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory;
import javafx.stage.Stage;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
public class Spin extends Application {
Spinner<Integer> spinner;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage aPrimaryStage) throws Exception {
IntegerSpinnerValueFactory valueFactory = new IntegerSpinnerValueFactory(0, 10);
spinner = new Spinner<>(valueFactory);
spinner.setEditable(true);
spinner.valueProperty().addListener((observableValue, oldValue, newValue) -> handleSpin(observableValue, oldValue, newValue));
aPrimaryStage.setScene(new Scene(spinner));
aPrimaryStage.show();
}
private void handleSpin(ObservableValue<?> observableValue, Number oldValue, Number newValue) {
try {
if (newValue == null) {
spinner.getValueFactory().setValue((int)oldValue);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}