0

I'm trying to build an audio library using javafx and scene builder.

This is my relevant bit from my fxml file:

TableView fx:id="tableView" layoutX="45.0" layoutY="85.0" prefHeight="481.0" prefWidth="573.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columns>
                      <TableColumn prefWidth="154.0" text="Album" />
                      <TableColumn prefWidth="160.0" text="Songs" />
                        <TableColumn prefWidth="133.0" text="Artist" />
                        <TableColumn prefWidth="125.0" text="Genre" />
                    </columns>
                  </TableView>

It is linked with my controller class.

This is the data I am trying to populate it with from my Controller class:

final ObservableList<Integer> ratingSample = FXCollections.observableArrayList(1, 2, 3, 4, 5);
ObservableList<String> artists = FXCollections.observableArrayList("Adele",
        "Unknown", "Beyonce", "Rihanna", "Avril", "Disturbia", "Kid Rock", "Jessi J", "Unknown", "Unknown");

final ObservableList<Integer> ratingSample = FXCollections.observableArrayList(1, 2, 3, 4, 5);
    ObservableList<String> artists = FXCollections.observableArrayList("Adele",
            "Unknown", "Beyonce", "Rihanna", "Avril", "Disturbia", "Kid Rock", "Jessi J", "Unknown", "Unknown");

     ObservableList<String> titles = FXCollections.observableArrayList("Running in the Deep",
                "Title 01","Title 09","What's my name","What the Hell","Disturbia","Kid Rock","Price Tag","Title 2","09");


@FXML
TableView<Music> table = new TableView<>();

@FXML
private TableView<Music> tblViewer = new TableView<Music>();
@FXML
TableColumn<Music, Album> albumArt = new TableColumn<Music, Album>("Album Art");
@FXML
TableColumn<Music, String> title = new TableColumn<Music, String>("Title");
@FXML
TableColumn<Music, String> artist = new TableColumn<Music, String>("Artist");
@FXML
TableColumn<Music, Integer> rating = new TableColumn<Music, Integer>("Rating");
@FXML
    public TableView getTableView() {   

albumArt.setCellValueFactory(new PropertyValueFactory("album"));
title.setCellValueFactory(new PropertyValueFactory("title"));
artist.setCellValueFactory(new PropertyValueFactory("artist"));
rating.setCellValueFactory(new PropertyValueFactory("rating"));

        // SETTING THE CELL FACTORY FOR THE ALBUM ART
        albumArt.setCellFactory(new Callback<TableColumn<Music, Album>, TableCell<Music, Album>>() {
            @Override
            public TableCell<Music, Album> call(TableColumn<Music, Album> param) {
                TableCell<Music, Album> cell = new TableCell<Music, Album>() {
                    @Override
                    public void updateItem(Album item, boolean empty) {
                        if (item != null) {
                            HBox box = new HBox();
                            box.setSpacing(10);
                            VBox vbox = new VBox();
                            vbox.getChildren().add(new Label(item.getArtist()));
                            vbox.getChildren().add(new Label(item.getAlbum()));

                            ImageView imageview = new ImageView();
                            imageview.setFitHeight(50);
                            imageview.setFitWidth(50);
                            imageview.setImage(new Image(MusicTable.class.getResource("img").toString() + "/" + item.getFilename()));

                            box.getChildren().addAll(imageview, vbox);
                            //SETTING ALL THE GRAPHICS COMPONENT FOR CELL
                            setGraphic(box);
                        }
                    }
                };
                System.out.println(cell.getIndex());
                return cell;
            }

        });


        // SETTING THE CELL FACTORY FOR THE RATINGS COLUMN
        rating.setCellFactory(new Callback<TableColumn<Music, Integer>, TableCell<Music, Integer>>() {
            @Override
            public TableCell<Music, Integer> call(TableColumn<Music, Integer> param) {
                TableCell<Music, Integer> cell = new TableCell<Music, Integer>() {
                    @Override
                    public void updateItem(Integer item, boolean empty) {
                        if (item != null) {

                            ChoiceBox choice = new ChoiceBox(ratingSample);
                            choice.getSelectionModel().select(ratingSample.indexOf(item));
                            //SETTING ALL THE GRAPHICS COMPONENT FOR CELL
                            setGraphic(choice);
                        }
                    }
                };
                return cell;
            }

        });


        //ADDING ALL THE COLUMNS TO TABLEVIEW
        table.getColumns().addAll(albumArt, title, artist, rating);

        //ADDING ROWS INTO TABLEVIEW
        ObservableList<Music> musics = FXCollections.observableArrayList();
        for (int i = 0; i < 10; i++) {
            Album al = new Album(i + 1 + ".jpg", artists.get(i), artists.get(i));
            Music m = new Music(artists.get(i),al,titles.get(i),i%5);
            musics.add(m);
        }
        table.setItems(musics);

        return table;
    }

Music class:

package sample;

import javafx.beans.property.*;


public class Music {

    //Properties
    private SimpleStringProperty artist = new SimpleStringProperty();
    private ObjectProperty albumArt= new SimpleObjectProperty();
    private StringProperty title= new SimpleStringProperty();

    public Music(String artist, Album album, String title, int year) {
        setArtist(artist);
        setAlbum(album);
        setTitle(title);
        /*setGenre (genre);
        setYear(year);
        setRating(rating);*/
    }

    //ARTIST -----------
    public void setArtist(String art){
        artist.set(art);
    }

    public String getArtist(){
        return artist.get();
    }

    public StringProperty artistProperty(){
        return artist;
    }

    //ALBUM ------------
    public void setAlbum(Album alb){
        albumArt.set(alb);
    }

    public Object getAlbum(){
        return albumArt.get();
    }

    public ObjectProperty albumProperty(){
        return albumArt;
    }

    //TITLE -------------
    public void setTitle(String tit){
        title.set(tit);
    }

    public String getTitle(){
        return title.get();
    }

    public StringProperty titleProperty(){
        return title;
    }
    /*//GENRE --------------
    public void setGenre(String gen){
        genre.set(gen);
    }

    public String setGenre(){
        return genre.get();
    }

    public StringProperty genreProperty(){
        return genre;
    }


    //YEAR --------------
    public void setYear(int yea){
        year.set(yea);
    }

    public Integer getYear(){
        return year.get();
    }

    public IntegerProperty yearProperty(){
        return year;
    }

    //RATING --------------
    public void setRating(int rat){
        rating.set(rat);
    }

    public Integer getRating(){
        return rating.get();
    }

    public IntegerProperty ratingProperty(){
        return rating;
    }

    */
}

So, the code runs, but no data is shown on the table.

I have tried quite a few versions of this. However nothing seems to work. Could somebody help me understand what I am doing wrong?

april21
  • 115
  • 2
  • 9
  • Have you set the `cellValueFactory` (as well as the `cellFactory`) on each of the columns? – James_D May 20 '15 at 15:37
  • @James_D I have edited it. but still its not loading the data on the table view – april21 May 20 '15 at 16:12
  • Can you post the `Music` class? Also, why have you declared two `TableView`s? Is the one in the `getTableView()` method ever added to the scene graph? – James_D May 20 '15 at 16:13
  • The relationship between the FXML file and the controller is all messed up. The `fx:id` of the table you define in the FXML file is `tableView` but there is no matching field in your controller (that you have shown). Similarly, the `TableColumn`s you have defined in the FXML have no `fx:id`, so those are not the columns that are being configured in the controller. It is **always** an error to initialize fields in the controller if they are annotated with `@FXML`: `@FXML private TableView table = new TableView<>();` should be `@FXML private TableView table ;` – James_D May 20 '15 at 16:20
  • See if http://stackoverflow.com/questions/28588842/javafx-field-value-in-controller-class-isnt-saved-correctly-trouble-updating-l helps (The first part of the answer is relevant, the second isn't.) – James_D May 20 '15 at 16:31
  • Have posted the music class. I declared @FXML public TableView tableView; before, is that correct? - because it won't let me do private TableView table ; and it throws an error – april21 May 20 '15 at 16:55
  • I have checked the link out but I don't see what I should change in my code. I have tried making a few changes - but still no data is getting displaed. thanks a lot for taking the time out to help me, i really appreciate it, if you could help me understand further what I am doing wrong and what I could do I would be very very grateful! – april21 May 20 '15 at 16:57
  • There really is just too much wrong here to deal with in a single question. (Why are there two `TableView`s in the controller? Why are you adding columns when they are already added in the FXML? When is the `getTableView()` method called?) I recommend you work through the tutorial first (start [here](http://docs.oracle.com/javase/8/javafx/get-started-tutorial/fxml_tutorial.htm#JFXST206) to understand how to use FXML, then [here](http://docs.oracle.com/javase/8/javafx/fxml-tutorial/fxml_tutorial_intermediate.htm) for a table example), and just get a table view working. Then try your own. – James_D May 20 '15 at 17:04
  • Hey, I've gotten my head around this- but I am struggling to formulate how to put the edit and delete buttons for the address book. Could you please help me out? - even if you roughly tell me what to do - it would be so much help! @James_D – april21 May 21 '15 at 20:29

0 Answers0