Does anyone know how to populate a jTable from a hibernate List?

Does anyone know how to populate a jTabel from a Hibernate List? I've tried to create my own TableModel, but am getting CastException when trying to cast the item.. example
public class HibernateTableModel implements TableModel
{
 List datalist;             // The ResultSet to interpret
 
  int numcols, numrows;          // How many rows and columns in the table
  String [] cols = {"ID", "Claim No", " Last Name", "Serial No", "Model No"};

  /**
   * This constructor creates a TableModel from aList.  It is package
   * private because it is only intended to be used by
   * TableModelFactory, which is what you should use to obtain a
   * TableModel
   **/
  public HibernateTableModel(List datalist) throws SQLException
  {
    this.datalist = datalist; // Save the results
    // Get metadata on them
    numcols = getColumnCount(); // How many columns?                        
    numrows = getRowCount(); // How many rows?
  }
 
 
  public void addWidgetList(List l)
  {
    datalist.addAll(l);
    fireTableDataChanged();
  }
  /** Automatically close when we're garbage collected */
  protected void finalize() {  }

  // These two TableModel methods return the size of the table
  public int getColumnCount() {
     numcols = cols.length;
   return numcols;
   }
   
  public int getRowCount() {
    numrows = datalist.size();
  return numrows;
  }
 
  public String getColHeader(int column) {
      return cols[column];
  }

  // This TableModel method returns columns names from the ResultSetMetaData
  public String getColumnName(int column) {
    return cols[column];
  }

  // This TableModel method specifies the data type for each column.  
  // We could map SQL types to Java types, but for this example, we'll just
  // convert all the returned data to strings.
  public Class getColumnClass(int column) { return String.class; }
 
 
  public WarrantyClaimBean getWidgetAt(int row)
  {
      Object obj = datalist.get(row);
    return (WarrantyClaimBean) obj;
  }
  /**
   * This is the key method of TableModel: it returns the value at each cell
   * of the table.  We use strings in this case.  If anything goes wrong, we
   * return the exception as a string, so it will be displayed in the table.
   * Note that SQL row and column numbers start at 1, but TableModel column
   * numbers start at 0.
   **/
  public Object getValueAt(int row, int column) {
   System.out.println(Integer.toString(row));
   Object value;
    WarrantyClaimBean widget;
    widget = getWidgetAt(row);
    switch (column)  
   {
      case 0:
         return Long.toString(widget.getRecId());
      case 1:
         return widget.getClaimNo();
      case 2:
         return widget.getLastName();
      case 3:
         return widget.getSerialNo();
      case 4:
         return widget.getVacModel();
      default:
         return null;
   }
  }

  // Our table isn't editable
  public boolean isCellEditable(int row, int column) { return false; }

  // Since its not editable, we don't need to implement these methods
  public void setValueAt(Object value, int row, int column) {}
  public void addTableModelListener(TableModelListener l) {}
  public void removeTableModelListener(TableModelListener l) {}

  private void fireTableDataChanged()
  {
  }
}

Any help would be appreciated.
jlowe1268Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
>>am getting CastException when trying to cast the item.. example

Please post the stack trace.
0
Dejan PažinHead of SW DevelopmentCommented:

Since you didn't tell us where the ClassCastException occurs, I am guessing it is here:

public WarrantyClaimBean getWidgetAt(int row)
  {
      Object obj = datalist.get(row);
    return (WarrantyClaimBean) obj;
  }

Change that method, to see the class of object you are getting:


public WarrantyClaimBean getWidgetAt(int row)
  {
      Object obj = datalist.get(row);
    System.out.println("actual class is: " + obj.getClass());
    return (WarrantyClaimBean) obj;
  }

That way you will see what you have to cast it to.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jlowe1268Author Commented:
Thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.