A TableModel for Entity Beans

For my graduation project i needed a component that could display a list of Entity Beans. I found that JTable is such a component and with the help of custom TableCellRenderer and TableCellEditor components i was able to customize the rendering to my needs. In order to get the data into the JTable i implemented a custom TableModel, namely EntityTableModel. Now i can easily generate a JTable that displays Entity Beans:

// fetch the elements we want to display
Object[] elements = employeeController
  .getEntityManager()
  .createNamedQuery("findEmployees")
  .getResultList()
  .toArray();

// build map with column name, entity attribute pairs
HashMap<String, String> colAttrs = new LinkedHashMap<String, String>();
colAttrs.put("First Name", "firstName");
colAttrs.put("Last Name", "lastName");

// build collection with editable attributes
Collection<String> editables = new ArrayList<String>();

// initialise the tablemodel
TableModel tableModel = new EntityTableModel(elements, colAttrs, editables);

// initialise the table
JTable table = new JTable(tableModel);

This entry was posted on Sunday, June 18th, 2006 at 02:28 and is filed under Hibernate. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.