ImRaphael
ImRaphael

Reputation: 206

How to get tableview item on hyperlink click

I have following problem in javafx tableview, my table view is connected to certain model, and normal CRUD operations work with no issues, I also added a column which is not connected to ANY data model, and it just contains a hyperlink on which user can click, and he is prompted with a pop up.

All of this work, what it does not work is, when I click on hyperlink I want to also pass the row value, and normally it works like

tableview.getSelectionModel().getSelectedItem();

But now it does not work, since i am not clicking directly a cell, but I click a hyperlink, and if I click first some row and then hyperlink, I get that row that I highlighted. Is there any way to select row when clicking hyperlink,so I don't have to first click row then hyperlink in same row.

public class RemoveCell<T> extends TableCell<T, Void> {

    private final Hyperlink link;
    private final Hyperlink link1;
    private final HBox pane = new HBox();

    public RemoveCell() {
        link = new Hyperlink("Remove");
        link1 = new Hyperlink("Edit");
        pane.getChildren().addAll(link,link1);
        link1.setOnAction(evt -> {
            //lagerRet();
            if(tableView.getSelectionModel().getSelectedItem()!=null) {
                System.out.println("not null");
            }
            else {
                System.out.println("null");
            }
            // remove row item from tableview

           // ap.getChildren().removeAll();
            //ap.getChildren().setAll(mcon.loadParent(FxmlView.CHART));
            PopOver popsy = new PopOver();
            try {
                popsy.setContentNode(control.loadUni(FxmlView.POP));

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //popsy.setContentNode(panemain);
            popsy.headerAlwaysVisibleProperty().set(true);;
            popsy.show(link);


        });
        link.setOnAction(evt -> {
            // remove row item from tableview
            System.out.println("a quick test");
        });


    }

    @Override
    protected void updateItem(Void item, boolean empty) {
        super.updateItem(item, empty);

        setGraphic(empty ? null : pane );
        //setGraphic(empty ? null : link1 );


    }

}

and lastly this how I populate column

testColumn.setCellFactory(tc -> new RemoveCell<>());

Upvotes: 0

Views: 211

Answers (1)

fabian
fabian

Reputation: 82461

It's possible to access the row item via TableRow containing the TableCell.

T item = getTableRow().getItem();

It's also possible to get the index in TableView.items using TableCell.getIndex which allows for removal without searching for the item in the list first.

int itemIndex = getIndex();
getTableView().getItems().remove(itemIndex);

Upvotes: 1

Related Questions