Last active
November 30, 2021 21:34
-
-
Save willparsons/8ce205edf528076e265c22f7e1dd6209 to your computer and use it in GitHub Desktop.
MVC JavaFX example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Model | |
public class Model { | |
private final StringProperty name; | |
public Model() { | |
name = new SimpleStringProperty(); | |
} | |
public StringProperty getNameProperty() { | |
return name; | |
} | |
} | |
// Controller | |
public class Controller { | |
private final View view; | |
private final Model model; | |
public StartController() { | |
view = new View(); | |
model = new Model(); | |
view.getName().textProperty().bind(model.getNameProperty()); | |
} | |
public Scene getScene() { | |
return view.getScene(); | |
} | |
} | |
public class View { | |
private final Label name; | |
private Scene scene; | |
public View() { | |
label = new Label(); | |
setup(); | |
} | |
private void setup() { | |
Pane root = new StackPane(); | |
root.getChildren().add(name); | |
scene = new Scene(root, 600, 400); | |
} | |
public Label getName() { | |
return name; | |
} | |
public Scene getScene() { | |
return scene; | |
} | |
} | |
public class Main extends Application { | |
@Override | |
public void start(Stage stage) { | |
Controller c = new Controller(); | |
stage.setScene(c.getScene()); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LGTM!