JavaFx如何在TableView中设置特定行的行背景色
我使用的TabeView有两列:JavaFx如何在TableView中设置特定行的行背景色,java,javafx,Java,Javafx,我使用的TabeView有两列: @FXML private TableView<Person> personTable; @FXML private TableColumn<Person, String> firstNameColumn; @FXML private TableColumn<Person, String> lastNameColumn; public class Person { private final StringProp
@FXML
private TableView<Person> personTable;
@FXML
private TableColumn<Person, String> firstNameColumn;
@FXML
private TableColumn<Person, String> lastNameColumn;
public class Person {
private final StringProperty firstName;
private final StringProperty lastName;
/**
* Default constructor.
*/
public Person() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param firstName
* @param lastName
*/
public Person(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
}
@FXML
个人桌面视图;
@FXML
private TableColumn firstNameColumn;
@FXML
私有TableColumn lastNameColumn;
公共阶层人士{
私有财产名;
私有财产姓氏;
/**
*默认构造函数。
*/
公众人士(){
这个(空,空);
}
/**
*具有一些初始数据的构造函数。
*
*@param firstName
*@param lastName
*/
公众人物(字符串名、字符串名){
this.firstName=新的SimpleStringProperty(firstName);
this.lastName=新的SimpleStringProperty(lastName);
}
公共字符串getFirstName(){
返回firstName.get();
}
public void setFirstName(字符串firstName){
this.firstName.set(firstName);
}
public StringProperty firstNameProperty(){
返回名字;
}
公共字符串getLastName(){
返回lastName.get();
}
public void setLastName(字符串lastName){
this.lastName.set(lastName);
}
公共StringProperty lastNameProperty(){
返回姓氏;
}
}
我有一个计算行数的逻辑,我需要将这一行的背景设置为红色(每隔几秒钟我计算这一行,我需要将caculcated行背景设置为红色)
我检查那些问题:
但它确实有帮助。
因此,我可以设置一个随机行,它是背景色吗?根据您的具体需要,您可以执行以下操作:
ObjectProperty<Person> criticalPerson = new SimpleObjectProperty<>();
personTable.setRowFactory(tv -> {
TableRow<Person> row = new TableRow<>();
BooleanBinding critical = row.itemProperty().isEqualTo(criticalPerson);
row.styleProperty().bind(Bindings.when(critical)
.then("-fx-background-color: red ;")
.otherwise(""));
return row ;
});
ObjectProperty criticalPerson=new SimpleObjectProperty();
personTable.setRowFactory(电视->{
TableRow行=新TableRow();
BooleanBinding critical=row.itemProperty().isEqualTo(criticalPerson);
row.styleProperty().bind(Bindings.when(临界)
。然后(“-fx背景色:红色;”)
。否则以““””号填列;
返回行;
});
以下是SSCCE:
import java.util.function.Function;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class HighlightTableRows extends Application {
@Override
public void start(Stage primaryStage) {
TableView<Person> table = new TableView<>();
table.getColumns().add(column("First Name", Person::firstNameProperty));
table.getColumns().add(column("Last Name", Person::lastNameProperty));
for (int i = 1 ; i <=50 ; i++) {
table.getItems().add(new Person("Person"+i, "McPerson"+i));
}
ObjectProperty<Person> criticalPerson = new SimpleObjectProperty<>();
table.setRowFactory(tv -> {
TableRow<Person> row = new TableRow<>();
BooleanBinding critical = row.itemProperty().isEqualTo(criticalPerson).and(row.itemProperty().isNotNull());
row.styleProperty().bind(Bindings.when(critical)
.then("-fx-background-color: red ;")
.otherwise(""));
return row ;
});
BorderPane root = new BorderPane(table);
Button apply = new Button("Make critical");
apply.setOnAction(e -> criticalPerson.set(table.getSelectionModel().getSelectedItem()));
apply.disableProperty().bind(table.getSelectionModel().selectedItemProperty().isNull());
Button clear = new Button("Clear");
clear.setOnAction(e -> criticalPerson.set(null));
HBox controls = new HBox(5, apply, clear);
controls.setAlignment(Pos.CENTER);
controls.setPadding(new Insets(5));
root.setBottom(controls);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private <S,T> TableColumn<S,T> column(String text, Function<S, ObservableValue<T>> prop) {
TableColumn<S,T> col = new TableColumn<>(text);
col.setCellValueFactory(cellData -> prop.apply(cellData.getValue()));
return col ;
}
public class Person {
private final StringProperty firstName;
private final StringProperty lastName;
/**
* Default constructor.
*/
public Person() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param firstName
* @param lastName
*/
public Person(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
}
public static void main(String[] args) {
launch(args);
}
}
import java.util.function.function;
导入javafx.application.application;
导入javafx.beans.binding.Bindings;
导入javafx.beans.binding.BooleanBinding;
导入javafx.beans.property.ObjectProperty;
导入javafx.beans.property.SimpleObject属性;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.property.StringProperty;
导入javafx.beans.value.observeValue;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.Button;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableRow;
导入javafx.scene.control.TableView;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.HBox;
导入javafx.stage.stage;
公共类HighlightTableRows扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
TableView table=新TableView();
table.getColumns().add(column(“First Name”,Person::firstNameProperty));
table.getColumns().add(列(“姓氏”,Person::lastNameProperty));
对于(int i=1;i{
TableRow行=新TableRow();
BooleanBinding critical=row.itemProperty().isEqualTo(criticalPerson).和(row.itemProperty().isNotNull());
row.styleProperty().bind(Bindings.when(临界)
。然后(“-fx背景色:红色;”)
。否则以““””号填列;
返回行;
});
BorderPane根=新的BorderPane(表);
按钮应用=新按钮(“使关键”);
apply.setOnAction(e->criticalPerson.set(table.getSelectionModel().getSelectedItem());
apply.disableProperty().bind(table.getSelectionModel().SelectEditeProperty().isNull());
按钮清除=新按钮(“清除”);
clear.setOnAction(e->criticalPerson.set(null));
HBox控制=新的HBox(5,应用,清除);
控件。设置对齐(位置中心);
控件。设置填充(新插图(5));
根。setBottom(对照);
场景=新场景(根);
初级阶段。场景(场景);
primaryStage.show();
}
私有表列(字符串文本、函数属性){
TableColumn col=新的TableColumn(文本);
col.setCellValueFactory(cellData->prop.apply(cellData.getValue());
返回列;
}
公共阶层人士{
私有财产名;
私有财产姓氏;
/**
*默认构造函数。
*/
公众人士(){
这个(空,空);
}
/**
*具有一些初始数据的构造函数。
*
*@param firstName
*@param lastName
*/
公众人物(字符串名、字符串名){
this.firstName=新的SimpleStringProperty(firstName);
this.lastName=新的SimpleStringProperty(lastName);
}
公共字符串getFirstName(){
返回firstName.get();
}
public void setFirstName(字符串firstName){
this.firstName.set(firstName);
}
public StringProperty firstNameProperty(){
返回名字;
}
公共字符串getLastName(){
返回lastName.get();
}
public void setLastName(字符串lastName){
this.lastName.set(lastName);
}
公共StringProperty lastNameProperty(){
返回姓氏;
}
}
公共静态void main(字符串[]args){
发射(args);
}
}
如果多行同时需要红色,则使用<代码>观察员> <代码>进行明显的修改,其中包含行应该是红色的项等。还可以考虑向模型类添加“<代码> BooLoeAuthor <代码>,并让表行观察它。
< P>根据您所需要的,您可以这样做。:ObjectProperty<Person> criticalPerson = new SimpleObjectProperty<>();
personTable.setRowFactory(tv -> {
TableRow<Person> row = new TableRow<>();
BooleanBinding critical = row.itemProperty().isEqualTo(criticalPerson);
row.styleProperty().bind(Bindings.when(critical)
.then("-fx-background-color: red ;")
.otherwise(""));
return row ;
});
ObjectProperty criticalPerson=new SimpleObjectProperty();
personTable.setRowF