Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

18 Jun 2006

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);