javafx main application
This commit is contained in:
parent
510ad19f90
commit
c454749544
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
|
@ -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<Image> masterData = FXCollections.observableArrayList();
|
||||
|
||||
|
||||
public SearchController() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the controller class. This method is automatically called
|
||||
* after the fxml file has been loaded.
|
||||
* <p>
|
||||
* 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<List<String>> task = new Task<List<String>>() {
|
||||
@Override
|
||||
protected List<String> call() throws Exception {
|
||||
updateMessage("Loading images");
|
||||
|
||||
List<String> result = new ArrayList<>();
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
// task.setOnRunning((e) -> loadingDialog.show());
|
||||
task.setOnSucceeded(event -> {
|
||||
List<String> 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.control.Pagination?>
|
||||
<?import javafx.scene.control.TextField?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<AnchorPane xmlns:fx="http://javafx.com/fxml"
|
||||
xmlns="http://javafx.com/javafx"
|
||||
fx:controller="com.baeldung.javafx.view.SearchController"
|
||||
prefHeight="400.0" prefWidth="600.0">
|
||||
<children>
|
||||
|
||||
<HBox id="HBox" alignment="CENTER" spacing="5.0" AnchorPane.leftAnchor="10.0"
|
||||
AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
|
||||
<children>
|
||||
<Label text="Search Icons:"/>
|
||||
<TextField fx:id="searchField" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
|
||||
<Button fx:id="searchButton" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
|
||||
</children>
|
||||
</HBox>
|
||||
|
||||
<Pagination fx:id="pagination"
|
||||
AnchorPane.leftAnchor="10.0"
|
||||
AnchorPane.rightAnchor="10.0"
|
||||
AnchorPane.topAnchor="50.0"
|
||||
visible="false">
|
||||
|
||||
</Pagination>
|
||||
|
||||
</children>
|
||||
</AnchorPane>
|
Loading…
Reference in New Issue