Tomáš Čičman Tomáš Čičman - 2 years ago 137
Java Question

JavaFX How to get data of selected row from Tableview

I would like to get data from one selected row

I have this little code

Stlpce aktualne = (Stlpce) tableview_objednavka.getSelectionModel().getSelectedItems();
double aktcena = aktualne.getCena();


But When I run the app I get this error

Caused by: java.lang.ClassCastException: javafx.scene.control.TableView$TableViewArrayListSelectionModel$5 cannot be cast to sample.Stlpce


I use scene builder for create TableView.

Can you help me?

This is solved - the problem was, that I have
getSelectedItems();
instead of
getSelectedItem();

doc doc
Answer Source

The exception is clear in its meaning: http://docs.oracle.com/javase/7/docs/api/java/lang/ClassCastException.html

The method call you make (https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TableView.TableViewSelectionModel.html#getSelectedItems--) returns a reference to an

ObvservableList<T>

Object, which Stlpe does not implement/extend and therefore a Stlpe reference cannot point to such an Object.

Is the Stlpe class the Type class of your ObservableList? If so, maybe you need to find the Stlpe Object in your list:

if (returnedList.size() > 0) {
    Stlpe item = returnedList.get(0);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download