ソリューション
標準TableCellのデフォルトオーバーラン動作では、それは文字列が切り捨てられたことを示すために、文字列の末尾の文字列と表示...
だ切り捨てることです。 JavaFXのすべてのLabeledコントロールはこのように動作します。
ご質問の例では、文字列の中央に楕円記号(...
)があります。それを達成するために、適切な細胞工場によって生成されたセルのset the overrun style:
setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS);
はellipsisesに表示されるテキスト文字列は、適切な細胞工場によって生成されたセルにsetting a new ellipsis stringによってmodifedすることができます。
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
は、次のサンプルアプリケーションで使用されます。
- 姓の欄には、中央にテキストをelidesと姓の列が中央にテキストをelidesが、何のカスタム省略記号文字列を提供しない
<--->
- のカスタム省略記号文字列を使用しています。
- 電子メール列は、デフォルトのオーバーランポリシーと、オーバーラン文字列の末尾に省略記号
...
を配置する省略記号文字列を使用します。
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", "[email protected]"),
new Person("Isabella", "Johnson", "[email protected]"),
new Person("Ethangorovichswavlowskikaliantayaprodoralisk", "Llanfairpwllgwyngyllgogerychwyrndrobwyll-llantysiliogogogoch", "[email protected]ch.com"),
new Person("Emma", "Jones", "[email protected]"),
new Person("Michael", "Brown", "[email protected]")
);
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);
}
}
}
'setTextOverrunは()'私が探していたまさにです!非常によく一緒に答えをありがとう、ありがとう!もし可能なら+10! – SnakeDoc