I want my tableView to show in their respective columns the Key that corresponds to a bank account number and the Value to the balance in it, as in the image:
I don't know if maybe I'm making some misuse of the tableView being an observable List since I tried it but when creating the account it only shows me the value in the 2 column and not the Key that corresponds to the Account #:
The code of my controller class with which I refresh the table in each account creation is:
private void refrescarCuentas() {
ListaCuentas.getItems().clear();
Iterator lisCuent = modelFactoryController.getBanco().getListaCuentasBanco().entrySet().iterator();
for (String key : modelFactoryController.getBanco().getListaCuentasBanco().keySet()) {
for (Double value : modelFactoryController.getBanco().getListaCuentasBanco().values()) {
listaCuentas.add(new Cuenta(key, value));
ListaCuentas.setItems(listaCuentas);
}
}
}
I have already debugged the method and it should work since in the message that the JOptionPane shows me it tells me that if I capture the "Key"... I appreciate the help.
Labels and initializable in case the error is found there:
@FXML
private TableView<Cuenta> ListaCuentas;
@FXML
private TableColumn<Cuenta, String> nunCuenta;
@FXML
private TableColumn<Cuenta, Double> saldo;
@FXML
private ObservableList<Cuenta> listaCuentas = FXCollections.observableArrayList();
@FXML
private void initialize() {
/*
* @Author Santiago Poveda Garcia Luis Felipe Toro
*
* @Date: 19/03/2021
*/
nunCuenta.setCellValueFactory(new PropertyValueFactory<>("#Cuenta"));
saldo.setCellValueFactory(new PropertyValueFactory<>("Saldo"));
ListaCuentas.setItems(listaCuentas);
refrescarCuentas();
}
public void crearCuenta() {
listaCuentas.clear();
String clienteSelecCuen = cbClienteCuenta.getValue();
Double saldoInicial = Double.parseDouble(tfSaldoInicial.getText());
int tipoCuenta = 5;
int bTipo = cbTipoCuenta.getSelectionModel().getSelectedIndex();
String numeroCuenta = "";
if (bTipo == 0) {
tipoCuenta = 1;
numeroCuenta += "A_H";
}
if (bTipo == 1) {
tipoCuenta = 2;
numeroCuenta += "C_O";
}
Iterator listaClient = modelFactoryController.getBanco().getListaClientes().iterator();
while (listaClient.hasNext()) {
Cliente client = (Cliente) listaClient.next();
if (client.toString().equals(clienteSelecCuen)) {
numeroCuenta += client.getCedula();
client.crearCuenta(numeroCuenta, saldoInicial, tipoCuenta);
modelFactoryController.getBanco().crearCuenta(numeroCuenta, saldoInicial, tipoCuenta);
JOptionPane.showMessageDialog(null,
"Cuenta de cliente creada y agregada al banco, numero cuenta:" + numeroCuenta);
refrescarCuentas();
}
}
}
The
String
one that is passed as a parameter to the constructor of the classPropertyValueFactory
is the name of the property within the class of the elements that you add to the table (in your caseCuenta
).This means that JavaFX will search for the getter corresponding to the property specified in the
PropertyValueFactory
: for a propertycuenta
it will be searched for by thegetCuenta()
.Without seeing the class code
Cuenta
you should do something like this: