Tomáš Čičman
Tomáš Čičman

Reputation: 75

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();

Upvotes: 1

Views: 9255

Answers (3)

Samuel Moshie
Samuel Moshie

Reputation: 570

In your declaration the TableView should be casted to your object eg: TableView <Stlpce> tableview_objednavka;

Upvotes: 0

Vikas Tiwari
Vikas Tiwari

Reputation: 367

If you only care about which row is selected, assuming you have a TableView, you can simply use:

List selected = selectionModel.getSelectedItems();

or if your table only allows single row selection:

SomeObject selected = selectionModel.getSelectedItem();

System.out.println(selected.getName());

Try it 100% working...

or try this for better understand Get row data from TableView

Upvotes: 3

doc
doc

Reputation: 817

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);
}

Upvotes: 1

Related Questions