summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2015-03-31 11:19:32 +0200
committerStefan Suhren <suhren.stefan@fh-swf.de>2015-03-31 11:19:32 +0200
commite53db140f27e405f4e541ad23b737836db36b2ad (patch)
tree8646193cd16ff1b4d0c1b95a4b7c8f2f18d21468 /src/de/fhswf/in/inf
parentbafe58a20a02e79b2af61c7b179e7d9ff68b8a4f (diff)
downloadJava2-e53db140f27e405f4e541ad23b737836db36b2ad.tar.gz
Java2-e53db140f27e405f4e541ad23b737836db36b2ad.zip
Exercise No.2.1 with extra MainBorderPain
Diffstat (limited to 'src/de/fhswf/in/inf')
-rw-r--r--src/de/fhswf/in/inf/java2/aufgabe02/CardChangeEvent.java75
-rw-r--r--src/de/fhswf/in/inf/java2/aufgabe02/JavaFxMain.java60
-rw-r--r--src/de/fhswf/in/inf/java2/aufgabe02/MainBorderPane.java54
3 files changed, 189 insertions, 0 deletions
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<ActionEvent>
+{
+
+ private ObservableList<Node> 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<Node> 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<String, String> 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<String, String> 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);
+ }
+}