From c03364a718d5c16b08c42342824ea5201affb8f9 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Wed, 16 Dec 2020 16:54:51 +0100 Subject: [PATCH 1/3] add file chooser to test button --- src/main/java/de/skymatic/javafxtest/App.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/de/skymatic/javafxtest/App.java b/src/main/java/de/skymatic/javafxtest/App.java index d2ed815..3a65fd1 100644 --- a/src/main/java/de/skymatic/javafxtest/App.java +++ b/src/main/java/de/skymatic/javafxtest/App.java @@ -11,6 +11,7 @@ import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; import javafx.scene.text.Text; +import javafx.stage.FileChooser; import javafx.stage.Stage; /** @@ -27,6 +28,13 @@ public void start(Stage stage) { var textInputFiled = new TextField(); textInputFiled.setPromptText("Test text field: Enter anything you want."); var button = new Button("Test Button"); + button.setOnAction(actionEvent -> { + var fileChooser = new FileChooser(); + var file = fileChooser.showOpenDialog(stage); + if( file != null){ + System.out.println(file.getPath()); + } + }); var vbox = new VBox(label, textInputFiled, button); vbox.setPadding(new Insets(20)); vbox.setAlignment(Pos.CENTER); From c6ba4a7f4df840c690e5ba1486224145dafb4a0c Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Wed, 16 Dec 2020 18:35:57 +0100 Subject: [PATCH 2/3] simplify app to just choose a newly created file --- src/main/java/de/skymatic/javafxtest/App.java | 72 +++++++++---------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/src/main/java/de/skymatic/javafxtest/App.java b/src/main/java/de/skymatic/javafxtest/App.java index 3a65fd1..de45d92 100644 --- a/src/main/java/de/skymatic/javafxtest/App.java +++ b/src/main/java/de/skymatic/javafxtest/App.java @@ -1,59 +1,51 @@ package de.skymatic.javafxtest; import javafx.application.Application; -import javafx.geometry.Insets; -import javafx.geometry.Pos; -import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.control.Label; -import javafx.scene.control.TextField; -import javafx.scene.layout.HBox; -import javafx.scene.layout.StackPane; -import javafx.scene.layout.VBox; -import javafx.scene.text.Text; import javafx.stage.FileChooser; import javafx.stage.Stage; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.UUID; + /** - * JavaFX App + * Test App for JavaFX Bug */ public class App extends Application { + private static final String PROBLEMATIC_CHAR = "\uD83C\uDF40"; //pile of poo emoji + private static final String tmpDir = System.getProperty("java.io.tmpdir"); + + private static Path workingDir; + private static Path expectedFile; + @Override public void start(Stage stage) { - var javaVersion = SystemInfo.javaVersion(); - var javafxVersion = SystemInfo.javafxVersion(); - - var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + "."); - var textInputFiled = new TextField(); - textInputFiled.setPromptText("Test text field: Enter anything you want."); - var button = new Button("Test Button"); - button.setOnAction(actionEvent -> { - var fileChooser = new FileChooser(); - var file = fileChooser.showOpenDialog(stage); - if( file != null){ - System.out.println(file.getPath()); + var fileChooser = new FileChooser(); + fileChooser.setInitialDirectory(workingDir.toFile()); + var chosenFile = fileChooser.showOpenDialog(stage); + if (chosenFile != null) { + if (!chosenFile.toPath().equals(expectedFile)) { + throw new IllegalStateException("Chosen path is not correct: " + chosenFile.getPath()); } - }); - var vbox = new VBox(label, textInputFiled, button); - vbox.setPadding(new Insets(20)); - vbox.setAlignment(Pos.CENTER); - vbox.setSpacing(10); - - var text = new Text("子貢問曰:有一言而可以終身行之者乎?子曰:其恕乎!己所不欲、勿施於人。"); //https://en.wikipedia.org/wiki/Confucius - var quoteBox = new HBox(text); - quoteBox.setAlignment(Pos.BOTTOM_RIGHT); - HBox.setMargin(text, new Insets(20)); - - var stackPane = new StackPane(quoteBox, vbox); - var scene = new Scene(stackPane, 640, 480); - stage.setScene(scene); - stage.setTitle("JavaFX Test App"); - stage.show(); + } + stage.close(); + System.exit(0); } - public static void main(String[] args) { + private static void prepare() throws IOException { + var dir = Files.createDirectories(Path.of(tmpDir).resolve(UUID.randomUUID()+"/" + PROBLEMATIC_CHAR)); + workingDir = dir.getParent(); + expectedFile = Files.createFile(dir.resolve("foo")); + } + + public static void main(String[] args) throws IOException { + assert tmpDir != null; + prepare(); + assert workingDir != null; launch(); } + } \ No newline at end of file From 222b499077dc35fadd0e9e596935f531a2ab371d Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Wed, 16 Dec 2020 18:57:21 +0100 Subject: [PATCH 3/3] correct wrong comment --- src/main/java/de/skymatic/javafxtest/App.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/de/skymatic/javafxtest/App.java b/src/main/java/de/skymatic/javafxtest/App.java index de45d92..95d5639 100644 --- a/src/main/java/de/skymatic/javafxtest/App.java +++ b/src/main/java/de/skymatic/javafxtest/App.java @@ -14,7 +14,7 @@ */ public class App extends Application { - private static final String PROBLEMATIC_CHAR = "\uD83C\uDF40"; //pile of poo emoji + private static final String PROBLEMATIC_CHAR = "\uD83C\uDF40"; //FOUR LEAF CLOVER private static final String tmpDir = System.getProperty("java.io.tmpdir"); private static Path workingDir; @@ -47,5 +47,4 @@ public static void main(String[] args) throws IOException { launch(); } - } \ No newline at end of file