Getting selected item from a JavaFX TableView

前端 未结 7 1577
轻奢々
轻奢々 2020-12-15 16:01

How do I get the selected item from a TableView in JavaFX?

I am currently using

ObservableList selectedItems = taview.getSelectionModel(         


        
相关标签:
7条回答
  • 2020-12-15 16:31

    @pranishres

    Here is my try for a cell selection change listener (even if row doesn’t change), inspired by @Java Man's code:

    tableView.getSelectionModel().setCellSelectionEnabled(true);
    ObservableList selectedCells = tableView.getSelectionModel().getSelectedCells();
    
    selectedCells.addListener(new ListChangeListener() {
        @Override
        public void onChanged(Change c) {
            TablePosition tablePosition = (TablePosition) selectedCells.get(0);
            Object val = tablePosition.getTableColumn().getCellData(tablePosition.getRow());
            System.out.println("Selected Value" + val);
        }
    });
    
    0 讨论(0)
  • 2020-12-15 16:32

    Ok, lets say you have a data model class named Person. This way:

    Person person = taview.getSelectionModel().getSelectedItem();
    System.out.println(person.getName());    
    

    Note that TableView must take a Person as a type argument to avoid casting:

    @FXML
    private TableView<Person> taview;
    

    or

    TableView<Person> taview = new TableView<>();
    

    when your row is selected, you will return one Person instance. Then do what ever you want with that instance.

    0 讨论(0)
  • 2020-12-15 16:32

    For One ItemSelection

    Product p = taview.getSelectionModel().getSelectedItem();
    System.out.println(p.getName()); 
    

    For Mulltiple ItemSelection

    ArrayList<Product> p = new ArrayList<>(taview.getSelectionModel().getSelectedItems());
            for (Product res : p) {     
                System.out.println(res.getName());      
              }
    
    0 讨论(0)
  • 2020-12-15 16:33
        tableview.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
        @Override
        public void changed(ObservableValue observableValue, Object oldValue, Object newValue) {
            //Check whether item is selected and set value of selected item to Label
            if(tableview.getSelectionModel().getSelectedItem() != null) 
            {    
               TableViewSelectionModel selectionModel = tableview.getSelectionModel();
               ObservableList selectedCells = selectionModel.getSelectedCells();
               TablePosition tablePosition = (TablePosition) selectedCells.get(0);
               Object val = tablePosition.getTableColumn().getCellData(newValue);
               System.out.println("Selected Value" + val);
             }
             }
         });
    

    Using this code you can get the selected value from JAVAFX TABLEVIEW Cell.

    Thanks..

    0 讨论(0)
  • 2020-12-15 16:38

    you can get data from tableview like that

    For One ItemSelection

    `tableview_obj.getSelectionModel().getSelectedItem().yourgetterobjectname`
    

    and Mulltiple Item Selection

    tableview_obj.getSelectionModel().getSelectedItems().get(indexNo).yourgetterobjectname

    0 讨论(0)
  • 2020-12-15 16:42

    you can use this method maybe it works for you

    ObservableList rowList = (ObservableList) tblView.getItems().get(getIndex());
    

    and for the call method will be like that

    String email = rowList.get(1).toString();
    String contact = rowList.get(2).toString();
    String name= rowList.get(0).toString();
    
    0 讨论(0)
提交回复
热议问题