执行结果:包含列但有0行数据的表(必须为1)。
基本控制器代码:
@FXML
private TableView<Dato> tabla;
@FXML
private TableColumn<Dato, String> colNumero;
@FXML
private TableColumn<Dato, String> colEntidad;
@FXML
private TableColumn<Dato, String> colUsuario;
@FXML
private TableColumn<Dato, String> colPassword;
@FXML
private TableColumn<Dato, String> colComentarios;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
nuevaEntrada = false;
colNumero.setCellValueFactory(new PropertyValueFactory<Dato, String>(
"Num"));
colEntidad.setCellValueFactory(new PropertyValueFactory<Dato, String>(
"Entidad"));
colUsuario.setCellValueFactory(new PropertyValueFactory<Dato, String>(
"Usuario"));
colPassword.setCellValueFactory(new PropertyValueFactory<Dato, String>(
"Password"));
colComentarios
.setCellValueFactory(new PropertyValueFactory<Dato, String>(
"Comentarios"));
buildData();
}
private ObservableList<Dato> data;
public void buildData() {
data = FXCollections.observableArrayList();
Dato dato = new Dato(1, "First", "First", "First", "First");
data.add(dato);
System.out.println(dato.toString());
tabla.setItems(data);
}
拿督的课程:
package application;
import javafx.beans.property.SimpleStringProperty;
public class Dato {
private SimpleStringProperty num;
private SimpleStringProperty entidad;
private SimpleStringProperty usuario;
private SimpleStringProperty password;
private SimpleStringProperty comentarios;
public Dato(int num, String entidad, String usuario, String password,
String comentarios) {
this.num = new SimpleStringProperty(Integer.toString(num));
this.entidad = new SimpleStringProperty(entidad);
this.usuario = new SimpleStringProperty(usuario);
this.password = new SimpleStringProperty(password);
this.comentarios = new SimpleStringProperty(comentarios);
}
public Dato(int num) {
this.num = new SimpleStringProperty(Integer.toString(num));
}
protected SimpleStringProperty getNum() {
return num;
}
protected SimpleStringProperty getEntidad() {
return entidad;
}
protected SimpleStringProperty getUsuario() {
return usuario;
}
protected SimpleStringProperty getPassword() {
return password;
}
protected SimpleStringProperty getComentarios() {
return comentarios;
}
}