Load table view with Observablelist

Asked

Viewed 588 times

0

I’m trying to load a table view with the result of a query. The result of the query is sent to an Observablelist which is then called in the controller.

I do not get any error but it also does not show any result. Can anyone help me find the right path? Code on the controller

public class HomeController implements Initializable {   
@FXML
private TableView prova;

@FXML
private TableColumn colId;
@FXML
private TableColumn colTipo;
@FXML
private TableColumn colProva;
@FXML
private TableColumn colData;

/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {        
        ObservableList<Prova> list = FXCollections.observableArrayList(
                new Prova(1, "aaa", "aaa", "bbb"),
                new Prova(2, "bb", "bb", "bbb"),
                new Prova(3, "cc", "cc", "cc")
        );
        prova.getItems().setAll(list);
       // prova.getItems().addAll(list);  Tambem não Trabalha       
    try {
        OprationsOnDB.loadTableProva().forEach((tab) -> {
            //itemIdCol.setCellFactory(new PropertyValueFactory((String) tab));
            System.out.println("Stuff with " + tab.toString());
        });     
    } catch (SQLException ex) {
        Logger.getLogger(HomeController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

FXML code

<TableView fx:id="prova" GridPane.columnIndex="0" GridPane.rowIndex="1">
  <columns>
    <TableColumn fx:id="colId" text="#" prefWidth="100"/>
    <TableColumnfx:id="colLocal"text="Local" prefWidth="100"/>
    <TableColumn fx:id="colProva" text="Prova" prefWidth="200"/>
    <TableColumnfx:id="colData" text="Data" prefWidth="200"/>
 </columns>
</TableView>

2 answers

1

You must define which class belongs to the table and the same for the columns, would look like this:

private TableView<\Prova> prova;
private TableColumn<\Prova, String> colID;
  • 1

    Is the other answer also yours? I can see that there is not much difference between them.

  • can use the first

0

Try doing this:

@FXML
private TableView<Prova> prova;
@FXML
private TableColumn<Prova, ?> colId;
@FXML
private TableColumn<Prova, ?> colTipo;
@FXML
private TableColumn<Prova, ?> colProva;
@FXML
private TableColumn<Prova, ?> colData;

After filling the data in the Observablelist, for each column declared:

colId.setCellValueFactory(new PropertyValueFactory<Prova, Integer>("codigo"));

Browser other questions tagged

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