mardi 4 avril 2017

JavaFX How to set String values to TableView

To give some background: I now am able to load files onto my mp3 program and play them but all the values in my tableview are null? enter image description here

My song class

package application;

//imports here

public class Song {
private String title;
private String artist;
private String album;
private SimpleStringProperty pTitle;
private SimpleStringProperty pArtist;
private SimpleStringProperty pAlbum;
private Media music;
private MediaPlayer mp;
private Image coverArt;

public Song(File file) {
    music = new Media(file.toURI().toString());
    music.getMetadata().addListener((Change<? extends String, ? extends Object> c) -> {
        if (c.wasAdded()) {
            if ("artist".equals(c.getKey())) {
                System.out.println(c.getKey()+":"+c.getValueAdded());
                this.pArtist = new SimpleStringProperty(c.getValueAdded().toString());
                //pArtist.set(c.getValueAdded().toString());
                artist = c.getValueAdded().toString();  
            } else if ("title".equals(c.getKey())) {
                title = c.getValueAdded().toString();
                System.out.println(c.getKey()+":"+c.getValueAdded());
            } else if ("album".equals(c.getKey())) {
                album = c.getValueAdded().toString();
                System.out.println(c.getKey()+":"+c.getValueAdded());
            } else if ("image".equals(c.getKey())) {
                coverArt = (Image) c.getValueAdded();
            }
        }
    });
    mp = new MediaPlayer(music);
    System.out.println(pArtist);
    System.out.println(artist);
    //artist = (String) mp.getMedia().getMetadata().get("artist");
    //title = (String) music.getMetadata().get("title");
    //album = (String) music.getMetadata().get("album");
    //artist = "test";
    //album = "test";
    //title = "test";
}

public void play() {
    mp.play();
}

public void pause() {
    mp.pause();
}

public void stop() {
    mp.stop();
}

public String getTitle(){
    return title;
}

public void setTitle(String title){
    this.title = title;
}

public String getArtist(){
    return artist;
}

public void setArtist(String artist){
    this.artist = artist;
}

public String getAlbum(){
    return album;
}

public void setAlbum(String album){
    this.album = album;
}

public Image getCover(){
    return coverArt;
}

public MediaPlayer getMP(){
    return mp;
}





}

Weirdly enough at first I thought it was because my String variables were not setting correctly and were set to null since it shows as null in the console when I put these print lines to test it when the Song object is being constructed. Here is a sample of the console when I test this.

null
null
artist:Foo Fighters
album:Saint Cecilia EP
title:Saint Cecilia

Here is my controller class

public class SceneController implements Initializable{
@FXML
private Button stopBtn;
@FXML
private Slider volume;
@FXML
private Button loadBtn;
@FXML
private Button playBtn;
@FXML
private TableView<Song> table;
@FXML
private Label label;
@FXML
private ProgressBar proBar;
private TableColumn songCol,artistCol,albumCol;
ObservableList<Song> songList = FXCollections.observableArrayList();
List<File> list;
FileChooser fileChooser = new FileChooser();
Desktop desktop;
Song mySong;



@Override
public void initialize(URL arg0, ResourceBundle arg1) {
    TableColumn songCol = new TableColumn("Song");
    TableColumn artistCol = new TableColumn("Artist");
    TableColumn albumCol = new TableColumn("Album");

    songCol.setCellValueFactory(
            new PropertyValueFactory<Song,String>("Title"));
    //songCol.setCellFactory(new Callback);
    artistCol.setCellValueFactory(
            new PropertyValueFactory<Song,String>("Artist"));
    albumCol.setCellValueFactory(
            new PropertyValueFactory<Song,String>("Album"));
    volume.setMin(0);
    volume.setMax(100);
    volume.setValue(100);
    volume.valueProperty().addListener(new InvalidationListener() {
        @Override
        public void invalidated(Observable observable) {
            mySong.getMP().setVolume(volume.getValue()/100.0);
        }

    });

}

// Event Listener on Button[#loadBtn].onAction
@FXML
public void loadFile(ActionEvent event) {
    Node source = (Node) event.getSource();
    Window theStage = source.getScene().getWindow();
    //set fileChooser filter
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("MP3 files", "*.mp3");
    fileChooser.getExtensionFilters().add(extFilter);
    fileChooser.setTitle("Select MP3 files");
    //File file = fileChooser.showOpenDialog(theStage);
    //mySong = new Song(file);
    list = fileChooser.showOpenMultipleDialog(theStage);
    if(list!=null){
        for(File x: list) {
            mySong = new Song(x);
            System.out.println(mySong.getTitle());
            songList.add(mySong);
        }
    }
    table.setItems(songList);
}

@FXML
public void playSong(ActionEvent event) {
    mySong.play();
}

@FXML
public void stopSong(ActionEvent event) {
    //mySong.pause();
    System.out.println("song title: "+mySong.getArtist()+mySong.getTitle());
    ImageView img = new ImageView(mySong.getCover());
    //img.fitWidthProperty().bind(label.widthProperty());
    //img.fitHeightProperty().bind(label.heightProperty());
    img.setFitHeight(120);
    img.setFitWidth(200);
    label.setGraphic(img);
    //label.setGraphic(new ImageView(mySong.getCover()));
}

But I made another test print line for my "Stop" button in the controller class and after everything is loaded and I press it, it prints out the artist and title fine. I have saw this other thread and checked my getter methods and they seem to be correct? I am really lost on this and if anyone could provide some insight and a solution as to whether it is because my variables are null or my PropertyValueFactory is not done correctly

Also I notice that the nulls come first even though should they not be the last thing printed since when I create a new song object in my controller class the first print lines that run are in the if statements?





Aucun commentaire:

Enregistrer un commentaire