Merge pull request #3894 from MherBaghinyan/BAEL-1638

Bael 1638
This commit is contained in:
Loredana Crusoveanu 2018-04-04 01:04:27 +03:00 committed by GitHub
commit a81bd92cef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 228 additions and 1 deletions

View File

@ -0,0 +1,29 @@
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);
scene.getStylesheets().add("/search.css");
primaryStage.setTitle("Title goes here");
primaryStage.setScene(scene);
primaryStage.show();
}
}

View File

@ -0,0 +1,61 @@
package com.baeldung.javafx.model;
import javafx.beans.property.*;
public class Person {
private SimpleIntegerProperty id;
private SimpleStringProperty name;
private SimpleBooleanProperty isEmployed;
public Person(Integer id, String name, boolean isEmployed) {
this.id = new SimpleIntegerProperty(id);
this.name = new SimpleStringProperty(name);
this.isEmployed = new SimpleBooleanProperty(isEmployed);
}
public int getId() {
return id.get();
}
public IntegerProperty idProperty() {
return id;
}
public void setId(int id) {
this.id.set(id);
}
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public boolean getIsEmployed() {
return isEmployed.get();
}
public BooleanProperty isEmployedProperty() {
return isEmployed;
}
public void setIsEmployed(boolean isEmployed) {
this.isEmployed.set(isEmployed);
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", isEmployed=" + isEmployed +
'}';
}
}

View File

@ -0,0 +1,100 @@
package com.baeldung.javafx.view;
import com.baeldung.javafx.model.Person;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import java.util.List;
import java.util.stream.Collectors;
public class SearchController {
public static final int PAGE_ITEMS_COUNT = 10;
@FXML
private TextField searchField;
@FXML
private Button searchButton;
@FXML
private Pagination pagination;
@FXML
private Label searchLabel;
private ObservableList<Person> masterData = FXCollections.observableArrayList();
public SearchController() {
masterData.add(new Person(5, "John", true));
masterData.add(new Person(7, "Albert", true));
masterData.add(new Person(11, "Monica", false));
}
@FXML
private void initialize() {
// search panel
searchButton.setText("Search");
searchButton.setOnAction(event -> loadData());
searchButton.setStyle("-fx-background-color: #457ecd; -fx-text-fill: #ffffff;");
searchField.setOnKeyPressed(event -> {
if (event.getCode().equals(KeyCode.ENTER)) {
loadData();
}
});
searchField.textProperty().addListener((observable, oldValue, newValue) -> {
searchLabel.setText(newValue);
});
pagination.setPageFactory(SearchController.this::createPage);
}
private Node createPage(Integer pageIndex) {
VBox dataContainer = new VBox();
TableView<Person> tableView = new TableView<>(masterData);
TableColumn id = new TableColumn("ID");
TableColumn name = new TableColumn("NAME");
TableColumn employed = new TableColumn("EMPLOYED");
tableView.getColumns().addAll(id, name, employed);
dataContainer.getChildren().add(tableView);
return dataContainer;
}
private void loadData() {
String searchText = searchField.getText();
Task<ObservableList<Person>> task = new Task<ObservableList<Person>>() {
@Override
protected ObservableList<Person> call() throws Exception {
updateMessage("Loading data");
return FXCollections.observableArrayList(masterData
.stream()
.filter(value -> value.getName().toLowerCase().contains(searchText))
.collect(Collectors.toList()));
}
};
task.setOnSucceeded(event -> {
masterData = task.getValue();
pagination.setVisible(true);
pagination.setPageCount(masterData.size() / PAGE_ITEMS_COUNT);
});
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
}
}

View File

@ -0,0 +1,33 @@
<?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 Text:"/>
<TextField fx:id="searchField" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
<Button fx:id="searchButton" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
<Label fx:id="searchLabel"/>
</children>
</HBox>
<Pagination fx:id="pagination"
AnchorPane.leftAnchor="10.0"
AnchorPane.rightAnchor="10.0"
AnchorPane.topAnchor="50.0"
visible="false">
</Pagination>
</children>
</AnchorPane>

View File

@ -0,0 +1,4 @@
.button {
-fx-background-color: red;
-fx-text-fill: white;
}

View File

@ -289,7 +289,7 @@
<module>persistence-modules/java-jdbi</module>
<module>jersey</module>
<module>java-spi</module>
</modules>
</modules>
<dependencies>
<!-- logging -->