• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to select a row in a Jtable?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I'm retrieving data from a database, that i display in a jtable (the columns are id, name, age)using AbstractTableModel with an ArrayList .That's working fine.
My problem is that i want to select one row and put it in a class and i do it like that:
1/MyTableModel tm = (MyTableModel)getScrollPaneTable().getModel();
2/Person aPerson = (Person)tm.getElements().get(aRow);
The class Person contains 3 fields (id,name,age)
The method getElements returns the ArrayList that is used to displays rows in the table.
I have a ClassCastException on the second line and i don't khow how to deal with it.
Can anybody help me?
Thanks
Tina Sow
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, I have no idea what your custom TableModel is returning. But whatever it is, its not in the format of a Person class.
Perhaps you need to modify your custom TableModel to return Person objects by a specific method. Then you can call that method.
Currently it appears that your custom TableModel is returning an ArrayList but you are trying to assign it to a Person. This will produce a class case exception.
You need to either convert from what you receive to what you want, or modify what the tablemodel is returning.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be explicit about what you are casting.

What class does tm.getElements() return?
Bill
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic