Skip to content

Instantly share code, notes, and snippets.

@manuel-mauky
Created April 26, 2016 16:43
Show Gist options
  • Save manuel-mauky/9437412aeaee2a6480a993c6eb1266eb to your computer and use it in GitHub Desktop.
Save manuel-mauky/9437412aeaee2a6480a993c6eb1266eb to your computer and use it in GitHub Desktop.
JavaFX Controller is removed by garbage collector while the fxml file is still shown
package org.example;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("/Test.fxml"));
Parent load = fxmlLoader.load();
primaryStage.setScene(new Scene(load));
primaryStage.show();
}
}
package org.example;
import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
public class MyView {
@FXML
public ToggleButton togglebutton;
@FXML
public Label label;
@FXML
public Button gcButton;
private MyViewModel viewModel = new MyViewModel();
public void initialize() {
label.textProperty().bind(Bindings.concat("Selected: ", togglebutton.selectedProperty()));
togglebutton.selectedProperty().addListener((observable, oldValue, newValue) -> System.out.println("togglebutton"));
viewModel.selectedProperty().bindBidirectional(togglebutton.selectedProperty());
gcButton.setOnAction(e -> {
System.gc();
});
}
}
package org.example;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
public class MyViewModel {
private BooleanProperty selected = new SimpleBooleanProperty();
public MyViewModel() {
selected.addListener((observable, oldValue, newValue) -> System.out.println("viewModel"));
}
public BooleanProperty selectedProperty() {
return selected;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.VBox?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" spacing="10.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.example.MyView">
<children>
<Label fx:id="label" text="Label" />
<ToggleButton fx:id="togglebutton" mnemonicParsing="false" text="ToggleButton" />
<Separator prefWidth="200.0" />
<Button mnemonicParsing="false" fx:id="gcButton" text="Run GC" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</VBox>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment