0

I'm faced with problems as I attempt to manage data inside of a controller class for my GUI.

1) I am finding trouble retrieving data in to a TableView.

2) I've tried to look at general designs of JavaFX applications and I haven't really gotten the "standard" way of creating such things. Am I following a standard pattern where I have the controllers and a main? Should I aim to have just one controller to make this easier (but the code messier?)

Main

public class TrackPlayerMain extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane root = new BorderPane();
        FXMLLoader trackPlayerLoader = new FXMLLoader(getClass().getResource("TrackPlayer.fxml"));
        root.setCenter(trackPlayerLoader.load());
        TrackPlayerController trackPlayerController = trackPlayerLoader.getController();

        DataModel model = new DataModel();
        trackPlayerController.initData(model);

        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Data

public class DataModel {
    private final ObservableList<Track> trackList = FXCollections.observableArrayList(track -> 
        new Observable[] {
            track.trackTitleProperty(), 
            track.trackArtistProperty(),
            track.trackGenreProperty()});
    private final ObjectProperty<Track> currentTrack = new SimpleObjectProperty<>(null);

    public ObjectProperty<Track> currentTrackProperty() {
        return currentTrack;
    }
    public final Track getCurrentTrack() {
        return currentTrackProperty().get();
    }
    public final void setCurrentTrack(Track track) {
        currentTrackProperty().set(track);
    }
    public ObservableList<Track> getTrackList() {
        return trackList;
    }
    public void loadData() { // Database as parameter?
        trackList.setAll(
                new Track("Track1", "Artist1", "Genre1"),
                new Track("Track2", "Artist2", "Genre2"),
                new Track("Track3", "Artist3", "Genre3"),
                new Track("Track4", "Artist4", "Genre4")
        );
    }
    public void saveData() { } // Database as parameter?
}

Track

public class Track {
    public Track(String trackTitle, String trackArtist, String trackGenre) {
        setTrackTitle(trackTitle);
        setTrackArtist(trackArtist);
        setTrackGenre(trackGenre);
    }
    // -----------------------------------------------------
    private final StringProperty trackTitle = new SimpleStringProperty();

    public final StringProperty trackTitleProperty() {
        return this.trackTitle;
    }
    public final String getTrackTitle() {
        return this.trackTitleProperty().get();
    }
    public final void setTrackTitle(final String trackTitle) {
        this.trackTitleProperty().set(trackTitle);
    }
    // -----------------------------------------------------
    private final StringProperty trackArtist = new SimpleStringProperty();

    public final StringProperty trackArtistProperty() {
        return this.trackArtist;
    }
    public final String getTrackArtist() {
        return this.trackArtistProperty().get();
    }
    public final void setTrackArtist(final String trackArtist) {
        this.trackArtistProperty().set(trackArtist);
    }   
    // -----------------------------------------------------
    private final StringProperty trackGenre = new SimpleStringProperty();

    public final StringProperty trackGenreProperty() {
        return this.trackGenre;
    }
    public final String getTrackGenre() {
        return this.trackGenreProperty().get();
    }
    public final void setTrackGenre(final String trackGenre) {
        this.trackGenreProperty().set(trackGenre);
    }
    // -----------------------------------------------------
}

Track Player Controller

public class TrackPlayerController implements Initializable {
    // -----------------------------------------------------
    @FXML private Button remove;
    @FXML private Button removeAll;
    @FXML private Button search;
    // -----------------------------------------------------
    @FXML private TableView<Track> playingTable;
    @FXML private TableColumn<Track, String> playingTitleCol;
    @FXML private TableColumn<Track, String> playingArtistCol;
    @FXML private TableColumn<Track, String> playingGenreCol;
    // -----------------------------------------------------
    @FXML private Button play;
    @FXML private Button pause;
    @FXML private Button rw;
    @FXML private Button ff;
    @FXML private Button reset;
    // -----------------------------------------------------
    @FXML private Label runTime;
    // -----------------------------------------------------
    private DataModel model;

    @Override
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        playingTitleCol.setCellValueFactory(new PropertyValueFactory<>("titleName"));
        playingArtistCol.setCellValueFactory(new PropertyValueFactory<>("artistName"));
        playingGenreCol.setCellValueFactory(new PropertyValueFactory<>("genreName"));
    }
    @FXML private void play(ActionEvent e) {
        System.out.println("Play pressed");
    }
    @FXML private void pause(ActionEvent e) {
        System.out.println("Pause");
    }
    @FXML private void remove(ActionEvent e) {
    }
    @FXML private void removeAll(ActionEvent e) {
    }
    @FXML private void search(ActionEvent e) throws IOException{
    }

    void initData(DataModel model) {
        if(this.model != null) {
            throw new IllegalStateException("Data has already been initialized");
        }
        this.model = model;
        playingTable.setItems(model.getTrackList());
    }
}
Xylus
  • 75
  • 8
  • 1
    Your data is represented by a model class (`PlayingData`), you need a single instance to be shared between all the controllers you load. Have a look at http://stackoverflow.com/questions/32342864/applying-mvc-with-javafx (which is simpler) and see if it helps. – James_D Feb 12 '16 at 02:45
  • So, in other words (I think), in your `back()` method, just pass the data again to the new controller you get from the `FXMLLoader` – James_D Feb 12 '16 at 02:53
  • @James_D Hi James, I tried to do what you've done in the question you have linked however no data is displayed after I use setItems although I think I don't fully understand your code after you setItems (in ListController.java). I will edit my post with your implementation, could you advise further if possible? – Xylus Feb 13 '16 at 17:30
  • Your property names are wrong... – James_D Feb 13 '16 at 18:49
  • @James_D I've corrected them but the data is still not being retrieved. What am I missing exactly? Something in the initData method? – Xylus Feb 13 '16 at 19:02
  • I apologize, I was being really stupid! I didn't call loadData() anywhere!! – Xylus Feb 13 '16 at 19:22

0 Answers0