Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to select a row in a Jtable?

 
tina sow
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Bill Liteplo
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be explicit about what you are casting.

What class does tm.getElements() return?
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!