解决方案
标准TableCell的默认溢出行为是截断它的字符串并显示...
在字符串的末尾以指示字符串已被截断。JavaFX 中的所有带标签的控件都以这种方式工作。
您问题中的示例在...
字符串中间有省略号。为此,请在相应单元工厂生成的单元上设置溢出样式:
setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS);
为省略号显示的文本字符串也可以通过在适当的单元格工厂生成的单元格上设置新的省略号字符串来修改:
setEllipsisString(ellipsisString);
演示这一点的简单表格单元格是:
class CenteredOverrunTableCell extends TableCell<Person, String> {
public CenteredOverrunTableCell() {
this(null);
}
public CenteredOverrunTableCell(String ellipsisString) {
super();
setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS);
if (ellipsisString != null) {
setEllipsisString(ellipsisString);
}
}
@Override protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(item == null ? "" : item);
}
}
示例应用程序
CenteredOverrunTableCell
用于以下示例应用程序 。
- First Name 列省略了中心的文本,并使用自定义省略号字符串
<--->
- 姓氏列省略了中心的文本,但不提供自定义省略号字符串。
- 电子邮件列仅使用默认的溢出策略和省略号字符串,将
...
省略号放置在溢出字符串的末尾。
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
public class ElidedTableViewSample extends Application {
private 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("Ethangorovichswavlowskikaliantayaprodoralisk", "Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch", "ethan@llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch.com"),
new Person("Emma", "Jones", "emma.jones@example.com"),
new Person("Michael", "Brown", "michael.brown@example.com")
);
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) {
stage.setTitle("Table View Sample");
stage.setWidth(470);
stage.setHeight(500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName"));
firstNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
@Override public TableCell<Person, String> call(TableColumn<Person, String> p) {
return new CenteredOverrunTableCell("<--->");
}
});
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));
lastNameCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
@Override public TableCell<Person, String> call(TableColumn<Person, String> p) {
return new CenteredOverrunTableCell();
}
});
TableColumn emailCol = new TableColumn("Email");
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));
table.setItems(data);
table.getColumns().addAll(
firstNameCol,
lastNameCol,
emailCol
);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10));
vbox.getChildren().addAll(label, table);
table.setColumnResizePolicy(
TableView.CONSTRAINED_RESIZE_POLICY
);
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
}
class CenteredOverrunTableCell extends TableCell<Person, String> {
public CenteredOverrunTableCell() {
this(null);
}
public CenteredOverrunTableCell(String ellipsisString) {
super();
setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS);
if (ellipsisString != null) {
setEllipsisString(ellipsisString);
}
}
@Override protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(item == null ? "" : item);
}
}
public static class Person {
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;
private Person(String fName, String lName, String email) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String fName) {
firstName.set(fName);
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String fName) {
lastName.set(fName);
}
public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
}
}