1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import javafx.application.Application; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.MapValueFactory; import javafx.scene.layout.VBox; import javafx.scene.text.Font; import javafx.stage.Stage;
import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Map;
public class TreeTableView extends Application { private TableView<Map<String, Object>> table = new TableView<>();
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Table View Sample"); stage.setWidth(450); stage.setHeight(500);
final Label label = new Label("Address Book"); label.setFont(new Font("Arial", 20));
constructTable(); final VBox vbox = new VBox(); vbox.setSpacing(5); vbox.setPadding(new Insets(10, 0, 0, 10)); vbox.getChildren().addAll(label, table);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene); stage.show(); }
private void constructTable() { TableColumn<Map<String, Object>, String> firstNameCol = new TableColumn<Map<String, Object>, String>("First Name"); firstNameCol.setMinWidth(100); firstNameCol.setCellValueFactory(new MapValueFactory("firstName"));
TableColumn<Map<String, Object>, String> lastNameCol = new TableColumn<Map<String, Object>, String>("Last Name"); lastNameCol.setMinWidth(100); lastNameCol.setCellValueFactory(new MapValueFactory("lastName"));
TableColumn<Map<String, Object>, String> emailCol = new TableColumn<Map<String, Object>, String>("Email"); emailCol.setMinWidth(200); emailCol.setCellValueFactory(new MapValueFactory("email"));
table.setItems(getData()); table.getColumns().addAll(firstNameCol, lastNameCol, emailCol); }
private ObservableList<Map<String, Object>> getData() { List<Person> list = new LinkedList<>(Arrays.asList(new Person("Jacob", "Smith", "jacob.smith@example.com","Jacob Street","NY"), new Person("Isabella", "Johnson", "isabella.johnson@example.com","Isabella Street","DL"), new Person("Ethan", "Williams", "ethan.williams@example.com","Ethan Street"," ML"), new Person("Emma", "Jones", "emma.jones@example.com","Emma Street","EL"), new Person("Michael", "Brown", "michael.brown@example.com","Michael Street","ML"))); ObservableList<Map<String, Object>> observableList = FXCollections.observableArrayList(); for (Person person : list) { observableList.add(JSONObject.parseObject(JSON.toJSONString(person))); } return observableList; }
public static class Person { private final SimpleStringProperty firstName; private final SimpleStringProperty lastName; private final SimpleStringProperty email;
Person(String fName, String lName, String email,String streetS, String cityS) { this.firstName = new SimpleStringProperty(fName); this.lastName = new SimpleStringProperty(lName); this.email = new SimpleStringProperty(email); }
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); } } }
|