I currently have a custom cell factory that contains videos played through javafx.scene.media. The problem is that when I add more than one video the application is laggy.
How can I put each cell on its own thread? Are there better ways to add the video to the cell that would have each video on its own thread?
CrontrolsFX Custom Cell Factory
gridView.setCellFactory(new Callback<GridView<MediaCard>, GridCell<MediaCard>>() {
@Override
public GridCell<MediaCard> call(GridView<MediaCard> param) {
// TODO Auto-generated method stub
return new MediaCell();
}
});
MediaCell.java
public class MediaCell extends GridCell<MediaCard>{
public MediaCell() {
super();
}
@Override
protected void updateItem(MediaCard item, boolean empty) {
if (item != null) {
super.updateItem(item, empty);
AnchorPane rootAnchorPane = new AnchorPane();
File f = new File(item.getPath());
Media media = new Media(f.toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
mediaPlayer.play();
MediaView mv = new MediaView(mediaPlayer);
mv.setPreserveRatio(true);
Button viewMediaButton = new Button("View");
VBox vbox = new VBox(mv, viewMediaButton);
DoubleProperty mvw = mv.fitWidthProperty();
DoubleProperty mvh = mv.fitHeightProperty();
mvw.bind(Bindings.selectDouble(mv.parentProperty(), "width"));
mvh.bind(Bindings.selectDouble(mv.parentProperty(), "height"));
rootAnchorPane.getChildren().addAll(vbox);
AnchorPane.setTopAnchor(vbox, 0.0);
AnchorPane.setBottomAnchor(vbox, 0.0);
AnchorPane.setLeftAnchor(vbox, 0.0);
AnchorPane.setRightAnchor(vbox, 0.0);
setGraphic(rootAnchorPane);
}
}
}
Failed attempt to move each cell to its own thread
gridView.setCellFactory(new Callback<GridView<MediaCard>, GridCell<MediaCard>>() {
@Override
public GridCell<MediaCard> call(GridView<MediaCard> param) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
mediaCell = new MediaCell();
}
}).start();
return mediaCell;
}
});