0% found this document useful (0 votes)
269 views

Delete Button Cell

This document defines a JavaFX application that displays a table with person data. It allows deleting rows by adding a delete button column to the table. When a button is clicked, it removes the corresponding person object from the underlying list. The Person class defines properties for first name, last name and email. The main class extends Application and initializes the table, adding columns for each person property. It sets the table items to an observable list of sample person data and displays the table in a scene.

Uploaded by

bato cera
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
269 views

Delete Button Cell

This document defines a JavaFX application that displays a table with person data. It allows deleting rows by adding a delete button column to the table. When a button is clicked, it removes the corresponding person object from the underlying list. The Person class defines properties for first name, last name and email. The main class extends Application and initializes the table, adding columns for each person property. It sets the table items to an observable list of sample person data and displays the table in a scene.

Uploaded by

bato cera
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import javafx.application.

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 {

private TableView<Person> tableView = new TableView<Person>();

private final ObservableList<Person> data =


FXCollections.observableArrayList(
new Person("Eliza", "Smith", "[email protected]"),
new Person("Isabella", "Johnson", "[email protected]"),
new Person("Imran", "Williams", "[email protected]"),
new Person("Emma", "Jones", "[email protected]"),
new Person("Russel", "Peters", "[email protected]"));

public static class Person {

private final SimpleStringProperty firstName;


private final SimpleStringProperty lastName;
private final SimpleStringProperty email;

private Person(String fName, String lName, String email) {


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);
}
}

@SuppressWarnings("unchecked")
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Table With Delete Row");
tableView.setEditable(false);

TableColumn<Person, String> firstName = new TableColumn<Person,


String>("First Name");
TableColumn<Person, String> lastName = new TableColumn<Person,
String>("Last Name");
TableColumn<Person, String> email = new TableColumn<Person,
String>("Email");

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);
}
});

//Adding the Button to the cell


col_action.setCellFactory(
new Callback<TableColumn<Record, Boolean>, TableCell<Record,
Boolean>>() {
@Override
public TableCell<Record, Boolean> call(TableColumn<Record, Boolean> p)
{
return new ButtonCell();
}

});

tableView.setItems(data);

VBox vBox = new VBox();


vBox.getChildren().addAll(tableView);
primaryStage.setScene(new Scene(vBox, 440, 400));
primaryStage.show();
}

public static void main(String[] args) {


launch(args);
}

//Define the button cell


private class ButtonCell extends TableCell<Record, Boolean> {
final Button cellButton = new Button("Delete");

ButtonCell(){

//Action when the button is pressed


cellButton.setOnAction(new EventHandler<ActionEvent>(){

@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);
}
});
}

//Display button if the row is not empty


@Override
protected void updateItem(Boolean t, boolean empty) {
super.updateItem(t, empty);
if(!empty){
setGraphic(cellButton);
}
}
}
}

You might also like