TableView does not update when I add new values

1

Good morning, I'm starting this java world and I'm having a hard time that I do not have much idea how to solve. I'm creating a JavaFX appliance registry, adding and searching values correctly but if I just added a value I can not search it. So how could I update my tableview to get this value and then 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);
}
    
asked by anonymous 28.02.2018 / 14:36

1 answer

0

You should save a reference to the ObservableList that you have placed as a set of items in your TableView. Then when it's added 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")
    );
}
    
28.02.2018 / 18:55