JavaFX 8 Spinner 控件不更新显示的值

JavaFX 8 Spinner control doesn't update value that's displayed

我是 Java 编程新手,并选择在 JavaFX8 中开发我的工具 UI。我需要一个微调器(JavaFX8u40 中的新功能!),用户可以为其指定一个从 0 到 120 秒的整数值(简单的部分),还有一个允许用户直接设置微调器值的按钮到给定的预设按钮值(此处为 0)。

问题是,当我单击按钮对其进行测试时,微调器值似乎没有更新并保持在 60(微调器初始值)或用户可能已通过微调器更改的任何其他值箭头控件。

我从搜索解决方案中了解到,这是因为我应该操纵出厂值而不是微调器值本身,但我得到了相同的结果。

这是我为反映该问题而创建的 FXML 文件的代码:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>


<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="TestViewController">
   <children>
      <BorderPane layoutX="28.0" layoutY="60.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <center>
            <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
               <children>
                  <Label mnemonicParsing="true" text="Clear Automatically _History After">
                     <opaqueInsets>
                        <Insets />
                     </opaqueInsets>
                     <padding>
                        <Insets right="5.0" />
                     </padding>
                  </Label>
                  <Spinner fx:id="spinnerClearHistoryDuration" initialValue="60" max="120" min="0" prefHeight="25.0" prefWidth="60.0" />
                  <Button fx:id="btnClearHistoryDuration" onAction="#clickBtnClearHistoryDuration" text="_0" />
               </children>
            </HBox>
         </center>
      </BorderPane>
   </children>
</AnchorPane>

这是匹配的控制器Java class:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Spinner;

public class TestViewController implements Initializable {

    @FXML
    private Spinner<Integer> spinnerClearHistoryDuration;

    @FXML
    private Button btnClearHistoryDuration;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        spinnerClearHistoryDuration = new Spinner(0, 120, 30);
    }

    @FXML
    void clickBtnClearHistoryDuration(ActionEvent event) {
        //Tried the following, which is more extended but gets to same result 
        //(supposedly because this is just using more detailed resources but the same approach)

        /*
        SpinnerValueFactory<Integer> valueFactory = spinnerClearHistoryDuration.getValueFactory();
        valueFactory.setValue(0);
        spinnerClearHistoryDuration.setValueFactory(valueFactory);
        */
        
        spinnerClearHistoryDuration.getValueFactory().setValue(0);
    }
    
}

当然,我的主要方法就这么简单:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class JavaFXTestApplication extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("TestView.fxml"));
        
        Scene scene = new Scene(root);
        
        stage.setScene(scene);
        stage.show();
    }

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

由于我还是这个平台的新手,我无法post截屏来进一步说明,对此深表歉意。

任何关于我忘记或遗漏了什么的想法都将不胜感激,因为 FXML8 是相当新的并且没有找到与我的问题有点相关的解决方案。

所有最好的人, 蒂埃里

问题是您正在实例化两次微调器。

有了这个:

@FXML
private Spinner<Integer> spinnerClearHistoryDuration;

微调器在加载 FXML 文件时已经实例化。

现在,在那之后,您创建了一个新实例:

@Override
public void initialize(URL location, ResourceBundle resources) {
    spinnerClearHistoryDuration = new Spinner(0, 120, 30);
}

解决方案:只删除第二个实例:

@Override
public void initialize(URL location, ResourceBundle resources) {        
}