Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How To Save Edited Data From A Custom Jtable That Uses A List As Data Source

jtable jcelleditor list

  • Please log in to reply
1 reply to this topic

#1 AndreaFar

AndreaFar

    CC Regular

  • Member
  • PipPipPip
  • 26 posts

Posted 21 June 2012 - 07:19 AM

I don't know how to save the edited data from a jtable... the available sample and tutorial about jtable that I have seen are very poor.

`

I use this model Table(similar to the model in another topic):
class MyTableModel extends AbstractTableModel {
	  private static final long serialVersionUID = 1L;
	  int numSports = DataSavedSports.loadNumeroSports();
	  private final List<Sport> objects = DataSavedSports
		.loadListSports();
  
	  private final String[] columnNames = { "Name Sport 1", "Criteria",
		"Name Sport 2" };
  
	  private final Class<?>[] metaModell = new Class[] { String.class,
		Integer.class };
  
	  public int getColumnCount() {
	   return columnNames.length;
	  }
  
	  public int getRowCount() {
	   return objects.size();
	  }
  
	  @Override
	  public String getColumnName(int col) {
	   return columnNames[col];
	  }
  
  
  
	  private Sport getRow(int row) {
	   return objects.get(row);
	  }
  
	  @Override
	  public Class<?> getColumnClass(int c) {
	   if (c < metaModell.length)
		return metaModell[c];
	   return Object.class;
	  }
  
	  @Override
	  public boolean isCellEditable(int row, int col) {
	   return true;
	  }
  
  
  
	  public List<Sport> getSport() {
	   return objects;
	  }
  
	  public void addRow(Sport v) {
	   this.objects.add(v);
	   fireTableDataChanged();
	  }

	  public Object getValueAt(int row, int col) {
		  if (row > objects.size())
			  return null;
		  Sport s = getRow(row);
		  switch (col) {
			  case 0:
				  return s.getIdSport();
			  case 1:
				  return s.getType();
		  }
		  return null;
	  }

				
				  @Override
  public void setValueAt(Object value, int row, int col) {
		  Sport s = getRow(row);
		  switch (col) {
			  case 0:
				  s.setIdSport((String) value);
				  fireTableDataChanged();
				  DataManager.saveSportList(objects);
				  break;
			  case 1:
				  s.setType((Integer) value);
				  fireTableDataChanged();
				  DataManager.saveSportList(objects);
				  break;
		  }

	 }


But the List of Sports isn't saved. What is wrong?
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 21 June 2012 - 09:28 AM

We would have to see the source code for DataManager.saveSportList(List<Sport>) to be able to answer your question, if that is indeed where you wrote the code that saves the list.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Also tagged with one or more of these keywords: jtable, jcelleditor, list

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download