Uran_K Uran_K - 16 days ago 9
Java Question

TableCell hyperlink not aligning correctly on TableView - JavaFX

I have this problem with the first

Hyperlink
being alignment on top of
TableCell
.

I have tried almost everything and I could not get it to work.

IMAGE

colData.setCellFactory(e -> {
return new TableCell<TabelaShitjet, Hyperlink>(){
@Override
protected void updateItem(Hyperlink item, boolean empty) {
super.updateItem(item, empty);
if (!empty){
item.setOnAction(e -> {
TeDhenatBlerjes(Integer.parseInt(getTableView().getColumns().get(0).getCellData(getTableRow().getIndex())+""), item.getText());
});
setGraphic(item);
}
}
};
});


CONSTRUCTOR

public class TabelaShitjet {
private Hyperlink data;

public TabelaShitjet(String data){
this.data = new Hyperlink(data);
}

public Hyperlink getData() {
return data;
}

public void setData(Hyperlink data) {
this.data = data;
}
}

Answer

I've got no idea why exactly this happens, but if you remove the graphic when cell becomes empty by setting it to null, the problem seems to be fixed.

You should undo any modifications done to a cell when a item is added on a call of updateItem where the cell becomes empty anyway, since otherwise empty cells could be shown as if they were non-empty:

@Override
protected void updateItem(Hyperlink item, boolean empty) {
    super.updateItem(item, empty);
    if (!empty){
        item.setOnAction(e -> {
            TeDhenatBlerjes(Integer.parseInt(getTableView().getColumns().get(0).getCellData(getTableRow().getIndex())+""), item.getText());
        });
    }

    // set graphic every time i.e. set it to null for empty cells
    setGraphic(item);
}

In general the updateItem method should be implemented like this:

@Override
protected void updateItem(ItemType item, boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
        // undo any updates that could have been made
        // to make the cell look different from the empty cell
    } else {
        // update cell to display item
    }
}