JavaFX8: How to create listener for selection of row in Tableview?

为君一笑 提交于 2019-12-02 19:01:10

The selectedItem in the selection model is an observable property, so you should be able to achieve this with:

tableview1.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
    if (newSelection != null) {
        tableview2.getSelectionModel().clearSelection();
    }
});

tableview2.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
    if (newSelection != null) {
        tableview1.getSelectionModel().clearSelection();
    }
});

My solution would be creating custom cell factory for table and set it for each table columns.

Callback<TableColumn<..., ...>, TableCell<..., ...>> value = param -> {
                TextFieldTableCell cell = new TextFieldTableCell<>();
                cell.addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
                            //your code
                        }
                );
                return cell;
            };
            packageName.setCellFactory(value);

    table1.column1.setCellFactory();
    table2.column1.setCellFactory();
    ...
Sofiane Huckfinn Sellam

I use it for deleting the chosen row.

public void ButtonClicked()
{
  ObservableList<Names> row , allRows;
  allRows = table.getItems();
  row = table.getSelectionModel().getSelectedItems();         
  row.forEach(allRows::remove);
}

This question helped me but during experiment in javafx and jfoenix this also works for me.

deleteSingle.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {

            StringProperty selectedItem = table.getSelectionModel().getSelectedItem().getValue().link1;

            System.out.println("That is selected item : "+selectedItem);

            if (selectedItem.equals(null)) {

            System.out.println(" No item selected");


            } else {
                System.out.println("Index to be deleted:" + selectedItem.getValue());

                 //Here was my database data retrieving and selectd
                 // item deleted and then table refresh
                table.refresh();

                return;
            }

        });

In case you need not only the row, but the x|y position of the table cell, do this:

table.getFocusModel().focusedCellProperty().addListener(
        new ChangeListener<TablePosition>() {
    @Override
    public void changed(ObservableValue<? extends TablePosition> observable,
            TablePosition oldPos, TablePosition pos) {
        int row = pos.getRow();
        int column = pos.getColumn();
        String selectedValue = "";

        if (table.getItems().size() > row
                && table.getItems().get(row).size() > column) {
            selectedValue = table.getItems().get(row).get(column);
        }

        label.setText(selectedValue);
    }
});

In this example, I am using a "classic" TableView with List<String> as column model. And, of course, that label is just an example from my code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!