Example usage for javafx.fxml FXMLLoader getController

List of usage examples for javafx.fxml FXMLLoader getController

Introduction

In this page you can find the example usage for javafx.fxml FXMLLoader getController.

Prototype

@SuppressWarnings("unchecked")
public <T> T getController() 

Source Link

Document

Returns the controller associated with the root object.

Usage

From source file:org.cryptomator.ui.MainApplication.java

@Override
public void start(final Stage primaryStage) throws IOException {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    Platform.runLater(() -> {//  ww w.jav  a2 s. c  o m
        /*
         * This fixes a bug on OSX where the magic file open handler leads
         * to no context class loader being set in the AppKit (event) thread
         * if the application is not started opening a file.
         */
        if (Thread.currentThread().getContextClassLoader() == null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    });

    Runtime.getRuntime().addShutdownHook(MainApplication.CLEAN_SHUTDOWN_PERFORMER);

    executorService = Executors.newCachedThreadPool();
    addShutdownTask(() -> {
        executorService.shutdown();
    });

    WebDavServer.getInstance().start();
    chooseNativeStylesheet();
    final ResourceBundle rb = ResourceBundle.getBundle("localization");
    final FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/main.fxml"), rb);
    final Parent root = loader.load();
    final MainController ctrl = loader.getController();
    ctrl.setStage(primaryStage);
    final Scene scene = new Scene(root);
    primaryStage.setTitle(rb.getString("app.name"));
    primaryStage.setScene(scene);
    primaryStage.sizeToScene();
    primaryStage.setResizable(false);
    primaryStage.show();
    ActiveWindowStyleSupport.startObservingFocus(primaryStage);
    TrayIconUtil.init(primaryStage, rb, () -> {
        quit();
    });

    for (String arg : getParameters().getUnnamed()) {
        handleCommandLineArg(ctrl, arg);
    }

    if (org.controlsfx.tools.Platform.getCurrent().equals(org.controlsfx.tools.Platform.OSX)) {
        Main.OPEN_FILE_HANDLER.complete(file -> handleCommandLineArg(ctrl, file.getAbsolutePath()));
    }

    LocalInstance cryptomatorGuiInstance = SingleInstanceManager.startLocalInstance(APPLICATION_KEY,
            executorService);
    addShutdownTask(() -> {
        cryptomatorGuiInstance.close();
    });

    cryptomatorGuiInstance.registerListener(arg -> handleCommandLineArg(ctrl, arg));
}

From source file:application.Main.java

@Override
public void start(Stage primaryStage) throws InterruptedException {

    System.out.println(applicationTitle + " initialized!");

    // define working variables
    String text = "";
    Boolean terminateExecution = Boolean.FALSE;

    // does the specified FXML XML file path exist?
    String fxmlXmlFileFullPath = fxmlPath.equals("") ? fxmlXmlFileName : fxmlPath + "/" + fxmlXmlFileName;
    URL urlXml = Main.class.getResource(fxmlXmlFileFullPath);
    if (urlXml == null) {
        terminateExecution = Boolean.TRUE;
        text = Main.class.getName() + ": FXML XML File '" + fxmlXmlFileFullPath
                + "' not found in resource path!";
        GenericUtilities.outputToSystemErr(text, systemErrorsDesired);
    }//from ww w.  j  av a 2 s . c  o  m

    // does the specified FXML CSS file path exist?
    String fxmlCssFileFullPath = fxmlPath.equals("") ? fxmlCssFileName : fxmlPath + "/" + fxmlCssFileName;
    URL urlCss = Main.class.getResource(fxmlCssFileFullPath);
    if (urlCss == null) {
        terminateExecution = Boolean.TRUE;
        text = Main.class.getName() + ": FXML CSS File '" + fxmlCssFileFullPath
                + "' not found in resource path!";
        GenericUtilities.outputToSystemErr(text, systemErrorsDesired);
    }

    if (terminateExecution) {
        text = Main.class.getName() + ": Execution terminated due to errors!";
        GenericUtilities.outputToSystemErr(text, systemErrorsDesired);
        GenericUtilities.outputToSystemOut(text, systemOutputDesired);
        return;
    }

    // initialize and display the primary stage
    try {
        // load the FXML file and instantiate the "root" object
        FXMLLoader fxmlLoader = new FXMLLoader(urlXml);
        Parent root = (Parent) fxmlLoader.load();

        controller = (FracKhemGUIController) fxmlLoader.getController();
        controller.setStage(primaryStage);
        controller.setInitStageTitle(applicationTitle);

        // initialize and display the stage
        createTrayIcon(primaryStage);
        firstTime = Boolean.TRUE;
        Platform.setImplicitExit(Boolean.FALSE);

        Scene scene = new Scene(root);
        scene.getStylesheets().add(urlCss.toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle(applicationTitle + " - " + controller.getPropFileCurrFileVal());
        primaryStage.show();
    } catch (Exception e) {
        text = ExceptionUtils.getStackTrace(e);
        GenericUtilities.outputToSystemErr(text, systemErrorsDesired);
        text = Main.class.getName() + ": Execution terminated due to errors!";
        GenericUtilities.outputToSystemErr(text, systemErrorsDesired);
        GenericUtilities.outputToSystemOut(text, systemOutputDesired);
    }

}

From source file:de.mirkosertic.desktopsearch.DesktopSearch.java

@Override
public void start(Stage aStage) throws Exception {

    // This is our base directory
    File theBaseDirectory = new File(SystemUtils.getUserHome(), "FreeSearchIndexDir");
    theBaseDirectory.mkdirs();/*  w  ww .j a  va 2s. co  m*/

    configurationManager = new ConfigurationManager(theBaseDirectory);

    Notifier theNotifier = new Notifier();

    stage = aStage;

    // Create the known preview processors
    PreviewProcessor thePreviewProcessor = new PreviewProcessor();

    try {
        // Boot the search backend and set it up for listening to configuration changes
        backend = new Backend(theNotifier, configurationManager.getConfiguration(), thePreviewProcessor);
        configurationManager.addChangeListener(backend);

        // Boot embedded JSP container
        embeddedWebServer = new FrontendEmbeddedWebServer(aStage, backend, thePreviewProcessor,
                configurationManager);

        embeddedWebServer.start();
    } catch (BindException | LockReleaseFailedException | LockObtainFailedException e) {
        // In this case, there is already an instance of DesktopSearch running
        // Inform the instance to bring it to front end terminate the current process.
        URL theURL = new URL(FrontendEmbeddedWebServer.getBringToFrontUrl());
        // Retrieve the content, but it can be safely ignored
        // There must only be the get request
        Object theContent = theURL.getContent();

        // Terminate the JVM. The window of the running instance is visible now.
        System.exit(0);
    }

    aStage.setTitle("Free Desktop Search");
    aStage.setWidth(800);
    aStage.setHeight(600);
    aStage.initStyle(StageStyle.TRANSPARENT);

    FXMLLoader theLoader = new FXMLLoader(getClass().getResource("/scenes/mainscreen.fxml"));
    AnchorPane theMainScene = theLoader.load();

    final DesktopSearchController theController = theLoader.getController();
    theController.configure(this, backend, FrontendEmbeddedWebServer.getSearchUrl(), stage.getOwner());

    Undecorator theUndecorator = new Undecorator(stage, theMainScene);
    theUndecorator.getStylesheets().add("/skin/undecorator.css");

    Scene theScene = new Scene(theUndecorator);

    // Hacky, but works...
    theUndecorator.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");

    theScene.setFill(Color.TRANSPARENT);
    aStage.setScene(theScene);

    aStage.getIcons().add(new Image(getClass().getResourceAsStream("/fds.png")));

    if (SystemTray.isSupported()) {
        Platform.setImplicitExit(false);
        SystemTray theTray = SystemTray.getSystemTray();

        // We need to reformat the icon according to the current tray icon dimensions
        // this depends on the underlying OS
        java.awt.Image theTrayIconImage = Toolkit.getDefaultToolkit()
                .getImage(getClass().getResource("/fds_small.png"));
        int trayIconWidth = new TrayIcon(theTrayIconImage).getSize().width;
        TrayIcon theTrayIcon = new TrayIcon(
                theTrayIconImage.getScaledInstance(trayIconWidth, -1, java.awt.Image.SCALE_SMOOTH),
                "Free Desktop Search");
        theTrayIcon.setImageAutoSize(true);
        theTrayIcon.setToolTip("FXDesktopSearch");
        theTrayIcon.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    Platform.runLater(() -> {
                        if (stage.isIconified()) {
                            stage.setIconified(false);
                        }
                        stage.show();
                        stage.toFront();
                    });
                }
            }
        });
        theTray.add(theTrayIcon);

        aStage.setOnCloseRequest(aEvent -> stage.hide());
    } else {

        aStage.setOnCloseRequest(aEvent -> shutdown());
    }

    aStage.setMaximized(true);
    aStage.show();
}

