我是JavaFx和GUI的新手,所以请放轻松我。
我正在开发某种MP3播放器应用程序,我目前处于GUI部分。我基本上在我的应用程序中间有一个表视图,它基本上是我的库,您可以从文件夹导入。我在左边创建了一个新表,它基本上是播放列表,我正在尝试实现功能,用户可以将多行从库中拖放到播放列表中。
这是一张目前为止看起来如何帮助您想象它的图片: Application GUI
我在这里以及google上搜索了所有可以找到的关于在同一个表中复制特定单元格的例子等等,但在我的情况下,我试图从中复制整个对象(行)库到播放列表。
如果有人可以帮我解决这个问题,我将不胜感激。
以下是代码的一些相关部分:
我的控制器类中的initialize方法的某些部分。
public void initialize(URL location, ResourceBundle resources) {
// Initialize Library Table
imageColumnID.setCellValueFactory(new PropertyValueFactory<Table, Image>("picture"));
nameColumnID.setCellValueFactory(new PropertyValueFactory<Table, String>("songTitle"));
artistColumnID.setCellValueFactory(new PropertyValueFactory<Table, String>("artist"));
albumColumnID.setCellValueFactory(new PropertyValueFactory<Table, String>("album"));
trackNumColumnID.setCellValueFactory(new PropertyValueFactory<Table, String>("trackNumber"));
genreColumnID.setCellValueFactory(new PropertyValueFactory<Table, String>("genre"));
dateColumnID.setCellValueFactory(new PropertyValueFactory<Table, String>("date"));
copyrightColumnID.setCellValueFactory(new PropertyValueFactory<Table, String>("copyrightInformation"));
webpageColumnID.setCellValueFactory(new PropertyValueFactory<Table, String>("publisherWebpage"));
ratingColumnID.setCellValueFactory(new PropertyValueFactory<Table, String>("rating"));
playCounterColumnID.setCellValueFactory(new PropertyValueFactory<Table, Integer>("playCounter"));
ratingData.add("");
ratingData.add("*");
ratingData.add("**");
ratingData.add("***");
ratingData.add("****");
ratingData.add("*****");
ratingColumnID.setCellFactory(ComboBoxTableCell.forTableColumn(new DefaultStringConverter(), ratingData));
ratingColumnID.setEditable(true);
ratingColumnID.setOnEditCommit(e -> handleRating());
library = new SongLibrary();
// library table
tableID.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
tableID.setRowFactory(e -> {
TableRow<Table> row = new TableRow<>();
row.setOnMouseClicked(ev -> {
if (ev.getButton() == MouseButton.PRIMARY && ev.getClickCount() == 2)
handleMouseClick();
});
row.setOnDragDetected(ev1 -> dragAndDropIntoPlaylist());
return row;
});
// Initialize Playlist Table
playlistSongTitle.setCellValueFactory(new PropertyValueFactory<PlaylistTable, String>("songTitle"));
playlistArtist.setCellValueFactory(new PropertyValueFactory<PlaylistTable, String>("artist"));
playlistAlbum.setCellValueFactory(new PropertyValueFactory<PlaylistTable, String>("album"));
playlistTracknr.setCellValueFactory(new PropertyValueFactory<PlaylistTable, String>("trackNumber"));
playlistTable.setRowFactory(e -> {
TableRow<PlaylistTable> row = new TableRow<>();
row.setOnDragDropped(ev -> dragAndDropIntoPlaylist());
return row;
});
}
库表构造函数:
public Table(Song song) {
this.song = song;
this.metadata = song.getMetadata();
// handle picture
if (metadata.getPicture().isPresent()) {
Image img = convertToJavaFXImage(metadata.getPicture().get().getData(), 50, 50);
ImageView image = new ImageView(img);
image.setFitHeight(30);
image.setFitWidth(30);
image.prefHeight(30);
image.prefWidth(30);
image.maxHeight(30);
image.maxWidth(30);
this.picture = new SimpleObjectProperty<ImageView>(image);
} else {
this.picture = new SimpleObjectProperty<>();
}
this.songTitle = new SimpleStringProperty(metadata.getSongTitle().orElse(""));
this.artist = new SimpleStringProperty(metadata.getArtist().orElse(""));
this.album = new SimpleStringProperty(metadata.getAlbumTitle().orElse(""));
this.trackNumber = metadata.getTrackNumber().isPresent() ?
new SimpleStringProperty(String.valueOf(metadata.getTrackNumber().get())) : new SimpleStringProperty("");
this.genre = metadata.getGenre().isPresent() ?
new SimpleStringProperty(metadata.getGenre().get().getName()) : new SimpleStringProperty("");
this.date = metadata.getDate().isPresent() ?
new SimpleStringProperty(metadata.getDate().get().toString().substring(0, 4)) : new SimpleStringProperty("");
this.copyrightInformation = metadata.getCopyrightInformation().isPresent() ?
new SimpleStringProperty(metadata.getCopyrightInformation().get().toString()) : new SimpleStringProperty("");
this.publisherWebpage = metadata.getPublisherWebpage().isPresent() ?
new SimpleStringProperty(metadata.getPublisherWebpage().get().toString()) : new SimpleStringProperty("");
this.rating = metadata.getRating().isPresent() ?
new SimpleStringProperty(String.valueOf(metadata.getRating().get())) : new SimpleStringProperty();
this.playCounter = metadata.getPlayCounter().isPresent() ?
new SimpleIntegerProperty(metadata.getPlayCounter().get()) : new SimpleIntegerProperty(0);
}
播放列表表构造函数:
public PlaylistTable(Table row){
this.song = row.getSong();
this.songTitle = new SimpleStringProperty(row.getSongTitle());
this.artist = new SimpleStringProperty(row.getArtist());
this.album = new SimpleStringProperty(row.getAlbum());
this.trackNumber = new SimpleStringProperty(row.getTrackNumber());
}
我的想法是我可以从库中选择一堆行并将它们拖放到播放列表中,基本上从“Table”类型转移行,然后将其传递给播放列表表的构造函数,然后添加到table的可观察列表,但我不确定如何实现它。
任何帮助将不胜感激!
谢谢!