Tema relacionado: Agregar y eliminar filas en TableView
En una entrada anterior (esta) se mostró como crear una tabla con JavaFX. En este caso se hará un ejemplo agregando un botón en las celdas de la tabla. La tabla quedará como lo muestra la siguiente imagen:
En una entrada anterior (esta) se mostró como crear una tabla con JavaFX. En este caso se hará un ejemplo agregando un botón en las celdas de la tabla. La tabla quedará como lo muestra la siguiente imagen:
En las tablas de JavaFX existe la opción de agregar celdas personalizadas, como se puede ver en la imagen existe un botón dentro de la celda. Otro punto importante es el manejo del contenido de la tabla por medio de una lista observable que realiza los cambios en la tabla cuando la lista cambia. El código de la tabla anterior es el siguiente:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.beans.property.SimpleStringProperty; | |
import javafx.collections.FXCollections; | |
import javafx.collections.ObservableList; | |
import javafx.event.Event; | |
import javafx.event.EventHandler; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.TableColumn; | |
import javafx.scene.control.TableView; | |
import javafx.scene.control.cell.PropertyValueFactory; | |
import javafx.stage.Stage; | |
public class TableViewSample extends Application { | |
private TableView table = new TableView(); | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage stage) throws Exception { | |
Group group = new Group(); | |
Scene scene = new Scene(group); | |
stage.setTitle("Table View Sample"); | |
stage.setWidth(400); | |
stage.setHeight(500); | |
ObservableList<Person> data = FXCollections.observableArrayList( | |
new Person("Paco", "Perez", "12345678",new Button("Info")), | |
new Person("Isabella", "Garcia","12345678", new Button("Info")), | |
new Person("Juan", "Juanes", "12345678",new Button("Info")), | |
new Person("German", "Jones","12345678", new Button("Info")), | |
new Person("Erick", "Mancha", "12345678",new Button("Info"))); | |
table.setEditable(true); | |
table.setMinWidth(390); | |
TableColumn firstNameCol = new TableColumn("Primer Nombre"); | |
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName")); | |
firstNameCol.setMinWidth(100); | |
TableColumn lastNameCol = new TableColumn("Segundo Nombre"); | |
lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName")); | |
lastNameCol.setMinWidth(100); | |
TableColumn phoneCol = new TableColumn("Teléfono"); | |
phoneCol.setCellValueFactory(new PropertyValueFactory<Person, String>("phone")); | |
phoneCol.setMinWidth(100); | |
TableColumn infoCol = new TableColumn("Información"); | |
infoCol.setCellValueFactory(new PropertyValueFactory<Person, String>("button")); | |
infoCol.setMinWidth(80); | |
table.setItems(data); | |
table.getColumns().addAll(firstNameCol, lastNameCol, phoneCol, infoCol); | |
group.getChildren().add(table); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
} |
Para que la tabla funcione con el patrón observador es necesario que cree un POJO (Plain Old Java Object) que represente a la tabla y se haga una lista observable con estos. También se le debe indicar a las columnas de la tabla que elemento del POJO representan. Dentro del POJO se le dan atributos del tipo propiedad y en el caso del botón se le agrega el método que desee que ejecute. El código del POJO es el siguiente:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Person { | |
private final SimpleStringProperty firstName; | |
private final SimpleStringProperty lastName; | |
private final SimpleStringProperty phone; | |
private final Button button; | |
private Person(String first, String last, String phon, Button button) { | |
this.firstName = new SimpleStringProperty(first); | |
this.lastName = new SimpleStringProperty(last); | |
this.phone = new SimpleStringProperty(phon); | |
this.button = button; | |
button.setOnMouseReleased(new EventHandler() { | |
@Override | |
public void handle(Event arg0) { | |
System.out.println(firstName + "," + lastName + "," + phone); | |
} | |
}); | |
} | |
public String getFirstName() { | |
return firstName.get(); | |
} | |
public void setFirstName(String first) { | |
firstName.set(first); | |
} | |
public String getLastName() { | |
return lastName.get(); | |
} | |
public void setLastName(String last) { | |
lastName.set(last); | |
} | |
public String getPhone() { | |
return phone.get(); | |
} | |
public void setEmail(String phone) { | |
this.phone.set(phone); | |
} | |
public Button getButton() { | |
return button; | |
} | |
} |
Muchas gracias por tu aporte esta solución resulto ser mas simple y entendible que otras que busque en ingles :)
ResponderEliminarBuenas como seria para añadir checkbox detro de una grilla en javafx pero ya registros de la base de datos. Desde ya gracias.
ResponderEliminarComo seria para agregar 3 botones en esa columna que se pongan de manera horizontal.
ResponderEliminarMuy entendible.
ResponderEliminar