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

前端 未结 5 1344
广开言路
广开言路 2020-12-24 05:56

I currently have two tableviews in one screen, which results in both TableViews have rows which the user can select.

Now I want only one row to be selected at the sa

相关标签:
5条回答
  • 2020-12-24 06:31

    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.

    0 讨论(0)
  • 2020-12-24 06:33

    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();
        }
    });
    
    0 讨论(0)
  • 2020-12-24 06:36

    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;
                }
    
            });
    
    0 讨论(0)
  • 2020-12-24 06:51

    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();
        ...
    
    0 讨论(0)
  • 2020-12-24 06:51

    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);
    }
    
    0 讨论(0)
提交回复
热议问题