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
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);
}
});
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
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?
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With