Skip to content

Instantly share code, notes, and snippets.

@hastebrot
Forked from jewelsea/Main.java
Created March 1, 2014 00:54
Show Gist options
  • Save hastebrot/9283038 to your computer and use it in GitHub Desktop.
Save hastebrot/9283038 to your computer and use it in GitHub Desktop.
<?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>
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);
}
}
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);
}
}
/**
* 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: aliceblue;
}
#vista2 {
-fx-background-color: coral;
}
.button {
-fx-base: lightblue;
-fx-font-size: 20px;
}
<?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>
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
/**
* Controller class for the first vista.
*/
public class Vista1Controller {
/**
* Event handler fired when the user requests a new vista.
*
* @param event the event that triggered the handler.
*/
@FXML
void nextPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_2);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?scenebuilder-stylesheet vista.css?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<StackPane fx:id="vista2" xmlns:fx="http://javafx.com/fxml" fx:controller="Vista2Controller">
<children>
<Button mnemonicParsing="false" onAction="#previousPane" text="Previous Pane" />
</children>
</StackPane>
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
/**
* Controller class for the second vista.
*/
public class Vista2Controller {
/**
* Event handler fired when the user requests a previous vista.
*
* @param event the event that triggered the handler.
*/
@FXML
void previousPane(ActionEvent event) {
VistaNavigator.loadVista(VistaNavigator.VISTA_1);
}
}
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment