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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Dejan PažinConnect With a Mentor Head 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
 
CEHJCommented:
>>am getting CastException when trying to cast the item.. example

Please post the stack trace.
0
 
jlowe1268Author Commented:
Thanks for your help.
0
All Courses

From novice to tech pro — start learning today.