1

Im currently passing my data through a constructor which fills in data requested from both my abstract and extended classes (i.e MusicItem Abstract and CD and Vinyl classes extended)

Im trying to print these into a Table View using JavaFX from my controller but nothing is showing up even with the system.out line placed.

I've tried putting them into Observable Lists as well as normal array lists, I've triple checked the data fields to see if they match the table and i tried viewing the the object at the moment its entered but when i do that, the hex code for that object shows up. It connects to the database properly but doesn't display anything

abstract class MusicItem {
    @Id
    private int songID;
    private String name;
    private String artist;
    private String genre;
    private String releaseDate;
    private double price;

public MusicItem(int songID, String name, String artist, String genre, String releaseDate, double price) {
    this.songID = songID;
    this.name = name;
    this.artist = artist;
    this.genre = genre;
    this.releaseDate = releaseDate;
    this.price = price;
    }
}


@Entity
class Vinyl extends MusicItem{

private double speed;
private double diameter;

Vinyl(int songID, String name, String artist, String genre, String releaseDate, double price, double speed, double diameter) {
    super(songID, name, artist, genre, releaseDate, price);
    this.speed = speed;
    this.diameter = diameter;
    }
}


@Entity
class CD extends MusicItem{

private double duration;

CD(int songID, String name, String artist, String genre, String releaseDate, double price, double duration) {
    super(songID, name, artist, genre, releaseDate, price);
    this.duration = duration;
   }
}


public WestminsterMusicStoreManager() throws UnknownHostException {
}

@Override
public void insertItem() {
    System.out.print("Please enter Song ID : ");
    id = Validation.intValidate();
    System.out.print("Please enter Song name : ");
    name = Validation.stringValidate();
    System.out.print("Please enter Artist : ");
    artist = Validation.stringValidate();
    System.out.print("Please enter genre of  " + name + " : ");
    genre = Validation.stringValidate();
    System.out.println("Please enter the release date in the requested order: ");
    releaseDate = date.getDate();
    System.out.print("Please enter price of song : ");
    price = Validation.doubleValidate();

    System.out.println("Will you be entering a CD or Vinyl Entry");
    String choice = Validation.stringValidate();
    switch (choice.toLowerCase()){
        case "vinyl":
            System.out.print("Please enter diameter of vinyl : ");
            diameter = Validation.doubleValidate();
            System.out.print("Please enter speed of vinyl : ");
            speed = Validation.doubleValidate();
            Vinyl vinyl = new Vinyl(id,name,artist,genre,releaseDate,price,speed,diameter);
            musicItemList.add(vinyl);
            database.insertVinyl(vinyl);
            System.out.println(name+" was succesfully added to the database with an ID of"+id);
            break;


        case "cd":
            System.out.println("Please enter duration of the song");
            duration = Validation.doubleValidate();
            CD cd = new CD(id,name,artist,genre,releaseDate,price,duration);
            musicItemList.add(cd);
            System.out.println(cd);
            database.insertCD(cd);
            break;

            default:
                System.out.println("Your value needs to be a choice between either CD or Vinyl");
                insertItem();
    }
}
}

