Reputation: 436
I'm trying to display data from my ObservableList into a TableView, which I created in SceneBuilder. The TableView contains three TableColumns. I've given them the exact same name as in the FXML-file.
I've imported them into the Controller like you can see down below [1]
And then I'm trying to run my code. I basically have a song. The song consists of a title, artist and album. I want to display it on each column, but it keeps complaining about:
Duplicate TableColumns detected in TableView columns list with titles 'Song title', 'Artist', 'Album'.
And then crashes.
Anybody know what Im doing wrong?
@FXML
private TableView<Song> defaultTableView;
// 1) Song Title
@FXML
private TableColumn songTitleColumn;
// 2) Song artist
@FXML
private TableColumn songArtistColumn;
// 3) Song album
@FXML
private TableColumn songAlbumColumn;
public void initialize(URL location, ResourceBundle resources) {
Song song1 = new Song("a lot.mp3");
Song song2 = new Song("Work Out.mp3");
Song song3 = new Song("Jesus Walks.mp3");
Playlist myPlaylist = new Playlist("My awesome playlist");
myPlaylist.addSong(song1);
myPlaylist.addSong(song2);
song1.getSongTitleFromDB();
song1.getSongArtistFromDB();
song1.getSongAlbumFromDB();
myPlaylist.displayAllSongs(defaultTableView,songTitleColumn,songArtistColumn,songAlbumColumn);
ObservableList<Song> songs = FXCollections.observableArrayList();
songs.add(song1);
songTitleColumn.setCellFactory(new PropertyValueFactory<Song,String>("songTitle"));
songArtistColumn.setCellFactory(new PropertyValueFactory<Song, String>("songArtist"));
songAlbumColumn.setCellFactory(new PropertyValueFactory<Song, String>("songAlbum"));
defaultTableView.setItems(songs);
defaultTableView.getColumns().addAll(songTitleColumn,songArtistColumn,songAlbumColumn);
Upvotes: 0
Views: 308
Reputation: 82491
Your fxml file looks something like this:
<TableView fx:id="defaultTableView">
<columns>
<TableColumn fx:id="songTitleColumn"/>
<TableColumn fx:id="songArtistColumn"/>
<TableColumn fx:id="songAlbumColumn"/>
</columns>
</TableView>
Since columns
is a readonly list property, loading the fxml file results in the columns being added to the TableView
. Trying to add them another time to the TableView
results in the exception.
You do this in the initialize
method:
defaultTableView.getColumns().addAll(songTitleColumn, songArtistColumn, songAlbumColumn);
You need to remove this link or do all of the column creation in the initialize
method.
Upvotes: 2