From c45474954408111f00f0ab7c7fb4d206b52c70e3 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Thu, 22 Mar 2018 09:20:32 +0400 Subject: [PATCH 1/4] javafx main application --- .../main/java/com/baeldung/javafx/Main.java | 27 +++++ .../javafx/view/SearchController.java | 98 +++++++++++++++++++ .../src/main/resources/SearchController.fxml | 32 ++++++ 3 files changed, 157 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/javafx/Main.java create mode 100644 core-java/src/main/java/com/baeldung/javafx/view/SearchController.java create mode 100644 core-java/src/main/resources/SearchController.fxml diff --git a/core-java/src/main/java/com/baeldung/javafx/Main.java b/core-java/src/main/java/com/baeldung/javafx/Main.java new file mode 100644 index 0000000000..1107804ad2 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/javafx/Main.java @@ -0,0 +1,27 @@ +package com.baeldung.javafx; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.layout.AnchorPane; +import javafx.stage.Stage; + +public class Main extends Application { + + public static void main(String[] args) { + launch(args); + } + + @Override + public void start(Stage primaryStage) throws Exception { + + FXMLLoader loader = new FXMLLoader(Main.class.getResource("/SearchController.fxml")); + AnchorPane page = (AnchorPane) loader.load(); + Scene scene = new Scene(page); + + primaryStage.setTitle("Title goes here"); + primaryStage.setScene(scene); + primaryStage.show(); + + } +} diff --git a/core-java/src/main/java/com/baeldung/javafx/view/SearchController.java b/core-java/src/main/java/com/baeldung/javafx/view/SearchController.java new file mode 100644 index 0000000000..b3dec4134f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/javafx/view/SearchController.java @@ -0,0 +1,98 @@ +package com.baeldung.javafx.view; + + +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; +import javafx.concurrent.Task; +import javafx.fxml.FXML; +import javafx.scene.Node; +import javafx.scene.control.Button; +import javafx.scene.control.Pagination; +import javafx.scene.control.TextField; +import javafx.scene.image.Image; +import javafx.scene.input.KeyCode; +import javafx.scene.layout.VBox; + +import java.util.ArrayList; +import java.util.List; + +public class SearchController { + + public static final int PAGE_ITEMS_COUNT = 30; + + @FXML + private TextField searchField; + @FXML + private Button searchButton; + @FXML + private Pagination pagination; + + private ObservableList masterData = FXCollections.observableArrayList(); + + + public SearchController() { + + } + + /** + * Initializes the controller class. This method is automatically called + * after the fxml file has been loaded. + *

+ * Initializes the table columns and sets up sorting and filtering. + */ + @FXML + private void initialize() { + + // search panel + searchButton.setText("Search"); + searchButton.setOnAction(event -> loadData()); + + searchField.setOnKeyPressed(event -> { + if (event.getCode().equals(KeyCode.ENTER)) { + loadData(); + } + }); + + pagination.setPageFactory(SearchController.this::createPage); + } + + private Node createPage(Integer pageIndex) { + + VBox iconContainer = new VBox(); + + //iconContainer.getChildren().add(myGrid); + + return iconContainer; + } + + private void loadData() { + + String searchText = searchField.getText(); + + Task> task = new Task>() { + @Override + protected List call() throws Exception { + updateMessage("Loading images"); + + List result = new ArrayList<>(); + + return result; + } + }; + +// task.setOnRunning((e) -> loadingDialog.show()); + task.setOnSucceeded(event -> { + List data = task.getValue(); + data.forEach(url -> masterData.add(new Image(url))); + pagination.setVisible(true); + pagination.setPageCount(masterData.size() / PAGE_ITEMS_COUNT); + }); + + Thread th = new Thread(task); + + th.setDaemon(true); + + th.start(); + } + +} diff --git a/core-java/src/main/resources/SearchController.fxml b/core-java/src/main/resources/SearchController.fxml new file mode 100644 index 0000000000..54be1061e8 --- /dev/null +++ b/core-java/src/main/resources/SearchController.fxml @@ -0,0 +1,32 @@ + + + + + + + + + + + + +