0

Hi guys I am new to java and I am stuck.

I have created a class called song with the code below

package musiclibrary;

public class Song {
    private String title;
private String albumArtist;
private String genre;
private double price;
private int duration;

    public Song() {
    }

        public Song(String title, String albumArtist, String genre, double price, int duration) {
        this.title = title;
        this.albumArtist = albumArtist;
        this.genre = genre;
        this.price = price;
        this.duration = duration;
    }

    public String getTitle() {
        return title;
    }

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

    public String getAlbumArtist() {
        return albumArtist;
    }

    public void setAlbumArtist(String albumArtist) {
        this.albumArtist = albumArtist;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int durationSeconds) {
        this.duration = durationSeconds;
    }
}

and i have created an object array as shown below

     private  ObservableList<Song> songs =FXCollections.observableArrayList();
     public  ObservableList<Song> load()
     {
     songs.add(new Song("J. Cole", "Lost Ones", "Hip Hop", 24.66, 3000));
     songs.add(new Song("Erykah Badu", "Time's a wasting", "Neo-Soul", 24.66, 4000));
     songs.add(new Song("Common", "Book of Life", "Hip Hop", 24.66, 2000));
     songs.add(new Song("CommonGround", "dasdsad", "asdsadsad", 24.66, 3000));
     return  songs;
     }

Problem is that I don't know how to call the "load" function in a listView in the main method. below is what i have

  private ListView<Song> ListView;
    @Override
    public void initialize(URL url, ResourceBundle rb) {

          PlayList pl= new PlayList();
          ListView.setItems(pl.play());



    }

Attached is the result. please help!

Result

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Tom
  • 1
  • 1
    What exactly is your problem? Are you unsatisified with the representation of the songs? If so, you may want to overwrite the [`toString()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) method of `Song` (for more details, see [the corresponding Oracle tutorial](https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html)). – Turing85 Oct 09 '16 at 18:29

1 Answers1

1

Seems as though you're displaying the default toString() inherited from java.lang.Object. You should override it in Song and display the information you want. E.g., let's assume you just want to display the Song's title and artist:

@Override
public Sting toString() {
    return title + " by " + albumArtist;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350