Skip to content

Commit c454749

Browse files
committed
javafx main application
1 parent 510ad19 commit c454749

3 files changed

Lines changed: 157 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.javafx;
2+
3+
import javafx.application.Application;
4+
import javafx.fxml.FXMLLoader;
5+
import javafx.scene.Scene;
6+
import javafx.scene.layout.AnchorPane;
7+
import javafx.stage.Stage;
8+
9+
public class Main extends Application {
10+
11+
public static void main(String[] args) {
12+
launch(args);
13+
}
14+
15+
@Override
16+
public void start(Stage primaryStage) throws Exception {
17+
18+
FXMLLoader loader = new FXMLLoader(Main.class.getResource("/SearchController.fxml"));
19+
AnchorPane page = (AnchorPane) loader.load();
20+
Scene scene = new Scene(page);
21+
22+
primaryStage.setTitle("Title goes here");
23+
primaryStage.setScene(scene);
24+
primaryStage.show();
25+
26+
}
27+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.baeldung.javafx.view;
2+
3+
4+
import javafx.collections.FXCollections;
5+
import javafx.collections.ObservableList;
6+
import javafx.concurrent.Task;
7+
import javafx.fxml.FXML;
8+
import javafx.scene.Node;
9+
import javafx.scene.control.Button;
10+
import javafx.scene.control.Pagination;
11+
import javafx.scene.control.TextField;
12+
import javafx.scene.image.Image;
13+
import javafx.scene.input.KeyCode;
14+
import javafx.scene.layout.VBox;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
19+
public class SearchController {
20+
21+
public static final int PAGE_ITEMS_COUNT = 30;
22+
23+
@FXML
24+
private TextField searchField;
25+
@FXML
26+
private Button searchButton;
27+
@FXML
28+
private Pagination pagination;
29+
30+
private ObservableList<Image> masterData = FXCollections.observableArrayList();
31+
32+
33+
public SearchController() {
34+
35+
}
36+
37+
/**
38+
* Initializes the controller class. This method is automatically called
39+
* after the fxml file has been loaded.
40+
* <p>
41+
* Initializes the table columns and sets up sorting and filtering.
42+
*/
43+
@FXML
44+
private void initialize() {
45+
46+
// search panel
47+
searchButton.setText("Search");
48+
searchButton.setOnAction(event -> loadData());
49+
50+
searchField.setOnKeyPressed(event -> {
51+
if (event.getCode().equals(KeyCode.ENTER)) {
52+
loadData();
53+
}
54+
});
55+
56+
pagination.setPageFactory(SearchController.this::createPage);
57+
}
58+
59+
private Node createPage(Integer pageIndex) {
60+
61+
VBox iconContainer = new VBox();
62+
63+
//iconContainer.getChildren().add(myGrid);
64+
65+
return iconContainer;
66+
}
67+
68+
private void loadData() {
69+
70+
String searchText = searchField.getText();
71+
72+
Task<List<String>> task = new Task<List<String>>() {
73+
@Override
74+
protected List<String> call() throws Exception {
75+
updateMessage("Loading images");
76+
77+
List<String> result = new ArrayList<>();
78+
79+
return result;
80+
}
81+
};
82+
83+
// task.setOnRunning((e) -> loadingDialog.show());
84+
task.setOnSucceeded(event -> {
85+
List<String> data = task.getValue();
86+
data.forEach(url -> masterData.add(new Image(url)));
87+
pagination.setVisible(true);
88+
pagination.setPageCount(masterData.size() / PAGE_ITEMS_COUNT);
89+
});
90+
91+
Thread th = new Thread(task);
92+
93+
th.setDaemon(true);
94+
95+
th.start();
96+
}
97+
98+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.control.Button?>
4+
<?import javafx.scene.control.Label?>
5+
<?import javafx.scene.control.Pagination?>
6+
<?import javafx.scene.control.TextField?>
7+
<?import javafx.scene.layout.*?>
8+
<AnchorPane xmlns:fx="http://javafx.com/fxml"
9+
xmlns="http://javafx.com/javafx"
10+
fx:controller="com.baeldung.javafx.view.SearchController"
11+
prefHeight="400.0" prefWidth="600.0">
12+
<children>
13+
14+
<HBox id="HBox" alignment="CENTER" spacing="5.0" AnchorPane.leftAnchor="10.0"
15+
AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
16+
<children>
17+
<Label text="Search Icons:"/>
18+
<TextField fx:id="searchField" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
19+
<Button fx:id="searchButton" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
20+
</children>
21+
</HBox>
22+
23+
<Pagination fx:id="pagination"
24+
AnchorPane.leftAnchor="10.0"
25+
AnchorPane.rightAnchor="10.0"
26+
AnchorPane.topAnchor="50.0"
27+
visible="false">
28+
29+
</Pagination>
30+
31+
</children>
32+
</AnchorPane>

0 commit comments

Comments
 (0)