【问题标题】:JavaFX 8 Spinner control doesn't update value that's displayedJavaFX 8 Spinner 控件不更新显示的值
【发布时间】:2015-04-02 20:22:42
【问题描述】:

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

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);
    }
    
}

由于我还是这个平台的新手,我不被允许发布屏幕截图来进一步说明,对此感到抱歉。

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

所有最好的人, 蒂埃里

【问题讨论】:

    标签: java refresh spinner javafx-8 fxml


    【解决方案1】:

    问题是你实例化了两次微调器。

    有了这个:

    @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) {        
    }
    

    【讨论】:

    • 感谢 José 的帮助。不幸的是,它没有用:(
    • 它对我有用,清理并构建项目并再次运行它。
    • 哇,这确实很奇怪。这次奏效了。非常感谢你,何塞!
    猜你喜欢
    • 2018-06-18
    • 2019-10-19
    • 1970-01-01
    • 2015-11-27
    • 2021-01-21
    • 2019-05-27
    • 2016-04-18
    • 2016-12-13
    • 1970-01-01
    相关资源
    最近更新 更多