Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

tableview - Cell factory in javafx

I am using JavaFx 2.0 and Java 7. The question is regarding Table View in JavaFX.

The below sample code creates a firstName column and assigns cell factory and cell value factory to it.

Callback<TableColumn, TableCell> cellFactory = 
new Callback<TableColumn, TableCell>() {
    public TableCell call(TableColumn p) {
        return new EditingCell();
} };


TableColumn firstNameCol = new TableColumn("First Name");
    firstNameCol.setCellValueFactory(
        new PropertyValueFactory<Person,String>("firstName")
    );
firstNameCol.setCellFactory(cellFactory);

My requirement is I have a column which doesn't directly map to a single attribute in Person object, but instead is a custom value created by concatenating one or more attributes of Person object.

Consider a scenario where I have a table column named Full Name which will have values of Prefix + Last Name + "," + First Name .

1) In this scenario, how will you write the cell value factory?

firstNameCol.setCellValueFactory(
            new PropertyValueFactory<Person,String>(???????)
        );

2) how will you write cell factory?

In this scenario do we need to implement both cell value factory and cell factory or any one is sufficient? If one is sufficient then which one?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Straightforwardly,

Cell Value Factory : it is like a "toString()" of only part of the row item for that related cell.
Cell Factory : it is a renderer of the cell from the cell item. Default behavior is setText(cell.item.toString()) if the cell item is not a Node, setGraphic((Node)cell.item) otherwise. Set this property if the cell is supposed to support editing OR if you want more graphics (controls) other than default Label.

So for your scenario, leaving cell factory with default value will be sufficient (2). And here is sample code for (1):

firstAndLastNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person, String>, ObservableValue<String>>() {

    @Override
    public ObservableValue<String> call(TableColumn.CellDataFeatures<Person, String> p) {
        if (p.getValue() != null) {
            return new SimpleStringProperty(p.getValue().getPrefix() + " " + p.getValue().getFirstName() + "," + p.getValue().getLastName());
        } else {
            return new SimpleStringProperty("<no name>");
        }
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...