JavaFX UI

This commit is contained in:
mherbaghinyan 2018-03-27 10:11:05 +04:00
parent c454749544
commit 5f90412f7c
5 changed files with 100 additions and 28 deletions

View File

@ -19,6 +19,8 @@ public class Main extends Application {
AnchorPane page = (AnchorPane) loader.load(); AnchorPane page = (AnchorPane) loader.load();
Scene scene = new Scene(page); Scene scene = new Scene(page);
scene.getStylesheets().add("/search.css");
primaryStage.setTitle("Title goes here"); primaryStage.setTitle("Title goes here");
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.show(); 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

@ -1,20 +1,18 @@
package com.baeldung.javafx.view; package com.baeldung.javafx.view;
import com.baeldung.javafx.model.Person;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.concurrent.Task; import javafx.concurrent.Task;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.Button; import javafx.scene.control.*;
import javafx.scene.control.Pagination;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode; import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox; import javafx.scene.layout.VBox;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
public class SearchController { public class SearchController {
@ -26,20 +24,17 @@ public class SearchController {
private Button searchButton; private Button searchButton;
@FXML @FXML
private Pagination pagination; private Pagination pagination;
@FXML
private Label searchLabel;
private ObservableList<Image> masterData = FXCollections.observableArrayList(); private ObservableList<Person> masterData = FXCollections.observableArrayList();
public SearchController() { public SearchController() {
masterData.add(new Person(5, "John", true));
masterData.add(new Person(7, "Albert", true));
masterData.add(new Person(11, "Monica", false));
} }
/**
* 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 @FXML
private void initialize() { private void initialize() {
@ -47,12 +42,18 @@ public class SearchController {
searchButton.setText("Search"); searchButton.setText("Search");
searchButton.setOnAction(event -> loadData()); searchButton.setOnAction(event -> loadData());
searchButton.setStyle("-fx-background-color: slateblue; -fx-text-fill: white;");
searchField.setOnKeyPressed(event -> { searchField.setOnKeyPressed(event -> {
if (event.getCode().equals(KeyCode.ENTER)) { if (event.getCode().equals(KeyCode.ENTER)) {
loadData(); loadData();
} }
}); });
searchField.textProperty().addListener((observable, oldValue, newValue) -> {
searchLabel.setText(newValue);
});
pagination.setPageFactory(SearchController.this::createPage); pagination.setPageFactory(SearchController.this::createPage);
} }
@ -60,7 +61,13 @@ public class SearchController {
VBox iconContainer = new VBox(); VBox iconContainer = new VBox();
//iconContainer.getChildren().add(myGrid); 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);
iconContainer.getChildren().add(tableView);
return iconContainer; return iconContainer;
} }
@ -69,29 +76,26 @@ public class SearchController {
String searchText = searchField.getText(); String searchText = searchField.getText();
Task<List<String>> task = new Task<List<String>>() { Task<List<Person>> task = new Task<List<Person>>() {
@Override @Override
protected List<String> call() throws Exception { protected List<Person> call() throws Exception {
updateMessage("Loading images"); updateMessage("Loading data");
return masterData
List<String> result = new ArrayList<>(); .stream()
.filter(value -> value.getName().toLowerCase().contains(searchText))
return result; .collect(Collectors.toList());
} }
}; };
// task.setOnRunning((e) -> loadingDialog.show());
task.setOnSucceeded(event -> { task.setOnSucceeded(event -> {
List<String> data = task.getValue(); List<Person> data = task.getValue();
data.forEach(url -> masterData.add(new Image(url))); data.forEach(p -> masterData.add(p));
pagination.setVisible(true); pagination.setVisible(true);
pagination.setPageCount(masterData.size() / PAGE_ITEMS_COUNT); pagination.setPageCount(masterData.size() / PAGE_ITEMS_COUNT);
}); });
Thread th = new Thread(task); Thread th = new Thread(task);
th.setDaemon(true); th.setDaemon(true);
th.start(); th.start();
} }

View File

@ -14,9 +14,10 @@
<HBox id="HBox" alignment="CENTER" spacing="5.0" AnchorPane.leftAnchor="10.0" <HBox id="HBox" alignment="CENTER" spacing="5.0" AnchorPane.leftAnchor="10.0"
AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0"> AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="10.0">
<children> <children>
<Label text="Search Icons:"/> <Label text="Search Text:"/>
<TextField fx:id="searchField" prefWidth="-1.0" HBox.hgrow="ALWAYS"/> <TextField fx:id="searchField" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
<Button fx:id="searchButton" prefWidth="-1.0" HBox.hgrow="ALWAYS"/> <Button fx:id="searchButton" prefWidth="-1.0" HBox.hgrow="ALWAYS"/>
<Label fx:id="searchLabel"/>
</children> </children>
</HBox> </HBox>

View File

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