如何用数据库数据填充TableView(来自NamedQuery的对象)

时间:2019-03-05 20:57:45

标签: arrays tableview

这是我的 TableView

 @FXML
    private TableView<Donante> tblDonantes;
    @FXML
    private TableColumn<Donante, String> colApellido = new TableColumn<>("Apellido");
    @FXML
    private TableColumn<Donante, String> colNombre = new TableColumn<>("Nombre");
    @FXML
    private TableColumn<Donante, Long> colCUILCUIT = new TableColumn<>("CUIL");
    @FXML
    private TableColumn<Donante, LocalDate> colFechaExtraccion = new TableColumn<>("Fecha Extracción");

我的

            tblDonantes.setPlaceholder(new Label("No se encontraron Donantes"));
            colApellido.setCellValueFactory(new PropertyValueFactory<Donante, String>("apellido"));
            colApellido.setMinWidth(250);
            colNombre.setCellValueFactory(new PropertyValueFactory<Donante, String>("nombre"));
            colNombre.setMinWidth(250);
            colCUILCUIT.setCellValueFactory(new PropertyValueFactory<Donante, Long>("cuil"));
            colCUILCUIT.setMinWidth(100);
            colFechaExtraccion.setCellValueFactory(new PropertyValueFactory<Donante, LocalDate>("fecha"));
            colFechaExtraccion.setMinWidth(150);t
tblDonantes.getColumns().addAll(colApellido, colNombre, colCUILCUIT, colFechaExtraccion);

此查询Query buscarDonantes = AreaPrincipal.em.createNamedQuery("Donante.buscarSinDonaciones");返回此图片Donors list 我需要用捐助者列表填充我的tabeview(tblDonantes)...我尝试这样做,但什么也没看到...

ObservableList<Donante> listaDonantes = FXCollections.observableArrayList(buscarDonantes.getResultList());
            tblDonantes.setItems(listaDonantes);

我也尝试过...

List<Object[]> resultList = (List<Object[]>) buscarDonantes.getResultList();    
for (int i = 0; i < resultList.size(); i++) {
      System.out.print(resultList.get(i)[0] + resultList.get(i)[1] + resultList.get(i)[2] + resultList.get(i)[3]);
            }

当然,我在控制台中看到数据...,但是我需要用我的tableView...。这是怎么回事? TIA ... Ernesto

1 个答案:

答案 0 :(得分:0)

回答我自己的问题(感谢Komposta.net的Chris): 第一:创建一个“通用”模型...

package ar.dignitas.balthus.model;

import java.util.ArrayList;
import java.util.Arrays;

public class Generica {
    private ArrayList<Object> data;
    public Generica(Object... args) {
        data = new ArrayList<>(Arrays.asList(args));
    }
    public Object get0() {
        return data.get(0);
    }
    public Object get1() {
        return data.get(1);
    }
    public Object get2() {
        return data.get(2);
    }
    public Object get3() {
        return data.get(3);
    }
    public Object get4() {
        return data.get(4);
    }
    public Object get5() {
        return data.get(5);
    }
    @Override
    public String toString() {
        return data.toString();
    }  
}

第二:TableView进行了一些修改

   @FXML
   private TableView<Generica> tblDonantes;
   @FXML
   private TableColumn<Generica, String> colApellido = new TableColumn<>("Apellido");
   @FXML
   private TableColumn<Generica, String> colNombre = new TableColumn<>("Nombre");
   @FXML
   private TableColumn<Generica, String> colCUILCUIT = new TableColumn<>("CUIL");
   @FXML
   private TableColumn<Generica, String> colFechaExtraccion = new TableColumn<>("Fecha 
   Extracción");

第三条:单元格列!

colCUILCUIT.setCellValueFactory(cellData -> new 
    SimpleStringProperty(cellData.getValue().get0().toString()));
colCUILCUIT.setMinWidth(100);
colApellido.setCellValueFactory(cellData -> new 
    SimpleStringProperty(cellData.getValue().get1().toString()));
colApellido.setMinWidth(250);
colNombre.setCellValueFactory(cellData -> new 
    SimpleStringProperty(cellData.getValue().get2().toString()));       
colNombre.setMinWidth(250);
colFechaExtraccion.setCellValueFactory(cellData -> new 
    SimpleStringProperty(cellData.getValue().get3().toString()));
colFechaExtraccion.setMinWidth(150);

tblDonantes.getColumns().addAll(colCUILCUIT, colApellido, colNombre, colFechaExtraccion);

第四...最后:

    btnBuscarTodos.setOnAction(event -> {
                limpiarColumnasTblDonantes();
            /*****************************************************************************
* The SELECT clause queries more than one column or entity, the 
* results are aggregated in an object array (Object[]) in the java.util.List 
* returned by getResultList( ).     
*****************************************************************************/
    Query buscarDonantes = AreaPrincipal.em.createNamedQuery("Donante.buscarSinDonaciones");
    List<Object[]> listaDonantes = FXCollections.observableArrayList(buscarDonantes.getResultList());
    ObservableList<Generica> data = FXCollections.observableArrayList();    
        for (Object[] filaRad : listaDonantes) { //for para recorrer las filas
                    data.add(new Generica(filaRad));
                }
                tblDonantes.setItems(data);     
            });

我不知道实现此目标的更好方法...目前,此代码有效。有更多的学习时间,我相信可以改善它。

@Ernesto