Delete Button Cell
Delete Button Cell
Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import com.sun.prism.impl.Disposer.Record;
/**
* Author : Abhinay_Agarwal
*/
public class TableViewDeleteSample extends Application {
@SuppressWarnings("unchecked")
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Table With Delete Row");
tableView.setEditable(false);
firstName.setCellValueFactory(new PropertyValueFactory<Person,
String>("firstName"));
lastName.setCellValueFactory(new PropertyValueFactory<Person,
String>("lastName"));
email.setCellValueFactory(new PropertyValueFactory<Person,
String>("email"));
tableView.getColumns().add(firstName);
tableView.getColumns().add(lastName);
tableView.getColumns().add(email);
//Insert Button
TableColumn col_action = new TableColumn<>("Action");
tableView.getColumns().add(col_action);
col_action.setCellValueFactory(
new Callback<TableColumn.CellDataFeatures<Record, Boolean>,
ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean>
call(TableColumn.CellDataFeatures<Record, Boolean> p) {
return new SimpleBooleanProperty(p.getValue() != null);
}
});
});
tableView.setItems(data);
ButtonCell(){
@Override
public void handle(ActionEvent t) {
// get Selected Item
Person currentPerson = (Person)
ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());
//remove selected item from the table list
data.remove(currentPerson);
}
});
}