Question

I have implemented a custom TableCell & TableColumn to show a hyperlink while the cell is not editing. I want to add setOnAction event for the hyperlink. As i want to reuse the TableCell i cannot add the code in TableCell updateItem method. Is there any way to implement this?

public class TableColumnHyperlink<S> extends TableColumn<S, String> {

    public TableColumnHyperlink (String header) {
        super(header);
        Callback<TableColumn<S, String>, TableCell<S, String>> hypCellFactory =
            (TableColumn<S, String> p) -> new TableCellHyperlink();

        setCellFactory(hypCellFactory);
    }
}

And the TableCell implementation is

public class TableCellHyperlink<S> extends TableCell<S, String> {

    private final TextField textField;
    private final Hyperlink hyperlink;

    public TableCellHyperlink() {

        textField = new TextField();
        hyperlink = new Hyperlink();
        setAlignment(Pos.CENTER_LEFT);
    }

    @Override
    public void startEdit() {
        super.startEdit();
        createTextField();
        setText(null);
        setGraphic(textField);
        textField.requestFocus();
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();
        setText(getItem());
        setGraphic(null);
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty && (getTableRow() == null ? true : getTableRow().isEmpty()));
        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if(isEditing()) {
                setText(getString());
                setGraphic(textField);
            } else {
                setText(null);
                hyperlink.setText(getString());
                setGraphic(hyperlink);
            }
        }
    }


    private void createTextField() {
        textField.setText(getString());
        textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);

        textField.addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent t) -> {
            if (t.getCode() == KeyCode.ENTER) {
                commitEdit(textField.getText());
            } else if (t.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            }
        });
    }

    private String getString() {
        return (getItem() != null)?getItem():"";
    }
}

enter image description here

Était-ce utile?

La solution

If the event handler implementation will vary by instance, you need to pass the event handler (or a function) into the constructor. Since you probably need to access the cell, you'll do something like

public class TableCellHyperlink<S> extends TableCell<S, String> {

    private final TextField textField;
    private final Hyperlink hyperlink;


    public TableCellHyperlink(Consumer<TableCellHyperlink<S> handlerFunction) {

        textField = new TextField();
        hyperlink = new Hyperlink();
        hyperlink.setOnAction(event -> handlerFunction.accept(this));
        setAlignment(Pos.CENTER_LEFT);
    }

    // ...
}

Now you can do something like

TableCellHyperlink<MyType> hyperlinkCell = new TableCellHyperlink<>(cell -> {
    MyType rowValue = (MyType) cell.getTableRow().getValue(); // getTableRow() returns TableRow, not TableRow<MyType>
    String cellValue = cell.getItem();
    // implement action
});

Obviously you can move the parameter up and pass it into the custom TableColumn constructor if you like.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top