1
У меня есть TableView, где содержимое должно быть оформлено с использованием разных цветов, возможно ли добиться этого в JavaFX.Можно ли выборочно стилизовать содержимое TableCell в 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", "[email protected]"), new Person(
"Isabella Johnson", "[email protected]"), new Person("Ethan Williams", "[email protected]"), new Person("Emma Jones",
"[email protected]"), new Person("Michael Brown", "[email protected]"));
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);
}
}
}
Выход выше программы
Можно ли стиль фамилию с другим цветом, чем первое имя?