public class Controller implements Initializable {
public GridPane customerMainLayout;
@FXML private TableColumn<MusicItem, String> artistCol, songNameCol, durationCol, genreCol;
@FXML private TableColumn<MusicItem, String> priceCol;
@FXML private TableColumn<MusicItem, Double> speedCol,diameterCol;
@FXML private TableColumn<MusicItem, Integer> songIDCol, releaseYearCol;
public TableView<MusicItem> customerViewTable;
@FXML private static JFXTextField searchBar;
@FXML private static JFXButton searchBtn;

private WestminsterMusicStoreManager musicStoreManager =new WestminsterMusicStoreManager();
private ObservableList<MusicItem> musicItem = FXCollections.observableArrayList(musicStoreManager.musicItemList);

public Controller() throws UnknownHostException {
}

public void initialize(URL location, ResourceBundle resources) {

    songIDCol.setCellValueFactory(new PropertyValueFactory<>("songID"));
    songNameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
    artistCol.setCellValueFactory(new PropertyValueFactory<>("artist"));
    genreCol.setCellValueFactory(new PropertyValueFactory<>("genre"));
    releaseYearCol.setCellValueFactory(new PropertyValueFactory<>("releaseDate"));
    priceCol.setCellValueFactory(new PropertyValueFactory<>("price"));
    durationCol.setCellValueFactory(new PropertyValueFactory<>("duration"));
    speedCol.setCellValueFactory(new PropertyValueFactory<>("speed"));
    diameterCol.setCellValueFactory(new PropertyValueFactory<>("diameter"));
    addTableItems();
}


private void addTableItems() {
    musicItem.forEach(musicItem -> {
        if (musicItem instanceof CD){
            CD cd = (CD)musicItem;
            System.out.println(cd);
            customerViewTable.getItems().add(cd);
        }else {
            Vinyl vinyl = (Vinyl)musicItem;
            System.out.println(vinyl);
            customerViewTable.getItems().add(vinyl);
        }
    });
   }
}


AiSirachcha21
  • 119
  • 14
  • There don't seem to be getters for the properties. `PropertyValueFactory` relies on methods to retrieve those column values. As for the "hex code": https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – fabian Aug 02 '19 at 13:21
  • Hey so, does it necessarily require a getter in this case since im already feeding in the object from my external class? – AiSirachcha21 Aug 02 '19 at 13:45
  • You have no `Property`'s for your `MusicItem` class... So not only is there no getter, but there is no property to even look for. – trilogy Aug 02 '19 at 14:36
  • @Ryan Check out [the documentation](https://openjfx.io/javadoc/12/javafx.controls/javafx/scene/control/cell/PropertyValueFactory.html) of `PropertyValueFactory`. It uses reflection to locate the appropriate property-getter method or, if that method doesn't exist, the appropriate getter method. It also assumes standard Java Bean naming convention. So if you have a property `x`, then you need a `getX()` method, a `setX(X)` method (if writable), and a `xProperty()` method (if it's a JavaFX property). – Slaw Aug 02 '19 at 14:43
  • @Slaw wouldn't this still require properties to exist in the model class? I only see `String` and other primitives... – trilogy Aug 02 '19 at 15:12
  • @trilogy No. The `PropertyValueFactory` class relies on the so-called "JavaFX Bean" convention, which is fundamentally a form of the Java Bean convention just with an added "property-getter" that returns an instance of a JavaFX `[ReadOnly]Property`. However, the `PropertyValueFactory` class doesn't _require_ a property-getter. If the method is present it will use it, otherwise it falls back to the plain getter method and wraps the result in a `ReadOnlyObjectWrapper` (an `ObservableValue`). The table cell won't be notified of changes to the underlying model "property" in this case, though. – Slaw Aug 02 '19 at 15:55
  • So this same code was replicated on my friends machine and it worked.... he checked it himself and there was nothing wrong with it... and STILL no values are shown @Slaw – AiSirachcha21 Aug 02 '19 at 23:31

1 Answers1

0

Figured it out. The code I wrote was never incorrect. The Observable list i had called was calling a new instance which was why no values returned (Because the list was empty after the instance call). I connected that values directly from MongoDB and got the values by adding them to an observable list. And setting the table columns and data to whatever the result was and it worked well

 private ObservableList<MusicItem> addTableItems() throws UnknownHostException {
    ObservableList<MusicItem> musicItem = FXCollections.observableArrayList();
    Database database = new Database();
    for (MusicItem item: database.datastore.find(MusicItem.class).asList()){
        musicItem.addAll(item);
    }
    return musicItem;
}

Result After Database inclusion

AiSirachcha21
  • 119
  • 14
  • 2
    hmm ... if that's working (and I'm glad that you found a solution :), you didn't show the relevant code in your question: in particular the data objects (MusicItem and descendants) were incomplete - without getters/setters a PropertyValueFactory does nothing. So next time, please provide a [mcve] that demonstrates the problem. – kleopatra Aug 03 '19 at 09:29