From source file:gov.va.isaac.gui.listview.operations.FindAndReplace.java

@Override
public void init(ObservableList<SimpleDisplayConcept> conceptList) {
    super.init(conceptList);
    try {/*  w w w .  j a va2 s  .c  o m*/
        URL resource = FindAndReplace.class.getResource("FindAndReplaceController.fxml");
        FXMLLoader loader = new FXMLLoader(resource);
        loader.load();
        frc_ = loader.getController();
        super.root_ = frc_.getRoot();
    } catch (IOException e) {
        logger_.error("Unexpected error building panel", e);
        throw new RuntimeException("Error building panel");
    }
}

From source file:com.coolchick.translatortemplater.Main.java

/**
 * Shows the person overview inside the root layout.
 *//*from w w w  . j a  v a  2s.  c om*/
public void showTranslatorOverview() {
    try {
        // Load person overview.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("PersonOverview.fxml"));
        StackPane personOverview = loader.load();
        PersonOverviewController controller = loader.getController();
        controller.setMain(this);
        controller.setTranslators(getTranslators());
        primaryStage.setScene(new Scene(personOverview, 800, 600));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:context.ui.control.codebooknetwork.CodebookNetworkController.java

/**
 *
 *//*from ww w.  ja va 2  s  . com*/
public CodebookNetworkController() {
    super();
    try {
        setTaskname(NamingPolicy.generateTaskName(CodebookNetworkTaskInstance.class));
        setTaskInstance(new CodebookApplicationTaskInstance(getTaskname()));

        FXMLLoader loader2 = new FXMLLoader(
                getClass().getResource(CodebookNetworkConfigurationController.path));
        Parent s2Content = (Parent) loader2.load();
        configurationController = (CodebookNetworkConfigurationController) loader2.getController();

        setStep2Content(s2Content);

        super.postInitialize();
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:analyzer.code.FXMLAnalyzerController.java

@FXML
private void settingMunuItem() throws FileNotFoundException, IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLSetting.fxml"));
    try {//from  www  . j  a va2  s. com
        AnchorPane pane = (AnchorPane) loader.load();
        fxmlsc = loader.getController();
        fxmlsc.setAnalyzer(this);
        Scene scene = new Scene(pane);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle("??");
        stage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:analyzer.code.FXMLAnalyzerController.java

@FXML
private void metricsCplusPlusMunuItem() throws FileNotFoundException, IOException {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("FXMLMetrics.fxml"));
    try {//w  ww.j  a  v  a  2  s  .  c  om
        AnchorPane pane = (AnchorPane) loader.load();
        fxmlmc = loader.getController();
        fxmlmc.setAnalyzer((AnalyzerC) analyzer);
        Scene scene = new Scene(pane);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle(" ++");
        stage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.github.drbookings.DrBookingsApplication.java

private void startGUI(final Stage stage) throws IOException {
    final FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/MainView.fxml"));
    final Parent root = loader.load();
    final Scene scene = new Scene(root, 900, 800);
    mainController = loader.getController();
    String s = getClass().getPackage().getImplementationVersion();
    if (s == null) {
        s = "dev version";
    }//  w ww . j  a v  a  2 s . com
    stage.setTitle("Dr.Bookings " + s);
    stage.setScene(scene);
    stage.setOnCloseRequest(new CloseRequestEventHandler());
    stage.show();
    mainController.readDataFile(SettingsManager.getInstance().getDataFile());

}

From source file:net.rptools.gui.GuiBuilder.java

/**
 * Construct the scene in this contructor and later publish on the
 * application thread./*from   w  w w  .  jav a  2  s.  c  o m*/
 * @param parent component back-reference
 */
public GuiBuilder(final GuiComponentImpl parent) {
    component = parent;
    // Basic initialization
    rootStage = new Stage();
    final FXMLLoader fxmlLoader = new GuiFXMLLoader("main.fxml");
    try {
        rootRegion = (Region) fxmlLoader.load();
    } catch (final IOException e) {
        LOGGER.error("GUI can't be constructed", e);
    }
    fxmlListener = fxmlLoader.getController();
}