Advertisement
Guest User

Untitled

a guest
Dec 4th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.beans.property.SimpleBooleanProperty;
  3. import javafx.beans.property.SimpleStringProperty;
  4. import javafx.beans.value.ObservableValue;
  5. import javafx.collections.FXCollections;
  6. import javafx.collections.ObservableList;
  7. import javafx.event.ActionEvent;
  8. import javafx.event.EventHandler;
  9. import javafx.scene.Scene;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.control.TableCell;
  12. import javafx.scene.control.TableColumn;
  13. import javafx.scene.control.TableView;
  14. import javafx.scene.control.cell.PropertyValueFactory;
  15. import javafx.scene.layout.VBox;
  16. import javafx.stage.Stage;
  17. import javafx.util.Callback;
  18.  
  19. import com.sun.prism.impl.Disposer.Record;
  20.  
  21. /**
  22. * Author : Abhinay_Agarwal
  23. */
  24. public class TableViewDeleteSample extends Application {
  25.  
  26. private TableView<Person> tableView = new TableView<Person>();
  27.  
  28.  
  29. private final ObservableList<Person> data =
  30. FXCollections.observableArrayList(
  31. new Person("Eliza", "Smith", "eliza.smith@javafxpro.com"),
  32. new Person("Isabella", "Johnson", "isabella.johnson@javafxpro.com"),
  33. new Person("Imran", "Williams", "imran.williams@javafxpro.com"),
  34. new Person("Emma", "Jones", "emma.jones@javafxpro.com"),
  35. new Person("Russel", "Peters", "russel.peters@javafxpro.com"));
  36.  
  37.  
  38. public static class Person {
  39.  
  40. private final SimpleStringProperty firstName;
  41. private final SimpleStringProperty lastName;
  42. private final SimpleStringProperty email;
  43.  
  44. private Person(String fName, String lName, String email) {
  45. this.firstName = new SimpleStringProperty(fName);
  46. this.lastName = new SimpleStringProperty(lName);
  47. this.email = new SimpleStringProperty(email);
  48. }
  49.  
  50. public String getFirstName() {
  51. return firstName.get();
  52. }
  53.  
  54. public void setFirstName(String fName) {
  55. firstName.set(fName);
  56. }
  57.  
  58. public String getLastName() {
  59. return lastName.get();
  60. }
  61.  
  62. public void setLastName(String fName) {
  63. lastName.set(fName);
  64. }
  65.  
  66. public String getEmail() {
  67. return email.get();
  68. }
  69.  
  70. public void setEmail(String fName) {
  71. email.set(fName);
  72. }
  73. }
  74.  
  75. @SuppressWarnings("unchecked")
  76. @Override
  77. public void start(Stage primaryStage) {
  78. primaryStage.setTitle("Table With Delete Row");
  79. tableView.setEditable(false);
  80.  
  81. TableColumn<Person, String> firstName = new TableColumn<Person, String>("First Name");
  82. TableColumn<Person, String> lastName = new TableColumn<Person, String>("Last Name");
  83. TableColumn<Person, String> email = new TableColumn<Person, String>("Email");
  84.  
  85. firstName.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
  86. lastName.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
  87. email.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));
  88.  
  89. tableView.getColumns().add(firstName);
  90. tableView.getColumns().add(lastName);
  91. tableView.getColumns().add(email);
  92.  
  93. //Insert Button
  94. TableColumn col_action = new TableColumn<>("Action");
  95. tableView.getColumns().add(col_action);
  96.  
  97. col_action.setCellValueFactory(
  98. new Callback<TableColumn.CellDataFeatures<Record, Boolean>,
  99. ObservableValue<Boolean>>() {
  100.  
  101. @Override
  102. public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Record, Boolean> p) {
  103. return new SimpleBooleanProperty(p.getValue() != null);
  104. }
  105. });
  106.  
  107. //Adding the Button to the cell
  108. col_action.setCellFactory(
  109. new Callback<TableColumn<Record, Boolean>, TableCell<Record, Boolean>>() {
  110.  
  111. @Override
  112. public TableCell<Record, Boolean> call(TableColumn<Record, Boolean> p) {
  113. return new ButtonCell();
  114. }
  115.  
  116. });
  117.  
  118.  
  119. tableView.setItems(data);
  120.  
  121. VBox vBox = new VBox();
  122. vBox.getChildren().addAll(tableView);
  123. primaryStage.setScene(new Scene(vBox, 440, 400));
  124. primaryStage.show();
  125. }
  126.  
  127. public static void main(String[] args) {
  128. launch(args);
  129. }
  130.  
  131. //Define the button cell
  132. private class ButtonCell extends TableCell<Record, Boolean> {
  133. final Button cellButton = new Button("Delete");
  134.  
  135. ButtonCell(){
  136.  
  137. //Action when the button is pressed
  138. cellButton.setOnAction(new EventHandler<ActionEvent>(){
  139.  
  140. @Override
  141. public void handle(ActionEvent t) {
  142. // get Selected Item
  143. Person currentPerson = (Person) ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());
  144. //remove selected item from the table list
  145. data.remove(currentPerson);
  146. }
  147. });
  148. }
  149.  
  150. //Display button if the row is not empty
  151. @Override
  152. protected void updateItem(Boolean t, boolean empty) {
  153. super.updateItem(t, empty);
  154. if(!empty){
  155. setGraphic(cellButton);
  156. }else{
  157. setGraphic(null);
  158. }
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement