Advertisement
Guest User

Untitled

a guest
May 27th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.39 KB | None | 0 0
  1.  
  2. import java.util.function.Function;
  3.  
  4. import javafx.application.Application;
  5. import javafx.beans.binding.Bindings;
  6. import javafx.beans.property.SimpleStringProperty;
  7. import javafx.beans.property.StringProperty;
  8. import javafx.beans.value.ChangeListener;
  9. import javafx.beans.value.ObservableValue;
  10. import javafx.geometry.Pos;
  11. import javafx.scene.Scene;
  12. import javafx.scene.control.TableCell;
  13. import javafx.scene.control.TableColumn;
  14. import javafx.scene.control.TableView;
  15. import javafx.scene.control.cell.TextFieldTableCell;
  16. import javafx.scene.layout.VBox;
  17. import javafx.stage.Stage;
  18. import javafx.util.Callback;
  19.  
  20. public class TableViewSingleClickEditTest extends Application {
  21.  
  22.  
  23. @Override
  24. public void start(Stage primaryStage) {
  25.  
  26.  
  27. TableView<Person> table = new TableView<>();
  28. table.setEditable(true);
  29.  
  30. TableColumn<Person, String> firstNameCol = createCol("First Name", Person::firstNameProperty, 150);
  31. TableColumn<Person, String> lastNameCol = createCol("Last Name", Person::lastNameProperty, 150);
  32. TableColumn<Person, String> emailCol = createCol("Email", Person::emailProperty, 200);
  33.  
  34. TableColumn<Person, Void> indexCol = new TableColumn<>("");
  35. indexCol.setCellFactory(col -> {
  36. TableCell<Person, Void> cell = new TableCell<>();
  37. cell.textProperty().bind(Bindings.createStringBinding(() -> {
  38. if (cell.getIndex() >= 0 && cell.getIndex() < table.getItems().size() - 1) {
  39. return Integer.toString(cell.getIndex() + 1);
  40. } else if (cell.getIndex() == table.getItems().size() - 1) {
  41. return "*" ;
  42. } else return "" ;
  43. }, cell.indexProperty(), table.getItems()));
  44. return cell ;
  45. });
  46. indexCol.setPrefWidth(32);
  47.  
  48. table.getItems().addAll(
  49. new Person("Jacob", "Smith", "jacob.smith@example.com"),
  50. new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
  51. new Person("Ethan", "Williams", "ethan.williams@example.com"),
  52. new Person("Emma", "Jones", "emma.jones@example.com"),
  53. new Person("Michael", "Brown", "michael.brown@example.com")
  54. );
  55.  
  56. ChangeListener<String> lastPersonTextListener = new ChangeListener<String>() {
  57. @Override
  58. public void changed(ObservableValue<? extends String> obs, String oldText, String newText) {
  59. if (oldText.isEmpty() && ! newText.isEmpty()) {
  60. Person lastPerson = table.getItems().get(table.getItems().size() - 1);
  61. lastPerson.firstNameProperty().removeListener(this);
  62. lastPerson.lastNameProperty().removeListener(this);
  63. lastPerson.emailProperty().removeListener(this);
  64. Person newBlankPerson = new Person("", "", "");
  65. newBlankPerson.firstNameProperty().addListener(this);
  66. newBlankPerson.lastNameProperty().addListener(this);
  67. newBlankPerson.emailProperty().addListener(this);
  68. table.getItems().add(newBlankPerson);
  69. }
  70. }
  71. };
  72.  
  73. Person blankPerson = new Person("", "", "");
  74. blankPerson.firstNameProperty().addListener(lastPersonTextListener);
  75. blankPerson.lastNameProperty().addListener(lastPersonTextListener);
  76. blankPerson.emailProperty().addListener(lastPersonTextListener);
  77. table.getItems().add(blankPerson);
  78.  
  79. table.getColumns().add(indexCol);
  80. table.getColumns().add(firstNameCol);
  81. table.getColumns().add(lastNameCol);
  82. table.getColumns().add(emailCol);
  83.  
  84. VBox root = new VBox(15, table);
  85. root.setAlignment(Pos.CENTER);
  86. Scene scene = new Scene(root, 800, 600);
  87.  
  88. primaryStage.setScene(scene);
  89. primaryStage.show();
  90. }
  91.  
  92.  
  93. private TableColumn<Person, String> createCol(String title,
  94. Function<Person, ObservableValue<String>> mapper, double size) {
  95.  
  96. TableColumn<Person, String> col = new TableColumn<>(title);
  97.  
  98. col.setCellValueFactory(cellData -> mapper.apply(cellData.getValue()));
  99.  
  100. Callback<TableColumn<Person, String>, TableCell<Person, String>> defaultCellFactory
  101. = TextFieldTableCell.forTableColumn();
  102.  
  103. col.setCellFactory(column -> {
  104. TableCell<Person, String> cell = defaultCellFactory.call(column);
  105. cell.setOnMouseClicked(e -> {
  106. if (! cell.isEditing() && ! cell.isEmpty()) {
  107. cell.getTableView().edit(cell.getIndex(), column);
  108. }
  109. });
  110. return cell ;
  111. });
  112.  
  113. col.setPrefWidth(size);
  114.  
  115.  
  116. return col ;
  117. }
  118.  
  119. public class Person {
  120. private final StringProperty firstName = new SimpleStringProperty(this, "firstName");
  121. private final StringProperty lastName = new SimpleStringProperty(this, "lastName");
  122. private final StringProperty email = new SimpleStringProperty(this, "email");
  123.  
  124. public Person(String firstName, String lastName, String email) {
  125. this.firstName.set(firstName);
  126. this.lastName.set(lastName);
  127. this.email.set(email);
  128. }
  129.  
  130. public final StringProperty firstNameProperty() {
  131. return this.firstName;
  132. }
  133.  
  134. public final java.lang.String getFirstName() {
  135. return this.firstNameProperty().get();
  136. }
  137.  
  138. public final void setFirstName(final java.lang.String firstName) {
  139. this.firstNameProperty().set(firstName);
  140. }
  141.  
  142. public final StringProperty lastNameProperty() {
  143. return this.lastName;
  144. }
  145.  
  146. public final java.lang.String getLastName() {
  147. return this.lastNameProperty().get();
  148. }
  149.  
  150. public final void setLastName(final java.lang.String lastName) {
  151. this.lastNameProperty().set(lastName);
  152. }
  153.  
  154. public final StringProperty emailProperty() {
  155. return this.email;
  156. }
  157.  
  158. public final java.lang.String getEmail() {
  159. return this.emailProperty().get();
  160. }
  161.  
  162. public final void setEmail(final java.lang.String email) {
  163. this.emailProperty().set(email);
  164. }
  165.  
  166. }
  167.  
  168. public static void main(String[] args) {
  169. launch(args);
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement