A minimal JavaFX Presentation (in JavaFX)
If you want to do a presentation about JavaFX, it’s very handy to do the presentation itself in JavaFX. This way you can easily show your examples without leaving the presentation. Here’s a very minimal example how to do that. In NetBeans set up a new JavaFX Project “New Project” -> “JavaFX” -> “JavaFX Application” and name it “FXPresenter”. Now create the Slide class. It’s used to load an FXML file:
package fxpresenter; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.layout.AnchorPane; public class Slide extends AnchorPane implements Initializable { public Slide(String slide) { FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(slide)); fxmlLoader.setRoot(this); fxmlLoader.setController(this); try { fxmlLoader.load(); } catch (IOException exception) { throw new RuntimeException(exception); } } @Override public void initialize(URL url, ResourceBundle rb) { } }
Next we need a Presentation that contains the slides and switches between them:
package fxpresenter; import java.util.ArrayList; import java.util.List; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.stage.Screen; public class Presentation extends Group { private List<Slide> slides; private int index; private Slide current; public EventHandler<KeyEvent> keyEventHandler; public Presentation() { this.slides = new ArrayList<>(); keyEventHandler = new EventHandler<KeyEvent>() { public void handle(final KeyEvent keyEvent) { if (keyEvent.getCode() == KeyCode.LEFT) { previousSlidePlease(); } else if (keyEvent.getCode() == KeyCode.RIGHT) { nextSlidePlease(); } } }; } public void addSlide(Slide slide) { addSlide(slides.size(), slide); } public void addSlide(int index, Slide slide) { slides.add(index, slide); } public void previousSlidePlease() { if (index > 0) { index--; } setSlide(index); } public void nextSlidePlease() { if (index < slides.size() - 1) { index++; } setSlide(index); } public void setSlide(int index) { if (current != null) { getChildren().remove(current); current.removeEventHandler(KeyEvent.KEY_PRESSED, keyEventHandler); } current = slides.get(index); current.addEventHandler(KeyEvent.KEY_PRESSED, keyEventHandler); scaleToFit(); getChildren().add(slides.get(index)); current.requestFocus(); } void start() { index = -1; nextSlidePlease(); } private void scaleToFit() { javafx.geometry.Rectangle2D screenBounds = Screen.getPrimary().getBounds(); double prefWidth = current.getPrefWidth(); double prefHeight = current.getPrefHeight(); double scaleX = screenBounds.getWidth() / prefWidth; double scaleY = screenBounds.getHeight() / prefHeight; double centerX = (screenBounds.getWidth() / 2) - (prefWidth / 2); double centerY = (screenBounds.getHeight() / 2) - (prefHeight / 2); setTranslateX(centerX); setTranslateY(centerY); setScaleX(scaleX); setScaleY(scaleY); } }
It should be pretty obvious what the code does: If you set a slide it will be scaled to fit the screen, and it will listen to key events. pressing the right arrow moves to the next slide, and the right arrow key set’s the previous slide. And finally we need an Application to display the whole stuff in full screen:
package fxpresenter; import java.util.List; import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Screen; import javafx.stage.Stage; public class FXPresenter extends Application { @Override public void start(Stage stage) throws Exception { final Presentation presentation = new Presentation(); presentation.addSlide(new Slide('Slide1.fxml')); presentation.addSlide(new Slide('Slide2.fxml')); final Scene scene = new Scene(presentation); stage.setScene(scene); stage.setFullScreen(true); presentation.start(); List<Screen> screens = Screen.getScreens(); stage.show(); } public static void main(String[] args) { launch(args); } }
You’re done. Now you can go ahead and create your slides. For the sample create two new FXML files named “Slide1.fxml” and “Slide2.fxml”, and style them using the SceneBuilder, and your ready to go. Here’s a little video showing how you can use SceneBuilder to create the Slides:
Reference: A minimal JavaFX Presentation (in JavaFX) from our JCG partner Toni Epple at the Eppleton blog.