0

I don't succeed in filling a tableView with data, although I managed it before in exactly (as it appears to me ...) the same way.

(By the way: is there a way to display in one column not an object, that is referenced in the class I'm presenting in the tableView (such as FAVORITEBOOK), but an attribute-object of this object (such as the TITLE of the FAVORITEBOOK? Thanks!)

My Main class:

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.ArrayList;

public class Main extends Application {

    Student student1 = new Student(0, "Jennifer", new Book("JavaFX for Dummies"));
    Student student2 = new Student(1, "John", new Book("Cooking with Jamie Oliver"));
    Student student3 = new Student(2, "Barbara", new Book("Jokes from Germany"));
    ObservableList<Student> studentsObservable = FXCollections.observableArrayList(student1, student2, student3);

    public static void main (String[] args) {

        launch (args);
    }

    public void start (Stage primaryStage) throws Exception {

        TableColumn<Student, Integer> idCol = new TableColumn<>("id");
        TableColumn<Student, String> nameCol = new TableColumn<>("name");;
        TableColumn<Student, Book> favoriteBookCol = new TableColumn<>("favoriteBook");;
        TableView tableView = new TableView();
        tableView.getColumns().setAll(idCol, nameCol, favoriteBookCol);

        idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
        nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        favoriteBookCol.setCellValueFactory(new PropertyValueFactory<>("favoriteBook"));

        tableView.setItems(studentsObservable);

        HBox root = new HBox();
        root.getChildren().add(tableView);
        Scene scene = new Scene (root);
        primaryStage.setScene(scene);
        primaryStage.show();

    }
}

the classes Student and Book:

public class Student {
   Integer id;
   String name;
   Book favoriteBook;

   public Student (Integer id, String name, Book favoriteBook) {
       this.id = id;
       this.name = name;
       this.favoriteBook = favoriteBook;
   }
}

public class Book {
   String title;

   public Book (String title) {
       this.title = title;
   }
}

Thank you very much indeed for your effort and time!

  • Can you post the entire `Student` and `Book` classes, including the accessor methods? – James_D Jan 07 '21 at 19:15
  • Thanks for the hint (I think it was one). Now, with the accessor methods I can display the attributes - except for the favorite book (as I said I'd rather display the title than the book object itself ...) (I'll post the completed classes as an answer.) – MagisterInformaticus Jan 07 '21 at 19:26
  • Just do `favoriteBookCol.setCellValueFactory(data -> new SimpleStringProperty(data.getValue().getFavoriteBook().getTitle()));`, and make it a `TableColumn`. Or keep it as is and add a `cellFactory` that display's the book's title. – James_D Jan 07 '21 at 19:29
  • Why has the question been closed? The problem is half resolved so far. – MagisterInformaticus Jan 07 '21 at 19:30
  • Added a second duplicate. It's probably better to only ask one question per post. – James_D Jan 07 '21 at 19:33
  • Thanks, everything working now! How would I add that cellFactory displaying the book's title? – MagisterInformaticus Jan 07 '21 at 19:38
  • See https://stackoverflow.com/questions/50289679/work-with-value-from-propertyvaluefactory-in-tableview-javafx, just call `getTitle()` on the book that's being displayed. – James_D Jan 07 '21 at 19:39

0 Answers0