Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test;
- import java.util.Comparator;
- import javafx.application.Application;
- import javafx.beans.property.ReadOnlyObjectWrapper;
- import javafx.beans.property.SimpleStringProperty;
- import javafx.beans.value.ObservableValue;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Label;
- import javafx.scene.control.TableCell;
- import javafx.scene.control.TableColumn;
- import javafx.scene.control.TableColumn.CellDataFeatures;
- import javafx.scene.control.TableView;
- import javafx.scene.control.cell.PropertyValueFactory;
- import javafx.scene.layout.Pane;
- import javafx.scene.layout.Priority;
- import javafx.scene.layout.VBox;
- import javafx.scene.text.Font;
- import javafx.scene.text.Text;
- import javafx.scene.text.TextAlignment;
- import javafx.stage.Stage;
- import javafx.util.Callback;
- public class TableTest extends Application {
- private TableView<Person> table = new TableView<Person>();
- private final ObservableList<Person> data =
- FXCollections.observableArrayList(
- new Person("Jacob", "Smith", "jacob.smith@example.com","Coffee"),
- new Person("Isabella", "Johnson", "isabella.johnson@example.com","Fruit"),
- new Person("Ethan", "Williams", "ethan.williams@example.com","Fruit"),
- new Person("Emma", "Jones", "emma.jones@example.com","Coffee"),
- new Person("Michael", "Brown", "michael.brown@example.com","Fruit")
- );
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage stage) {
- stage.setTitle("Table View Sample");
- final Label label = new Label("Address Book");
- label.setFont(new Font("Arial", 20));
- final Label actionTaken = new Label();
- table.setEditable(true);
- TableColumn firstNameCol = new TableColumn("First Name");
- firstNameCol.setMinWidth(100);
- firstNameCol.setCellValueFactory(
- new PropertyValueFactory<Person, String>("firstName"));
- TableColumn lastNameCol = new TableColumn("Last Name");
- lastNameCol.setMinWidth(100);
- lastNameCol.setCellValueFactory(
- new PropertyValueFactory<Person, String>("lastName"));
- TableColumn emailCol = new TableColumn("Email");
- emailCol.setMinWidth(200);
- emailCol.setCellValueFactory(
- new PropertyValueFactory<Person, String>("email"));
- TableColumn<Person, Person> multiCol = new TableColumn<>("Multiline");
- multiCol.setMinWidth(200);
- multiCol.setCellValueFactory(new Callback<CellDataFeatures<Person, Person>, ObservableValue<Person>>() {
- @Override public ObservableValue<Person> call(CellDataFeatures<Person, Person> features) {
- return new ReadOnlyObjectWrapper(features.getValue());
- }
- });
- multiCol.setComparator(new Comparator<Person>() {
- @Override public int compare(Person p1, Person p2) {
- return p1.getLikes().compareTo(p2.getLikes());
- }
- });
- multiCol.setCellFactory(new Callback<TableColumn<Person, Person>, TableCell<Person, Person>>() {
- @Override public TableCell<Person, Person> call(TableColumn<Person, Person> multiCol) {
- return new TableCell<Person, Person>() {
- private Group grp = null;
- @Override public void updateItem(final Person person, boolean empty) {
- super.updateItem(person, empty);
- this.setAlignment(Pos.CENTER);
- if (!isEmpty()) {
- Text text = new Text(person.getFirstName());
- text.setX(0);
- text.setY(0);
- text.setTextAlignment(TextAlignment.CENTER); // Center text?
- Pane pane = new Pane();
- pane.setStyle("-fx-background-color: #66BB66;");
- pane.setLayoutX(0);
- pane.setLayoutY(0);
- pane.setPrefHeight(20);
- pane.setPrefWidth(this.prefWidth(-1)); // Column width?
- // -----
- Text text2 = new Text(person.getLastName());
- text2.setX(0);
- text2.setY(20);
- text2.setTextAlignment(TextAlignment.CENTER); // Center text?
- Pane pane2 = new Pane();
- pane2.setStyle("-fx-background-color: #79A8D8;");
- pane2.setLayoutX(0);
- pane2.setLayoutY(20);
- pane2.setPrefHeight(20);
- pane2.setPrefWidth(this.prefWidth(-1)); // Column width?
- // -----
- Text text3 = new Text(person.getEmail());
- text3.setX(0);
- text3.setY(40);
- text3.setTextAlignment(TextAlignment.CENTER); // Center text?
- Pane pane3 = new Pane();
- pane3.setStyle("-fx-background-color: #FF8888;");
- pane3.setLayoutX(0);
- pane3.setLayoutY(40);
- pane3.setPrefHeight(20);
- pane3.setPrefWidth(this.prefWidth(-1)); // Column width?
- // -----
- Group grp = new Group();
- grp.getChildren().add(pane);
- grp.getChildren().add(text);
- grp.getChildren().add(pane2);
- grp.getChildren().add(text2);
- grp.getChildren().add(pane3);
- grp.getChildren().add(text3);
- setGraphic(grp);
- setStyle("-fx-padding: 0 0 0 0;");
- }
- }
- };
- }
- });
- table.setItems(data);
- table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, multiCol);
- final VBox vbox = new VBox();
- vbox.setSpacing(5);
- vbox.setPadding(new Insets(10, 10, 10, 10));
- vbox.getChildren().addAll(label, table, actionTaken);
- VBox.setVgrow(table, Priority.ALWAYS);
- stage.setScene(new Scene(vbox));
- stage.show();
- }
- public static class Person {
- private final SimpleStringProperty firstName;
- private final SimpleStringProperty lastName;
- private final SimpleStringProperty email;
- private final SimpleStringProperty likes;
- private Person(String fName, String lName, String email, String likes) {
- this.firstName = new SimpleStringProperty(fName);
- this.lastName = new SimpleStringProperty(lName);
- this.email = new SimpleStringProperty(email);
- this.likes = new SimpleStringProperty(likes);
- }
- public String getFirstName() {
- return firstName.get();
- }
- public void setFirstName(String fName) {
- firstName.set(fName);
- }
- public String getLastName() {
- return lastName.get();
- }
- public void setLastName(String fName) {
- lastName.set(fName);
- }
- public String getEmail() {
- return email.get();
- }
- public void setEmail(String fName) {
- email.set(fName);
- }
- public String getLikes() {
- return likes.get();
- }
- public void setLikes(String likes) {
- this.likes.set(likes);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement