Skip to content

Instantly share code, notes, and snippets.

@NotArchon
Created August 20, 2022 19:43
Show Gist options
  • Save NotArchon/6af7a090dc336c00b0024d5392137598 to your computer and use it in GitHub Desktop.
Save NotArchon/6af7a090dc336c00b0024d5392137598 to your computer and use it in GitHub Desktop.
package com.discrypto.client;
import com.discrypto.client.screens.Screen;
import com.discrypto.client.screens.main.MainScreen;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
public class Client extends Application {
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage stage) throws Exception {
MainScreen main = new MainScreen();
main.stage = stage;
openScreen(main);
}
public static Scene loadScene(String fxmlName) {
return loadScene(fxmlName, null);
}
public static Scene loadScene(String fxmlName, Object controller) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getResource(fxmlName + ".fxml"));
if(controller != null)
fxmlLoader.setController(controller);
return new Scene(fxmlLoader.load());
} catch(IOException e) {
throw new RuntimeException(e);
}
}
public static void openScreen(Screen<?> screen) {
if(screen.stage == null)
screen.stage = new Stage();
screen.stage.setTitle(screen.title);
screen.stage.setScene(loadScene(screen.sceneName, screen.controller));
screen.beforeShow();
screen.stage.show();
screen.afterShow();
}
public static URL getResource(String name) {
return Client.class.getResource(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment