Skip to content

Instantly share code, notes, and snippets.

@mosababdo
Forked from jewelsea/Main.java
Created August 26, 2018 17:02
Show Gist options
  • Select an option

  • Save mosababdo/7cd1fe94edc02c5301b5e1744c9b5017 to your computer and use it in GitHub Desktop.

Select an option

Save mosababdo/7cd1fe94edc02c5301b5e1744c9b5017 to your computer and use it in GitHub Desktop.

Revisions

  1. @jewelsea jewelsea revised this gist Sep 6, 2013. 2 changed files with 3 additions and 3 deletions.
    4 changes: 2 additions & 2 deletions vista.css
    Original file line number Diff line number Diff line change
    @@ -15,11 +15,11 @@
    }

    #vista1 {
    -fx-background-color: coral;
    -fx-background-color: aliceblue;
    }

    #vista2 {
    -fx-background-color: aliceblue;
    -fx-background-color: coral;
    }

    .button {
    2 changes: 1 addition & 1 deletion vista2.fxml
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.StackPane?>
    <StackPane fx:id="vista1" xmlns:fx="http://javafx.com/fxml" fx:controller="Vista2Controller">
    <StackPane fx:id="vista2" xmlns:fx="http://javafx.com/fxml" fx:controller="Vista2Controller">
    <children>
    <Button mnemonicParsing="false" onAction="#previousPane" text="Previous Pane" />
    </children>
  2. @jewelsea jewelsea revised this gist Sep 6, 2013. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions Vista1Controller.java
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,9 @@
    public class Vista1Controller {

    /**
    * Event handler fires when the user requests a new vista.
    * Event handler fired when the user requests a new vista.
    *
    * @param event the event the triggered the handler.
    * @param event the event that triggered the handler.
    */
    @FXML
    void nextPane(ActionEvent event) {
    4 changes: 2 additions & 2 deletions Vista2Controller.java
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,9 @@
    public class Vista2Controller {

    /**
    * Event handler fires when the user requests a previous vista.
    * Event handler fired when the user requests a previous vista.
    *
    * @param event the event the triggered the handler.
    * @param event the event that triggered the handler.
    */
    @FXML
    void previousPane(ActionEvent event) {
  3. @jewelsea jewelsea created this gist Sep 6, 2013.
    74 changes: 74 additions & 0 deletions Main.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Scene;
    import javafx.scene.layout.Pane;
    import javafx.stage.Stage;

    import java.io.IOException;

    /**
    * Main application class.
    */
    public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception{
    stage.setTitle("Vista Viewer");

    stage.setScene(
    createScene(
    loadMainPane()
    )
    );

    stage.show();
    }

    /**
    * Loads the main fxml layout.
    * Sets up the vista switching VistaNavigator.
    * Loads the first vista into the fxml layout.
    *
    * @return the loaded pane.
    * @throws IOException if the pane could not be loaded.
    */
    private Pane loadMainPane() throws IOException {
    FXMLLoader loader = new FXMLLoader();

    Pane mainPane = (Pane) loader.load(
    getClass().getResourceAsStream(
    VistaNavigator.MAIN
    )
    );

    MainController mainController = loader.getController();

    VistaNavigator.setMainController(mainController);
    VistaNavigator.loadVista(VistaNavigator.VISTA_1);

    return mainPane;
    }

    /**
    * Creates the main application scene.
    *
    * @param mainPane the main application layout.
    *
    * @return the created scene.
    */
    private Scene createScene(Pane mainPane) {
    Scene scene = new Scene(
    mainPane
    );

    scene.getStylesheets().setAll(
    getClass().getResource("vista.css").toExternalForm()
    );

    return scene;
    }

    public static void main(String[] args) {
    launch(args);
    }
    }
    23 changes: 23 additions & 0 deletions MainController.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import javafx.fxml.FXML;
    import javafx.scene.Node;
    import javafx.scene.layout.StackPane;

    /**
    * Main controller class for the entire layout.
    */
    public class MainController {

    /** Holder of a switchable vista. */
    @FXML
    private StackPane vistaHolder;

    /**
    * Replaces the vista displayed in the vista holder with a new vista.
    *
    * @param node the vista node to be swapped in.
    */
    public void setVista(Node node) {
    vistaHolder.getChildren().setAll(node);
    }

    }
    19 changes: 19 additions & 0 deletions Vista1Controller.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;

    /**
    * Controller class for the first vista.
    */
    public class Vista1Controller {

    /**
    * Event handler fires when the user requests a new vista.
    *
    * @param event the event the triggered the handler.
    */
    @FXML
    void nextPane(ActionEvent event) {
    VistaNavigator.loadVista(VistaNavigator.VISTA_2);
    }

    }
    19 changes: 19 additions & 0 deletions Vista2Controller.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;

    /**
    * Controller class for the second vista.
    */
    public class Vista2Controller {

    /**
    * Event handler fires when the user requests a previous vista.
    *
    * @param event the event the triggered the handler.
    */
    @FXML
    void previousPane(ActionEvent event) {
    VistaNavigator.loadVista(VistaNavigator.VISTA_1);
    }

    }
    63 changes: 63 additions & 0 deletions VistaNavigator.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    import javafx.fxml.FXMLLoader;

    import java.io.IOException;

    /**
    * Utility class for controlling navigation between vistas.
    *
    * All methods on the navigator are static to facilitate
    * simple access from anywhere in the application.
    */
    public class VistaNavigator {

    /**
    * Convenience constants for fxml layouts managed by the navigator.
    */
    public static final String MAIN = "main.fxml";
    public static final String VISTA_1 = "vista1.fxml";
    public static final String VISTA_2 = "vista2.fxml";

    /** The main application layout controller. */
    private static MainController mainController;

    /**
    * Stores the main controller for later use in navigation tasks.
    *
    * @param mainController the main application layout controller.
    */
    public static void setMainController(MainController mainController) {
    VistaNavigator.mainController = mainController;
    }

    /**
    * Loads the vista specified by the fxml file into the
    * vistaHolder pane of the main application layout.
    *
    * Previously loaded vista for the same fxml file are not cached.
    * The fxml is loaded anew and a new vista node hierarchy generated
    * every time this method is invoked.
    *
    * A more sophisticated load function could potentially add some
    * enhancements or optimizations, for example:
    * cache FXMLLoaders
    * cache loaded vista nodes, so they can be recalled or reused
    * allow a user to specify vista node reuse or new creation
    * allow back and forward history like a browser
    *
    * @param fxml the fxml file to be loaded.
    */
    public static void loadVista(String fxml) {
    try {
    mainController.setVista(
    FXMLLoader.load(
    VistaNavigator.class.getResource(
    fxml
    )
    )
    );
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }
    12 changes: 12 additions & 0 deletions main.fxml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <?xml version="1.0" encoding="UTF-8"?>

    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?scenebuilder-stylesheet vista.css?>

    <VBox prefHeight="200.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml" fx:controller="MainController">
    <children>
    <Label fx:id="headerLabel" maxWidth="1.7976931348623157E308" text="Header" VBox.vgrow="NEVER" />
    <StackPane fx:id="vistaHolder" VBox.vgrow="ALWAYS" />
    </children>
    </VBox>
    28 changes: 28 additions & 0 deletions vista.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    /**
    * vista.css
    * Place in the same source directory as Main.java
    * Ensure that your build system copies this file to your build output directory.
    */

    #headerLabel {
    -fx-background-color: steelblue;
    -fx-text-fill: white;
    -fx-padding: 5px;
    }

    #vistaHolder {
    -fx-background-color: lightgrey;
    }

    #vista1 {
    -fx-background-color: coral;
    }

    #vista2 {
    -fx-background-color: aliceblue;
    }

    .button {
    -fx-base: lightblue;
    -fx-font-size: 20px;
    }
    11 changes: 11 additions & 0 deletions vista1.fxml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <?xml version="1.0" encoding="UTF-8"?>

    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <?scenebuilder-stylesheet vista.css?>

    <StackPane fx:id="vista1" xmlns:fx="http://javafx.com/fxml" fx:controller="Vista1Controller">
    <children>
    <Button mnemonicParsing="false" onAction="#nextPane" text="Next Pane" />
    </children>
    </StackPane>
    11 changes: 11 additions & 0 deletions vista2.fxml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <?xml version="1.0" encoding="UTF-8"?>

    <?scenebuilder-stylesheet vista.css?>

    <?import javafx.scene.control.Button?>
    <?import javafx.scene.layout.StackPane?>
    <StackPane fx:id="vista1" xmlns:fx="http://javafx.com/fxml" fx:controller="Vista2Controller">
    <children>
    <Button mnemonicParsing="false" onAction="#previousPane" text="Previous Pane" />
    </children>
    </StackPane>