Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read selection from TableView in JavaFX 2.0

I am trying to get the selection from a TableView in JavaFX 2.0. what happens is that I need to get the value of the row you selected in tableview I hope someone can help me

As would be placed on a table

I mean I want to get the data you select and if there is any way to handle an event to get the selected row automatically

like image 376
carson314 Avatar asked Nov 15 '12 07:11

carson314


4 Answers

you need ChangeListener and Clipboard to accomplish your task :)

Example Code :

Clipboard clipboard = Clipboard.getSystemClipboard(); 
  // add listner to your tableview selecteditemproperty   
userTable.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {
              // this method will be called whenever user selected row
            @override
             public void chnaged(ObservableValue observale, Object oldValue,Object newValue) {
               UserClass selectedUser = (UserClass)newValue;
             ClipboardContent content = new ClipboardContent();
                 // make sure you override toString in UserClass
             content.putString(selectedUser.toString()); 
             clipboard.setContent(content); 
            }
            });
like image 86
invariant Avatar answered Nov 05 '22 23:11

invariant


If i understood you correctly, you want to retrieve the row number of the cell that is currently selected inside a TableView.

To do this, request the SelectionModel of the TableView:

    // tv is of type TableView
    TableView.TableViewSelectionModel selectionModel = tv.getSelectionModel();
    ObservableList selectedCells = selectionModel.getSelectedCells();
    TablePosition tablePosition = (TablePosition) selectedCells.get(0);
    int row = tablePosition.getRow(); // yields the row that the currently selected cell is in
like image 21
zhujik Avatar answered Nov 06 '22 00:11

zhujik


It's still not clear to me what you are trying to do...

However, getting the selected row:

final Countries selectedCountry = tblCountries.getSelectionModel().getSelectedItem();

If there is a need that another pane becomes visible or another window to show just add an eventhandler to the onclicked property or such?

Is it that what you mean?

like image 4
Perneel Avatar answered Nov 06 '22 00:11

Perneel


    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..

like image 3
Java Man Avatar answered Nov 06 '22 00:11

Java Man