对于数字输入,将模拟控件同步到文本显示有时会很方便。在此Swing example中,JSpinner
和JSlider
分别监听更改事件,并分别更新对方的模型以进行匹配。如下所示,一个类似的JavaFX程序连接了 Spinner
和 Slider
,这些监听器使控件保持协调:
slider.valueProperty().addListener((Observable o) -> {
spinner.getValueFactory().setValue(slider.getValue());
});
spinner.valueProperty().addListener((Observable o) -> {
slider.setValue((double) spinner.getValue());
});
不幸的是,当我在微调器的
StringConverter
中添加SpinnerValueFactory
时,初始值是未格式化的,直到更改了任何一个控件为止-即使在添加转换器后再次显式设置初始值时:spinner.getValueFactory().setConverter(…);
spinner.getValueFactory().setValue(INITIAL_VALUE);
我要去哪里错了?
import java.text.NumberFormat;
import javafx.application.Application;
import javafx.beans.Observable;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.Spinner;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.converter.PercentageStringConverter;
/**
* @see https://stackoverflow.com/a/6067986/230513
*/
public class SpinSlider extends Application {
private static final double MIN = 0;
private static final double MAX = 1;
private static final double INITIAL_VALUE = 0.5;
private static final double STEP_INCREMENT = 0.1;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SpinSlider");
Slider slider = new Slider(MIN, MAX, INITIAL_VALUE);
slider.setBlockIncrement(STEP_INCREMENT);
Spinner spinner = new Spinner(MIN, MAX, INITIAL_VALUE, STEP_INCREMENT);
spinner.getValueFactory().setConverter(
new PercentageStringConverter(NumberFormat.getPercentInstance()));
spinner.getValueFactory().setValue(INITIAL_VALUE);
slider.valueProperty().addListener((Observable o) -> {
spinner.getValueFactory().setValue(slider.getValue());
});
spinner.valueProperty().addListener((Observable o) -> {
slider.setValue((double) spinner.getValue());
});
GridPane root = new GridPane();
root.addRow(0, slider, spinner);
root.setPadding(new Insets(8, 8, 8, 8));
root.setHgap(8);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
最佳答案
首先将INITIAL_VALUE
用作微调框的构造函数的initialValue
参数。在内部,微调器的具体 SpinnerValueFactory
是DoubleSpinnerValueFactory
,在其ChangeListener
属性中添加了value
;当再次设置初始值时,该值实际上并没有改变。有两种方法可以证明自己:
Spinner spinner = new Spinner(MIN, MAX, -42, STEP_INCREMENT);
spinner.getValueFactory().setConverter(…);
spinner.getValueFactory().setValue(INITIAL_VALUE);
SpinnerValueFactory
,并使用它来构造微调器:SpinnerValueFactory factory = new SpinnerValueFactory
.DoubleSpinnerValueFactory(MIN, MAX, INITIAL_VALUE, STEP_INCREMENT);
factory.setConverter(…);
Spinner spinner = new Spinner(factory);
此外,下面的示例用双向绑定(bind)替换了两个监听器,该双向绑定(bind)使用弱监听器来允许垃圾回收属性:
slider.valueProperty().bindBidirectional(
spinner.getValueFactory().valueProperty());
平凡的是,微调器和滑块可以相互控制。更常见的是,每个人都可以在控制另一个模型拥有的属性(property)的过程中保持同步:
model.xProperty().bindBidirectional(slider.valueProperty());
model.xProperty().bindBidirectional(spinner.getValueFactory().valueProperty());
import java.text.NumberFormat;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.converter.PercentageStringConverter;
/**
* @see https://stackoverflow.com/a/55427307/230513
* @see https://stackoverflow.com/a/6067986/230513
*/
public class SpinSlider extends Application {
private static final double MIN = 0;
private static final double MAX = 1;
private static final double INITIAL_VALUE = 0.5;
private static final double STEP_INCREMENT = 0.1;
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SpinSlider");
Slider slider = new Slider(MIN, MAX, INITIAL_VALUE);
slider.setBlockIncrement(STEP_INCREMENT);
SpinnerValueFactory factory = new SpinnerValueFactory
.DoubleSpinnerValueFactory(MIN, MAX, INITIAL_VALUE, STEP_INCREMENT);
factory.setConverter(new PercentageStringConverter(
NumberFormat.getPercentInstance()));
Spinner spinner = new Spinner(factory);
slider.valueProperty().bindBidirectional(spinner.getValueFactory().valueProperty());
GridPane root = new GridPane();
root.addRow(0, slider, spinner);
root.setPadding(new Insets(8, 8, 8, 8));
root.setHgap(8);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}