gpt4 book ai didi

JavaFX TableView 列宽\内容自动截断

转载 作者:搜寻专家 更新时间:2023-10-31 19:47:05 28 4
gpt4 key购买 nike

我正在尝试“巧妙地”缩写/截断嵌套在 TableView 中的列的 String 内容。我尝试过使用 UNCONSTRAINED_RESIZE_POLICY,但我更喜欢 CONSTRAINED_RESIZE_POLICY,因为它会根据窗口大小自动调整列的大小。

我要实现的目标:

String one = "SomeVeryLongString";

// Displayed in the column where user has shrunk it too narrow

+-------------+
| Some...ring |
+-------------+

// and if the user shrunk it a bit more

+-----------+
| Som...ing |
+-----------+

// and if they stretch it a bit

+---------------+
| SomeV...tring |
+---------------+

// etc...

我已经探索了读取 String 长度并进行我自己的截断的可能性,但是当用户收缩/拉伸(stretch) gui 时,不可能进行动态更新。我的直觉告诉我这必须通过 JavaFX 类 来完成,因为它会与 TableView 紧密相关,但是我还没有找到方法。

如何使用 JavaFX 完成此操作?

最佳答案

解决方案

标准的默认溢出行为TableCell是截断它的字符串并显示...在字符串的末尾表示字符串已被截断。全部Labeled JavaFX 中的控件以这种方式工作。

你问题中的例子有...字符串中间的省略号。为此,set the overrun style在由适当的细胞工厂生成的细胞上:

setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS);

也可以通过 setting a new ellipsis string 修改为省略号显示的文本字符串在由适当的细胞工厂生成的细胞上:

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用于以下示例应用程序。

  1. “名字”列省略了中间的文本并使用自定义省略号字符串 <--->
  2. “姓氏”列省略了中间的文本,但未提供自定义省略号字符串。
  3. 电子邮件列仅使用默认的超限策略和省略号字符串,其中放置了 ...超限字符串末尾的省略号。

elidedtableview

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);
}
}
}

关于JavaFX TableView 列宽\内容自动截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16728134/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com