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());
}
}