Advertisement
Guest User

Untitled

a guest
Mar 28th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. package Core;
  2.  
  3. import javafx.application.Application;
  4. import javafx.beans.property.SimpleStringProperty;
  5. import javafx.collections.FXCollections;
  6. import javafx.collections.ObservableList;
  7. import javafx.scene.Group;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.control.TableColumn;
  11. import javafx.scene.control.TableView;
  12. import javafx.scene.control.cell.PropertyValueFactory;
  13. import javafx.scene.layout.VBox;
  14. import javafx.scene.text.Font;
  15. import javafx.stage.Stage;
  16.  
  17. import java.awt.*;
  18.  
  19. public class Main2TestEnvironment extends Application {
  20.  
  21. private final TableView<Person> table = new TableView<>();
  22. private final ObservableList<Person> data =
  23. FXCollections.observableArrayList(
  24. new Person("Jacob", "Smith", "jacob.smith@example.com"),
  25. new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
  26. new Person("Ethan", "Williams", "ethan.williams@example.com"),
  27. new Person("Emma", "Jones", "emma.jones@example.com"),
  28. new Person("Michael", "Brown", "michael.brown@example.com")
  29. );
  30.  
  31. public static void Main2TestEnvironment(String[] args) {
  32. launch(args);
  33. }
  34.  
  35. @Override
  36. public void start(Stage stage) {
  37. Scene scene = new Scene(new Group());
  38. stage.setTitle("Table View Sample");
  39. stage.setWidth(450);
  40. stage.setHeight(500);
  41.  
  42. final Label label = new Label("Address Book");
  43. label.setFont(new Font("Arial", 20));
  44.  
  45. table.setEditable(true);
  46.  
  47. TableColumn firstNameCol = new TableColumn("First Name");
  48. firstNameCol.setMinWidth(100);
  49. firstNameCol.setCellValueFactory(
  50. new PropertyValueFactory<>("firstName"));
  51.  
  52. TableColumn lastNameCol = new TableColumn("Last Name");
  53. lastNameCol.setMinWidth(100);
  54. lastNameCol.setCellValueFactory(
  55. new PropertyValueFactory<>("lastName"));
  56.  
  57. TableColumn emailCol = new TableColumn("Email");
  58. emailCol.setMinWidth(200);
  59. emailCol.setCellValueFactory(
  60. new PropertyValueFactory<>("email"));
  61. //will work as expected
  62. //both Person() and Person(var, var,var) are defined functions of the class Person
  63. Person testP = new Person("Name", "Nachname", "mail@mail.com");
  64. testP.setEmail("mail2@mail.com");
  65. data.add(testP);
  66.  
  67. //will fail horribly
  68. Person testP2 = new Person();
  69. testP2.setEmail("mail2@mail.com");
  70. testP2.setFirstName("Michael");
  71. testP2.setLastName("Sloawiks");
  72. data.add(testP2);
  73.  
  74. table.setItems(data);
  75. table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
  76.  
  77. final VBox vbox = new VBox();
  78. vbox.setSpacing(5);
  79. //vbox.setPadding(new Insets(10, 0, 0, 10));
  80. vbox.getChildren().addAll(label, table);
  81.  
  82. ((Group) scene.getRoot()).getChildren().addAll(vbox);
  83.  
  84. stage.setScene(scene);
  85. stage.show();
  86. }
  87.  
  88. public static class Person {
  89.  
  90. private SimpleStringProperty firstName;
  91. private SimpleStringProperty lastName;
  92. private SimpleStringProperty email;
  93.  
  94. private Person(String fName, String lName, String email) {
  95. this.firstName = new SimpleStringProperty(fName);
  96. this.lastName = new SimpleStringProperty(lName);
  97. this.email = new SimpleStringProperty(email);
  98. }
  99.  
  100. private Person()
  101. {
  102.  
  103. }
  104.  
  105. public String getFirstName() {
  106. return firstName.get();
  107. }
  108.  
  109. public void setFirstName(String fName) {
  110. firstName.set(fName);
  111. }
  112.  
  113. public String getLastName() {
  114. return lastName.get();
  115. }
  116.  
  117. public void setLastName(String fName) {
  118. lastName.set(fName);
  119. }
  120.  
  121. public String getEmail() {
  122. return email.get();
  123. }
  124.  
  125. public void setEmail(String fName) {
  126. email.set(fName);
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement