为什么TableView没有填充?

时间:2015-05-20 14:43:28

标签: javafx tableview fxml

我正在尝试使用javafx和场景构建器构建音频库。

这是我在fxml文件中的相关位:

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>

它与我的控制器类链接。

这是我试图用我的Controller类填充它的数据:

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

音乐课:

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

    */
}

因此,代码运行,但表中没有显示数据。

我尝试了很多这个版本。然而似乎没有任何作用。有人能帮助我理解我做错了什么吗?

0 个答案:

没有答案
相关问题