Reputation: 347
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
Upvotes: 10
Views: 14703
Reputation: 1860
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..
Upvotes: 3
Reputation: 8900
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);
}
});
Upvotes: 9
Reputation: 3367
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?
Upvotes: 4
Reputation: 6574
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
Upvotes: 6