我怎样才能改变javafx中特定celldata的风格?

时间:2015-07-05 22:51:32

标签: javafx tableview

晚上好, 我想知道,如何将背景颜色改为18岁以下的红色,是否可能? 我从星期一开始尝试解决这个问题。有人可以给我一些网站,而不是比oracle文档解释更好吗? 我看到很多人,还在使用挥杆,我应该继续学习javafx还是开始学习摇摆? obs:抱歉我的英语不好。

控制器

package tableview;

    import java.net.URL;
    import java.util.ResourceBundle;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.layout.AnchorPane;

    public class LayoutController implements Initializable {

        @Override
        public void initialize(URL url, ResourceBundle rb) {
            getPerson();
            columnFName.setCellValueFactory(celldata -> celldata.getValue().getfName());
            columnLName.setCellValueFactory(celldata -> celldata.getValue().getlName());
            columnAge.setCellValueFactory(celldata -> celldata.getValue().getAge());
            tableView.setItems(person);
        }    

        @FXML
        private AnchorPane layout;

        //TABLE
        @FXML
        private TableView<Person> tableView;
        @FXML
        private TableColumn<Person, String> columnLName;
        @FXML
        private TableColumn<Person, String> columnFName;
        @FXML
        private TableColumn<Person, Number> columnAge;
        //END

        ObservableList person = FXCollections.observableArrayList();
        ObservableList getPerson() {
            person.add(new Person("John", "Smith", 15));
            person.add(new Person("May", "Smith", 18));
            person.add(new Person("Sam", "Lucca", 21));
            person.add(new Person("Homer", "Simpson", 14));
            return person;
        }


    }

人类

package tableview;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class Person {
    private SimpleStringProperty fName, lName;
    private SimpleIntegerProperty age;

    public Person() {
        this("", "", 0);
    }

    public Person(String fName, String lName, int age) {
        this.fName = new SimpleStringProperty(fName);
        this.lName = new SimpleStringProperty(lName);
        this.age = new SimpleIntegerProperty(age);
    }

    public SimpleStringProperty getfName() {
        return fName;
    }

    public SimpleStringProperty getlName() {
        return lName;
    }

    public SimpleIntegerProperty getAge() {
        return age;
    }
}

1 个答案:

答案 0 :(得分:0)

在桌面上设置行工厂。您想要观察行的itemProperty。管理背景颜色的最佳方法是使用外部CSS文件并设置CSS伪类,如果该行所代表的人具有年龄&lt; 18.(您可以将此代码放在控制器的initialize()方法中。)

    PseudoClass minorPseudoClass = PseudoClass.getPseudoClass("minor");

    tableView.setRowFactory(tv -> {
        TableRow<Person> row =  new TableRow<>();
        row.itemProperty().addListener((obs, oldPerson, newPerson) -> {
            if (newPerson != null) {
                row.pseudoClassStateChanged(minorPseudoClass, newPerson.getAge() < 18);
            } else {
                row.pseudoClassStateChanged(minorPseudoClass, false);
            }
        });
        return row ;
    });

然后使用适合您创建的伪类的样式定义外部样式表:

.table-row-cell:minor {
    -fx-control-inner-background: red ;
    -fx-control-inner-background-alt: #cc0000 ;
}

请注意,这假定表格中每个人的年龄都是固定的。如果在显示人员时有可能更改,则需要在行内容更改时注册并取消注册具有age属性的侦听器:

    tableView.setRowFactory(tv -> {
        TableRow<Person> row =  new TableRow<>();
        ChangeListener<Number> ageListener = (obs, oldValue, newValue) -> {
            row.pseudoClassStateChanged(minorPseudoClass, newValue.intValue() < 18);
        };
        row.itemProperty().addListener((obs, oldPerson, newPerson) -> {
            if (oldPerson != null) {
                oldPerson.ageProperty().removeListener(ageListener);
            }
            if (newPerson != null) {
                newPerson.ageProperty().addListener(ageListener);
                row.pseudoClassStateChanged(minorPseudoClass, newPerson.getAge() < 18);
            } else {
                row.pseudoClassStateChanged(minorPseudoClass, false);
            }
        });
        return row ;
    });

这是一个SSCCE。这不使用FXML,但显然你可以做同样的事情,在控制器的rowFactory方法中创建initialize()

import java.util.function.Function;

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.css.PseudoClass;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class HighlightYoungPeopleTableExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Person> tableView = new TableView<>();
        TableColumn<Person, String> firstNameColumn = column("First Name", Person::firstNameProperty, 150);
        TableColumn<Person, String> lastNameColumn = column("Last Name", Person::lastNameProperty, 150);
        TableColumn<Person, Integer> ageColumn = column("Age", person -> person.ageProperty().asObject(), 50);

        PseudoClass minorPseudoClass = PseudoClass.getPseudoClass("minor");

        tableView.setRowFactory(tv -> {
            TableRow<Person> row =  new TableRow<>();
            ChangeListener<Number> ageListener = (obs, oldValue, newValue) -> {
                row.pseudoClassStateChanged(minorPseudoClass, newValue.intValue() < 18);
            };
            row.itemProperty().addListener((obs, oldPerson, newPerson) -> {
                if (oldPerson != null) {
                    oldPerson.ageProperty().removeListener(ageListener);
                }
                if (newPerson != null) {
                    newPerson.ageProperty().addListener(ageListener);
                    row.pseudoClassStateChanged(minorPseudoClass, newPerson.getAge() < 18);
                } else {
                    row.pseudoClassStateChanged(minorPseudoClass, false);
                }
            });
            return row ;
        });

        tableView.getColumns().add(firstNameColumn);
        tableView.getColumns().add(lastNameColumn);
        tableView.getColumns().add(ageColumn);

        tableView.getItems().addAll(
                new Person("John", "Smith", 15),
                new Person("May", "Smith", 18),
                new Person("Sam", "Lucca", 21),
                new Person("Homer", "Simpson", 14)        
        );

        Scene scene = new Scene(new BorderPane(tableView), 800, 600);
        scene.getStylesheets().add("highlight-young-people-table.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private <S,T> TableColumn<S,T> column(String title, Function<S, ObservableValue<T>> property, double width) {
        TableColumn<S,T> col = new TableColumn<>(title);
        col.setPrefWidth(width);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        return col ;
    }

    public static class Person {
        private final StringProperty firstName = new SimpleStringProperty() ;
        private final StringProperty lastName = new SimpleStringProperty() ;
        private final IntegerProperty age = new SimpleIntegerProperty();

        public Person(String firstName, String lastName, int age) {
            setFirstName(firstName);
            setLastName(lastName);
            setAge(age);
        }

        public final StringProperty firstNameProperty() {
            return this.firstName;
        }

        public final String getFirstName() {
            return this.firstNameProperty().get();
        }

        public final void setFirstName(final String firstName) {
            this.firstNameProperty().set(firstName);
        }

        public final StringProperty lastNameProperty() {
            return this.lastName;
        }

        public final String getLastName() {
            return this.lastNameProperty().get();
        }

        public final void setLastName(final String lastName) {
            this.lastNameProperty().set(lastName);
        }

        public final IntegerProperty ageProperty() {
            return this.age;
        }

        public final int getAge() {
            return this.ageProperty().get();
        }

        public final void setAge(final int age) {
            this.ageProperty().set(age);
        }


    }

    public static void main(String[] args) {
        launch(args);
    }


}

亮点,年轻人table.css

.table-row-cell:minor {
    -fx-control-inner-background: red ;
    -fx-control-inner-background-alt: #cc0000 ;
}