Tableview does not update when adding new values

Asked

Viewed 124 times

1

Good morning, I’m starting in this world of java and I’m having a difficulty that I don’t have much idea how to solve. I am creating a register of equipment with Javafx, add and search values correctly but if I just added a value I can not search it. So how could I update my tableview to receive this value and later search it?

public class FXMLDocumentController implements Initializable{

@FXML
private TableColumn<Equipamento, Integer> tbCodigo;

@FXML
private TableColumn<Equipamento, String> tbDescricao;

@FXML
private TableColumn<Equipamento, String> tbStatus;

@FXML
public TableView<Equipamento> tabela;

@FXML
private TextField txNumPatrimonio;

@FXML
private TextField txDescricao;

@FXML
private TextField txStatus;

@FXML
private Label labelNumPatr;

@FXML
private Label labelDescr;

@FXML
private Label labelStatus;

@FXML
private AnchorPane pane2;

@FXML
private TextField txNumPatrConsulta;

@FXML
private TableView<Equipamento> tabelaConsulta;

@FXML
private TableColumn<Equipamento, Integer> tbNumPatrConsulta;

@FXML
private TableColumn<Equipamento, String> tbDescConsulta;

@FXML
private TableColumn<Equipamento, String> tbStatusConsulta;

@Override
public void initialize(URL location, ResourceBundle resources) {           
    carregaTabela();                

    tabela.getSelectionModel().selectedItemProperty().addListener(
            (observable, oldValue, newValue) -> selecionarItemTableViewEquip(newValue));

    System.out.println(" " + listaEquipamentos().get(0).getDescricao());
    System.out.println(" " + listaEquipamentos().get(1).getDescricao());
    System.out.println(" " + listaEquipamentos().get(2).getDescricao());
    System.out.println(" " + listaEquipamentos().get(3).getDescricao());       
}

//adiciona um equipamento;
@FXML
protected void addEquip(ActionEvent event) {
    Integer cod;
    String desc, stat;

    cod = Integer.parseInt(txNumPatrimonio.getText());
    desc = txDescricao.getText();
    stat = txStatus.getText();

    Equipamento e = new Equipamento(cod, desc, stat);
    listaEquipamentos().add(e);
    tabela.getItems().add(e);

    txNumPatrimonio.clear();
    txDescricao.clear();
    txStatus.clear();           
}

@FXML
public void carregaTabela(){    
    tbCodigo.setCellValueFactory(new PropertyValueFactory<>("codigo"));
    tbDescricao.setCellValueFactory(new PropertyValueFactory<>("descricao"));
    tbStatus.setCellValueFactory(new PropertyValueFactory<>("status"));

    tabela.setItems(listaEquipamentos());
}

//observableList com alguns dados
ObservableList<Equipamento> listaEquipamentos(){
    return FXCollections.observableArrayList(
            new Equipamento(12, "impressora a laser", "Sala 1"),
            new Equipamento(21, "notebook", "Secretaria"),
            new Equipamento(25, "fax", "Cozinha"),
            new Equipamento(31, "cafeteira", "Sala 1")
    );
}    

@FXML
protected void consultarEquip(ActionEvent event){
    ObservableList<Equipamento> listEquips = FXCollections.observableArrayList();

    Integer cod = Integer.parseInt(txNumPatrConsulta.getText());
    System.out.println(txNumPatrConsulta.getText());

    pane2.setVisible(true);

    listaEquipamentos().forEach((e) -> {
        if(Objects.equals(cod, e.getCodigo())){
            System.out.println(e.getDescricao());
            listEquips.add(e);

        }                       
    });

    tbNumPatrConsulta.setCellValueFactory(new PropertyValueFactory<>("codigo"));
    tbDescConsulta.setCellValueFactory(new PropertyValueFactory<>("descricao"));
    tbStatusConsulta.setCellValueFactory(new PropertyValueFactory<>("status"));

    tabelaConsulta.setItems(listEquips);
}

1 answer

0

You should keep a reference to the Observablelist you have placed as a set of items in your Tableview. Hence when adding do not:

tabela.getItems().add(e);

All CRUD should be done in the previously defined Observablelist:

private ObservableList<Equipamento> listaEquipamentos;
// [...]

protected void addEquip(ActionEvent event) {
    // [...]
    listaEquipamentos.add(new Equipamento(cod, desc, stat));
    // [...]           
}

public void carregaTabela(){    
    // [...]
    tabela.setItems(listaEquipamentos());
}

public void listarEquipamentos(){
    listaEquipamentos = FXCollections.observableArrayList(
        new Equipamento(12, "impressora a laser", "Sala 1"),
        new Equipamento(21, "notebook", "Secretaria"),
        new Equipamento(25, "fax", "Cozinha"),
        new Equipamento(31, "cafeteira", "Sala 1")
    );
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.