是否可以在JavaFx中有选择地设置TableCell的内容样式?

时间:2015-03-28 17:52:34

标签: java css javafx javafx-8

我有一个TableView,其内容需要使用不同的颜色设置样式,是否可以在JavaFX中实现相同的效果。

这是一个示例程序

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewSample extends Application
{

    private final TableView<Person> table = new TableView<>();
    private final ObservableList<Person> data = FXCollections.observableArrayList(new Person("Jacob Smith", "jacob.smith@example.com"), new Person(
        "Isabella Johnson", "isabella.johnson@example.com"), new Person("Ethan Williams", "ethan.williams@example.com"), new Person("Emma Jones",
        "emma.jones@example.com"), new Person("Michael Brown", "michael.brown@example.com"));

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

    @Override
    public void start(final Stage stage)
    {
        final Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(450);
        stage.setHeight(500);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        table.setEditable(true);

        final TableColumn nameCol = new TableColumn("First Name");
        nameCol.setMinWidth(200);
        nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));

        final TableColumn emailCol = new TableColumn("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));

        table.setItems(data);
        table.getColumns().addAll(nameCol, emailCol);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    public static class Person
    {

        private final SimpleStringProperty name;
        private final SimpleStringProperty email;

        private Person(final String fName, final String email)
        {
            this.name = new SimpleStringProperty(fName);
            this.email = new SimpleStringProperty(email);
        }

        public String getName()
        {
            return name.get();
        }

        public void setName(final String fName)
        {
            name.set(fName);
        }

        public String getEmail()
        {
            return email.get();
        }

        public void setEmail(final String fName)
        {
            email.set(fName);
        }
    }
}

上述程序的输出

enter image description here

是否可以使用与名字不同的颜色设置姓氏?

1 个答案:

答案 0 :(得分:2)

您可以实现这样的自定义单元格工厂:

nameCol.setCellFactory(column -> {
            return new TableCell<Person, String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);

                    if (item == null || empty) {

                        setText(null);
                        setStyle("");

                    } else {

                        Text text1 = new Text( item.substring(0, item.indexOf(" ")));
                        text1.setFill(Color.RED);
                        text1.setFont(Font.font("Helvetica", FontPosture.ITALIC, 12));
                        Text text2 = new Text( item.substring(item.indexOf(" ")));
                        text2.setFill(Color.BLUE);
                        text2.setFont(Font.font("Helvetica", FontWeight.BOLD, 12));
                        TextFlow textFlow = new TextFlow(text1, text2);

                        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                        setGraphic(textFlow);
                        setPrefHeight(20);

                    }
                }
            };
        });