MCPcopy Create free account
hub / github.com/PCGen/pcgen / PanelFromResource

Class PanelFromResource

code/src/java/pcgen/gui3/PanelFromResource.java:23–89  ·  view source on GitHub ↗

A utility class for loading and displaying JavaFX scenes from FXML resources. This class provides functionality to load FXML files, assign their controllers, and display the loaded scenes in a new JavaFX stage. @param The type of the controller associated with the FXML resource.

Source from the content-addressed store, hash-verified

21 * @param <T> The type of the controller associated with the FXML resource.
22 */
23public class PanelFromResource<T> implements Controllable<T>
24{
25 private static final Logger LOG = Logger.getLogger(PanelFromResource.class.getName());
26
27 private final FXMLLoader fxmlLoader = new FXMLLoader();
28
29 /**
30 * Constructs a new PanelFromResource instance.
31 *
32 * @param klass The class relative to which the FXML resource is located.
33 * @param resourceName The name of the FXML resource file to load.
34 * @throws NullPointerException if the resource cannot be found.
35 */
36 public PanelFromResource(Class<? extends T> klass, String resourceName)
37 {
38 URL resource = klass.getResource(resourceName);
39 Objects.requireNonNull(resource,
40 () -> MessageFormat.format("Resource {0} not found relative to class {1}", resourceName, klass));
41 LOG.log(Level.FINE, () -> MessageFormat.format(
42 "Loading a scene for resource name {0} (a class {1}). The final location is {2}", resourceName, klass,
43 resource));
44 fxmlLoader.setLocation(resource);
45 fxmlLoader.setResources(LanguageBundle.getBundle());
46 }
47
48 /**
49 * Retrieves the controller associated with the loaded FXML resource.
50 *
51 * @return The controller instance.
52 * @throws IllegalStateException if this method is called outside the JavaFX application thread.
53 */
54 @Override
55 public T getController()
56 {
57 GuiAssertions.assertIsJavaFXThread();
58 return fxmlLoader.getController();
59 }
60
61 /**
62 * Displays the loaded FXML resource as a new JavaFX stage.
63 *
64 * @param title The title of the stage to be displayed.
65 * @throws IllegalStateException if this method is called outside the JavaFX application thread.
66 */
67 public void showAsStage(String title)
68 {
69 GuiAssertions.assertIsJavaFXThread();
70
71 try
72 {
73 // Load the scene from the FXML resource.
74 Scene scene = fxmlLoader.load();
75
76 // Create and configure a new stage.
77 Stage stage = new Stage();
78 stage.setTitle(title);
79 stage.setScene(scene);
80 stage.sizeToScene();

Callers

nothing calls this directly

Calls 2

getLoggerMethod · 0.80
getNameMethod · 0.65

Tested by

no test coverage detected