私は、コントローラ内Spinner
を持っている:Spinnerが最初に初期化されたときにバインドされたプロパティを更新しないのはなぜですか?
@FXML
private Spinner<Integer> spnMySpinner;
とコントローラでSimpleIntegerProperty
:
private static final SimpleIntegerProperty myValue =
new SimpleIntegerProperty(3); //load a default value
私は、コントローラのinitialize
方法でそれらを一緒にバインドされています:
spnMySpinner.getValueFactory().valueProperty().bindBidirectional(myValueProperty().asObject());
しかしバインディングの仕事コントローラが2回目の初期化後にのみ正しくここで私はそれを再現することができます方法は次のとおりです。
- 私は関連するコントローラと、ステージを開く、それが正しく
myValue
プロパティ(数3)で指定されたデフォルト値をロードします。 - 私はそれ4.作るためにスピナーの増分ボタンをクリックしますこれは、スピナーのvalueプロパティの値を変更しますが、バウンドプロパティ
myValue
は、私は、ステージ/ウィンドウを閉じる番号3. - にそのまま残されています。
- 私は再び開き、スピナーの値は3です。
- もう一度インクリメントします。 Boomは現在バインディングが動作しており、スピンナーとバインドされたプロパティの両方に「4」があります。
最小限の全体が、発射可能/再現可能なコード:
Main.java:
package spinnerpoc;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MainWindow.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="myRoot" id="AnchorPane" prefHeight="231.0" prefWidth="337.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="spinnerpoc.MainWindowController">
<children>
<Button fx:id="btnOpenSpinnerWindow" layoutX="102.0" layoutY="103.0" mnemonicParsing="false" text="Open SpinnerWindow" onAction="#onOpenSpinnerWindow"/>
</children>
</AnchorPane>
MainWindowController.java:
package spinnerpoc;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class MainWindowController implements Initializable {
@FXML
private Button btnOpenSpinnerWindow;
@FXML
private AnchorPane myRoot;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void onOpenSpinnerWindow(ActionEvent event) throws IOException{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SpinnerWindow.fxml"));
Parent root = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.initOwner(myRoot.getScene().getWindow());
stage.initModality(Modality.WINDOW_MODAL);
stage.setTitle("SpinnerWindow");
stage.setScene(new Scene(root));
stage.show();
}
}
SpinnerWindow.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.control.SpinnerValueFactory.DoubleSpinnerValueFactory?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<ScrollPane xmlns:fx="http://javafx.com/fxml/1" fitToWidth="true" prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8.0.60" fx:controller="spinnerpoc.SpinnerWindowController">
<content>
<VBox maxWidth="1.7976931348623157E308">
<children>
<Spinner fx:id="spnMySpinner" editable="true" prefWidth="50.0" max="10" min="1" />
</children>
</VBox>
</content>
</ScrollPane>
SpinnerWindowController.java:私は何を
package spinnerpoc;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Spinner;
public class SpinnerWindowController implements Initializable {
private static final SimpleIntegerProperty myValue = new SimpleIntegerProperty(3);
public static SimpleIntegerProperty myValueProperty() {
return myValue;
}
public static Integer getMyValue() {
return myValue.getValue();
}
public static void setMyValue(int value) {
myValue.set(value);
}
@FXML
private Spinner<Integer> spnMySpinner;
@Override
public void initialize(URL url, ResourceBundle rb) {
spnMySpinner.getValueFactory().valueProperty().bindBidirectional(myValueProperty().asObject());
}
}
(。BitBucket repoのコードも利用可能)
をしないのですか?
それはあなたが与えられている情報から知ることは不可能です。問題を再現する[MCVE]を作成し、質問を組み込むために質問を編集します。 –
@ James_D私は簡単な例を書いたが、それは100行以上あるので、私はそれをbitbucketにプッシュした。 – Leprechaun
質問にはあまりにも多くのコードが含まれていません。例をあなたの質問に移しました。 –