I need to have TableView CELL should display text in multi line in single cell with multi color.
In CELL I am displaying Multiline text using \"\\n\" currently. But
You can use a custom cell factory in this case, here is an example:
TableView
holding two TableColumns
, one for the fullname
and the second for the address
of a Person
instance.I've then set the cellFactory
as follows:
addressCol.setCellFactory(column->{
return new TableCell() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if(item==null || empty) {
setGraphic(null);
} else {
VBox vbox = new VBox();
List textList = Arrays.asList(item.split("\n"));
String[] colors = {"#3E50B4", "#FF3F80", "#727272"};
int colorCount = colors.length;
for(int i=0;i
Now this is just an example where I've used a VBox
containing a bunch of Label
instances (one for each line), and I've hard coded the colors, you can use whatever you like, for example, you can try a TextFlow
with Text
nodes and you can also use CSS Style classes.
Here is the full example code:
TableViewExample.fxml
Person.java
public class Person {
private String fullname, address;
public Person() {}
public Person(String fullname, String address) {
this.fullname = fullname;
this.address = address;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
MainApp.java
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class MainApp extends Application implements Initializable {
@FXML private TableView personTable;
@FXML private TableColumn fullnameCol, addressCol;
ObservableList persons = FXCollections.observableArrayList();
public static void main(String [] args) {
launch(args);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
Person p1 = new Person("John Doe", "Charlotte ,\n403 St. Tryon Street");
Person p2 = new Person("Riyad Mahrez", "xxxxx, \n007 St.YYYY");
persons.addAll(p1, p2);
fullnameCol.setCellValueFactory(new PropertyValueFactory("fullname"));
addressCol.setCellValueFactory(new PropertyValueFactory("address"));
addressCol.setCellFactory(column->{
return new TableCell() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if(item==null || empty) {
setGraphic(null);
} else {
VBox vbox = new VBox();
List textList = Arrays.asList(item.split("\n"));
String[] colors = {"#3E50B4", "#FF3F80", "#727272"};
int colorCount = colors.length;
for(int i=0;i
I hope this helps...