我有一个基于libVLC的小型JavaFX媒体播放器。您可以在此处查看源代码:http://bitbucket.org/alibranic/vpfx
我想为当前播放媒体的行着色。媒体扩展文件。我尝试了很多东西,但我有很多想法。我不确定,这是最好的方法。
/**
* selects current playing item in playlist and scrolls to it. Called, when user clicks on Next-Button.
*/
private void selectListItem() {
Platform.runLater(new Runnable() {
@Override
public void run() {
playList.scrollTo(actualPlayingMediaIndex);
playList.getSelectionModel().select(actualPlayingMediaIndex);
// How can I colorize the actual playing media's row now?
}
});
}
playList.setRowFactory(new Callback<TableView<Media>, TableRow<Media>>() {
@Override
public TableRow<Media> call(TableView<Media> tableView) {
final TableRow<Media> row = new TableRow<>();
// do something...
return row;
}
});
}
正如你在gif中看到的那样:我点击播放并选择当前播放行。但是当我点击其他行时它就不再存在了。只要媒体播放,我想保留背景颜色。
答案 0 :(得分:0)
如果您希望表格自动更新,则需要一个属性。将actualPlayingMediaIndex
设为属性或添加Media类。
这是一种方法。
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TableStyle extends Application {
public static void main(String[] args) { launch(args); }
SimpleIntegerProperty actualPlayingMediaIndex = new SimpleIntegerProperty(-1);
@Override
public void start(Stage stage) {
ObservableList<Media> data = FXCollections.observableArrayList();
for (int i = 0;i<100;i++) data.add(new Media("name "+i,"info "+i));
TableView<Media> tv = new TableView<>(data);
tv.setEditable(true);
tv.getColumns().addAll(
new TableColumn<>("name"),
new TableColumn<>("info")
);
for(TableColumn tc:tv.getColumns()){
tc.setCellValueFactory(new PropertyValueFactory<>(tc.getText()));
tc.setCellFactory(TextFieldTableCell.forTableColumn());
tc.setPrefWidth(100);
}
tv.setRowFactory(p -> new TableRow<Media>() {
@Override protected void updateItem(Media item, boolean empty) {
super.updateItem(item, empty);
styleProperty().bind(Bindings.createStringBinding(() ->
getIndex() == actualPlayingMediaIndex.get()
? "-fx-background-color: #ee9999;"
: ""
, actualPlayingMediaIndex));
}
});
TextField tf = new TextField();
tf.setPromptText("enter 0 - 99");
tf.setOnAction(e -> {
actualPlayingMediaIndex.set(Integer.parseInt(tf.getText()));
tv.scrollTo(actualPlayingMediaIndex.get());
tv.getSelectionModel().select(actualPlayingMediaIndex.get());
});
Scene scene = new Scene(new VBox(tv,tf));
//scene.getStylesheets().add("tablestyle/cells.css");
stage.setScene(scene);
stage.show();
}
public class Media {
private final StringProperty name = new SimpleStringProperty();
private final StringProperty info = new SimpleStringProperty();
public StringProperty nameProperty(){return name;}
public StringProperty infoProperty(){return info;}
public Media(String nm, String inf) {name.set(nm); info.set(inf);}
}
}