在JavaFX TableView可编辑TextFieldTableCell中包装文本 [英] Wrapping text in a JavaFX TableView editable TextFieldTableCell

查看:178
本文介绍了在JavaFX TableView可编辑TextFieldTableCell中包装文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的TableView由几列组成,并使用以下单元工厂包装文本:

My TableView consists of several columns and wraps the text with the following cellfactory:

private void setWrapCellFactory(TableColumn<CustomObject, String> table) {
    table.setCellFactory(tablecol -> {
        TableCell<CustomObject, String> cell = new TableCell<>();
        Text text = new Text();
        cell.setGraphic(text);
        text.wrappingWidthProperty().bind(cell.widthProperty());
        text.textProperty().bind(cell.itemProperty());
        return cell;
    });
}

这非常适合显示无法编辑的文本.

This works perfectly for showing uneditable text.

最后一列必须是可编辑的,并且可以跨越几行(而不是TableView行).为了防止出现省略号,我想将新编辑的文本换行.

The last column has to be editable and could span across several rows (not TableView rows). To prevent the ellips I would like to wrap the new edited text.

经过数小时的尝试,我似乎仍然无法正常工作.我有以下代码来编辑单元格,并且无法对其进行包装.

After hours of trying I still can't seem to get it working. I have the following code to edit my cell and a non-working attempt to wrap it.

非工作方式来包装我编辑过的文本:

Non-working method to wrap my edited text:

private void setWrapCellFactoryEditable(TableColumn<CustomObject, String> table) {
    table.setCellFactory(tablecol -> {
        TableCell<CustomObject, String> cell = new TextFieldTableCell<>(new DefaultStringConverter());
        Text text = new Text();
        text.setText(text.toString());
        text.wrappingWidthProperty().bind(cell.widthProperty());
        return cell;
    });
}

以下代码是列设置:

feedbackCol.setCellValueFactory(ev -> new ReadOnlyStringWrapper(ev.getValue().getLastFeedback()));
    setWrapCellFactoryEditable(feedbackCol);
    feedbackCol.setOnEditCommit((CellEditEvent<CustomObject, String> ev) -> {
        int id = ((CustomObject) ev.getTableView().getItems().get(
                ev.getTablePosition().getRow())).getId();
        dc.addTempFeedback(id, ev.getNewValue());
    });

对于常规的不可编辑文本,我使用的是TableCell,对于可编辑的文本,我使用的是TextFieldTableCell.我不知道为什么包装不能在可编辑单元格中使用.

For the regular non-editable text I'm using a TableCell, for editable text I'm using a TextFieldTableCell. I don't know why the wrapping isn't working with editable cells.

推荐答案

您需要创建自己的扩展TextFieldTableCell的类.编辑完成后,它应覆盖cancelEdit()updateItem()方法,以将单元格图形更改为Text组件(等于用于不可编辑单元格的组件).

You need to create your own class extending TextFieldTableCell. It should override cancelEdit() and updateItem() methods to change cell graphic to Text component (equal to one you used for non-editable cells) when editing is finished.

public class WrappingTextFieldTableCell<S> extends TextFieldTableCell<S, String> {

    private final Text cellText;

    public WrappingTextFieldTableCell() {
        super(new DefaultStringConverter());
        this.cellText = createText();
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();
        setGraphic(cellText);
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (!isEmpty() && !isEditing()) {
            setGraphic(cellText);
        }
    }

    private Text createText() {
        Text text = new Text();
        text.wrappingWidthProperty().bind(widthProperty());
        text.textProperty().bind(itemProperty());
        return text;
    }
}

这篇关于在JavaFX TableView可编辑TextFieldTableCell中包装文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