From e53db140f27e405f4e541ad23b737836db36b2ad Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Tue, 31 Mar 2015 11:19:32 +0200 Subject: Exercise No.2.1 with extra MainBorderPain --- .../in/inf/java2/aufgabe02/CardChangeEvent.java | 75 ++++++++++++++++++++++ .../fhswf/in/inf/java2/aufgabe02/JavaFxMain.java | 60 +++++++++++++++++ .../in/inf/java2/aufgabe02/MainBorderPane.java | 54 ++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 src/de/fhswf/in/inf/java2/aufgabe02/CardChangeEvent.java create mode 100644 src/de/fhswf/in/inf/java2/aufgabe02/JavaFxMain.java create mode 100644 src/de/fhswf/in/inf/java2/aufgabe02/MainBorderPane.java diff --git a/src/de/fhswf/in/inf/java2/aufgabe02/CardChangeEvent.java b/src/de/fhswf/in/inf/java2/aufgabe02/CardChangeEvent.java new file mode 100644 index 0000000..2050536 --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe02/CardChangeEvent.java @@ -0,0 +1,75 @@ +/** + * Class CardChangeEvent. + */ + +package de.fhswf.in.inf.java2.aufgabe02; + +import javafx.collections.ObservableList; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.scene.Node; +import javafx.scene.control.Label; + +/** + * Generic handler for loading Labels on button click. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class CardChangeEvent implements EventHandler +{ + + private ObservableList children; + + private String labelName; + + private Label label = null; + + /** + * Adds an eventHandler for the button so that a label is loaded into the + * stack pane. + * + */ + public CardChangeEvent(ObservableList children, String labelName) + { + if (children == null) + { + throw new IllegalArgumentException("children can't be null."); + } + + if (labelName == null) + { + throw new IllegalArgumentException("labelName can't be null."); + } + + if (labelName.isEmpty()) + { + throw new IllegalArgumentException("labelName can't be empty."); + } + + this.children = children; + this.labelName = labelName; + } + + /* + * (non-Javadoc) + * + * @see javafx.event.EventHandler#handle(javafx.event.Event) + */ + @Override + public void handle(ActionEvent event) + { + for (Node node : children) + { + node.setVisible(false); + } + + if (label == null) + { + label = new Label(labelName); + children.add(label); + } + + label.setVisible(true); + } +} diff --git a/src/de/fhswf/in/inf/java2/aufgabe02/JavaFxMain.java b/src/de/fhswf/in/inf/java2/aufgabe02/JavaFxMain.java new file mode 100644 index 0000000..21a98cc --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe02/JavaFxMain.java @@ -0,0 +1,60 @@ +/** + * File for the JavaFX class. + */ + +package de.fhswf.in.inf.java2.aufgabe02; + +import java.util.LinkedHashMap; + +import javafx.application.Application; +import javafx.scene.Scene; +import javafx.stage.Stage; + +/** + * The class which draws the hello world application. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class JavaFxMain extends Application +{ + + /** + * Calls the function to build the main window and entry point for the + * graphical application. + * + * @param args + * Command line arguments + */ + public static void main(String[] args) + { + launch(args); + } + + /* + * (non-Javadoc) + * + * @see javafx.application.Application#start(javafx.stage.Stage) + */ + @Override + public void start(Stage primaryStage) throws Exception + { + + LinkedHashMap labels = new LinkedHashMap<>(); + + for (int i = 1; i < 15; i++) + { + labels.put("Button " + i, "Label " + i); + } + + MainBorderPane root = new MainBorderPane(labels); + Scene scene = new Scene(root); + + primaryStage.setTitle("Label Control!"); + + primaryStage.setScene(scene); + primaryStage.show(); + + } + +} diff --git a/src/de/fhswf/in/inf/java2/aufgabe02/MainBorderPane.java b/src/de/fhswf/in/inf/java2/aufgabe02/MainBorderPane.java new file mode 100644 index 0000000..c70e1b3 --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe02/MainBorderPane.java @@ -0,0 +1,54 @@ +/** + * Class MainBorderPain. + */ + +package de.fhswf.in.inf.java2.aufgabe02; + +import java.util.Map; + +import javafx.scene.control.Button; +import javafx.scene.layout.BorderPane; +import javafx.scene.layout.StackPane; +import javafx.scene.layout.VBox; + +/** + * Dynamically creates the buttons and labels. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class MainBorderPane extends BorderPane +{ + + StackPane labelPane = new StackPane(); + + VBox buttonPane = new VBox(); + + /** + * Creates buttons with text from key and the button creates a label with + * name from value. + * + * @param names + * The Map with the key value mappings. + * + */ + public MainBorderPane(Map names) + { + if (names == null) + { + throw new IllegalArgumentException("labels can't be null."); + } + + Button tmp; + for (String name : names.keySet()) + { + tmp = new Button(name); + tmp.setOnAction(new CardChangeEvent(labelPane.getChildren(), names + .get(name))); + buttonPane.getChildren().add(tmp); + } + + setLeft(buttonPane); + setCenter(labelPane); + } +} -- cgit v1.2.3-70-g09d2