Skip to content

Instantly share code, notes, and snippets.

@willparsons
Last active November 30, 2021 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willparsons/8ce205edf528076e265c22f7e1dd6209 to your computer and use it in GitHub Desktop.
Save willparsons/8ce205edf528076e265c22f7e1dd6209 to your computer and use it in GitHub Desktop.
MVC JavaFX example
// 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);
}
}
@aidandagnall
Copy link

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment