• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieving all table items

 
Amy Phillips
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have created an empty JTable and a JTree of objects, when the user clicks one of the tree objects a new line appears on the table with the object details. Once the user has finished selecting objects they click a button. I then need to create vector or Arraylist of objects which are in the table - at the moment I have no idea how I do this any ideas?
Thanks
Amy
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are a few possiblities:
(1) Invoke getValueAt(int row, int col) on your table to fetch the objects one at a time.
(2) Do something like:
DefaultTableModel m = (DefaultTableModel) myTable.getModel();
Vector v = m.getDataVector();
Vector v now contains one vector of objects for each row in your table.
(3) Build your own table model by subclassing AbstractTableModel. Have this table model store the data in a way that is convenient for you. You can then build your table with something like:
TableModel model = new MyTableModel();
JTable table = new JTable(model);
 
Amy Phillips
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions Steve. I have been trying to implement the getValueAt and have it return an object which is really a test object (i.e. the item put into the selected cell was a Test object) but it comes out as an object and I am seemingly unable to convert it. This would be a problem if anyone knows how I can pass this object to another class and retrieve the matching Test object from an ArrayList? at the moment I have

Otherwise I have been trying to implement my own table model to return a Test from the getValueAt and this is going a little better but I am having troubles implementing an addRow() and a removeRow() - these were orginally handled by the DefaultTableModel and I can't seem to find anything on how I could implement this functionality through the AbstractTableModel - Any ideas?
Thanks very much
Amy
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to implement addRow() and removeRow() and very likely don't want to. The big problem is addRow(), which in DefaultTableModel requires either an Object array or a Vector as an argument. You don't have either one - you have a Test. So write methods to suit you, something like:
 
Amy Phillips
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant! Thanks Steve I have got it working
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!