Skip to content

Instantly share code, notes, and snippets.

@falkoschumann
Last active July 20, 2023 19:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save falkoschumann/0699048876dd897dac74b7821d50f271 to your computer and use it in GitHub Desktop.
Save falkoschumann/0699048876dd897dac74b7821d50f271 to your computer and use it in GitHub Desktop.
Using JavaFX view and view controller with factory method.
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
public class FXMLControllerFactory {
private FXMLControllerFactory() {}
public static <T> T newController(Class<T> controllerType, Stage stage) {
var filename = "/%s.fxml".formatted(controllerType.getSimpleName());
try {
var url = controllerType.getResource(filename);
var loader = new FXMLLoader(url);
loader.setRoot(stage);
loader.load();
return loader.getController();
} catch (Exception e) {
throw new IllegalStateException("Could not load view from file: %s".formatted(filename), e);
}
}
}
/* Inspired by UWP Type Ramp
*
* | Title | Weight | Size | Line Height | Ratio |
* |-----------|------------------|------|-------------|-------|
* | Header | light (300) | 46px | 56px | 3.286 |
* | Subheader | light (300) | 34px | 40px | 2.429 |
* | Title | semi-light (350) | 24px | 28px | 1.714 |
* | Subtitle | regular (400) | 20px | 24px | 1.429 |
* | Base | semi-bold (600) | 14px | 20px | 1.000 |
* | Body | regular (400) | 14px | 20px | 1.000 |
* | Caption | regular (400) | 12px | 14px | 0.857 |
*/
.root {
-fx-font-family: "Verdana";
}
.header {
-fx-font-size: 3.0em;
-fx-font-weight: normal;
}
.subheader {
-fx-font-size: 2.25em;
-fx-font-weight: normal;
}
.title {
-fx-font-size: 1.5em;
-fx-font-weight: normal;
}
.subtitle {
-fx-font-size: 1.25em;
-fx-font-weight: normal;
}
.body {
-fx-font-size: 1.0em;
-fx-font-weight: normal;
}
.base {
-fx-font-size: 1.0em;
-fx-font-weight: bold;
}
.caption {
-fx-font-size: 0.875em;
-fx-font-weight: normal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment