Neues Fenster mit FXML File auf Knopfdruck in JAVA FX 8.0
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javafxexamples; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Modality; import javafx.stage.Stage; import javafx.stage.StageStyle; /** * * @author jduerr */ public class WindowHelper { public WindowHelper() { } /** * * Create and show a new window with given name of FXML File. * * @param fxmlString The name of the FXML File i.e. "view.fxml". * @param stageTitle The title for the window. * @param s The StageStyle that the Window will use. * @param m The modality of the new windows. */ public void createWindowWithFXML(String fxmlString,String stageTitle, StageStyle s, Modality m){ FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fxmlString)); Parent root; try { root = (Parent) fxmlLoader.load(); Stage stage = new Stage(); stage.initModality(m); stage.initStyle(s); stage.setTitle(stageTitle); stage.setScene(new Scene(root)); stage.show(); } catch (IOException ex) { Logger.getLogger(MainViewController.class.getName()).log(Level.SEVERE, null, ex); } } }
Der Aufruf ist denkbar einfach. Er könnte z.B. wie folgt aussehen:
WindowHelper wh = new WindowHelper(); wh.createWindowWithFXML("About.fxml","About", StageStyle.DECORATED, Modality.WINDOW_MODAL);












