JavaFX 8 Spinner控件不会更新显示的值 [英] JavaFX 8 Spinner control doesn't update value that's displayed

查看:176
本文介绍了JavaFX 8 Spinner控件不会更新显示的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉Java编程,因此选择在JavaFX8中开发我的工具UI.我需要一个Spinner(JavaFX8u40中的新功能!),用户可以为它指定一个介于0到120秒之间的整数值(最简单的部分),还有一个按钮,它允许用户将Spinner值直接设置为给定的pre-设置按钮值(此处为0).

I am new to Java programming and opted for developing my tool UI in JavaFX8. I need to have a Spinner (new in JavaFX8u40!) for which the user can specify a integer value from 0 to 120 seconds (the easy part) but also a button which allows the user to set the spinner value directly to the given pre-set button value (here, 0).

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

Trouble is that when I click the button to test it, the spinner value doesn't seem to update and remains at 60 (spinner initial value) or any other value that may have been changed by the user through the spinner arrow controls.

通过对解决方案的搜索,我了解到这是因为我应该这样操作工厂值而不是微调器值,但是我会得到相同的结果.

I have understood from my searches for solution that this is because I should manipulate the factory value and not the spinner value as such, but I get to the same result.

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

Here is the code of my FXML file that I created to reflect the issue:

<?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类:

And here is the matching controller 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);
    }
    
}

当然,我的主要方法很简单,如下所示:

Of course, my main method is as simple as the following:

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

由于我仍然是这个平台的新手,因此我无法发布屏幕截图以进一步说明问题,

As I am still new to this platform, I was not allowed to post a screen capture to illustrate further, sorry about that.

任何关于我遗忘或错过的事情的想法都会受到赞赏,因为FXML8是一个相当新的东西,并且没有找到与我的问题相关的解决方案.

Any idea on what I am forgetting or missing out would be greatly appreciated as FXML8 is quite new and didn't find a somewhat-related solution to my issue.

最好的家伙, 蒂埃里

推荐答案

问题是您实例化微调器两次.

The problem is you are instantiating twice the spinner.

与此:

@FXML
private Spinner<Integer> spinnerClearHistoryDuration;

在加载FXML文件时,实例化微调器.

the spinner is already instantiated when the FXML file is loaded.

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

And now, after that, you create a new instance:

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

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

Solution: just remove the second instance:

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

这篇关于JavaFX 8 Spinner控件不会更新显示的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