This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Adding Empty Rows initially when my table model uses ArrayList()<Employee> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding Empty Rows initially when my table model uses ArrayList()<Employee>" Watch "Adding Empty Rows initially when my table model uses ArrayList()<Employee>" New topic

Adding Empty Rows initially when my table model uses ArrayList()<Employee>

L Sankaranarayanan

Joined: Dec 13, 2012
Posts: 11
I wish to construct a JTable with 10 rows defaults . My table model uses ArrayList and Employee objects. When I use it JTable the data is inserted in the last row Why? Could any help me

My code is

import java.util.Date;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.swing.*;
import java.util.*;
* @author L.Sankaranarayanan

public class EmployeeTableModel extends AbstractTableModel{
private static final long serialVersionUID = 1L;
protected static final int EMPLOYEE_ID =0;
protected static final int EMPLOYEEE_NAME =1;
protected static final int DATE_OF_BIRTH =2;
protected static final int ADDRESS =3;
protected static final int CITY =4;
protected static final int PINCODE =5;
protected static final int CONTACT_NUMBER =6;
protected static final int DEPARTMENT=7;
protected static final int DATE_OF_APPOINTMENT=8;
protected static final int PASSWORD =9;

protected String employeeId;
protected String employeeName;

protected java.sql.Date dateOfBirth;
protected String address;
protected String city;
protected String pinCode;
protected String contactNumber;
protected String department;
protected java.sql.Date dateOfAppointment;
protected String password;

private int rows;

static final public ColumnData columnHeads[] = {

new ColumnData( "Id", 30, JLabel.LEFT ),
new ColumnData( "Name", 100, JLabel.LEFT ),
new ColumnData( "DateOfBirth", 60, JLabel.RIGHT ),
new ColumnData( "Address", 100, JLabel.LEFT),
new ColumnData( "City", 100, JLabel.LEFT),
new ColumnData( "Pincode", 100, JLabel.LEFT),
new ColumnData( "ContactNumber", 100, JLabel.LEFT),
new ColumnData( "Department", 100, JLabel.LEFT),
new ColumnData( "DateOfAppointment", 100, JLabel.LEFT),
new ColumnData( "Password", 100, JLabel.LEFT)};

protected ArrayList<Employee> employees;

public EmployeeTableModel()
this.employees = new ArrayList<Employee>();
// createDefaultRows();


public EmployeeTableModel(ArrayList<Employee> employees)
this.employees = employees;


public void createDefaultRows()
{ rows =10;
for( int i = 0; i < rows; i++ )
//employees = new ArrayList<Employee>();
//for( int j = 0; j < columns.length; j++ )
// cols.add(new Employe());
employees.add(new Employee());

public int getRowCount() {
//if( employees.size()==0) return 10;
return employees.size();

public int getColumnCount() {
return columnHeads.length;

public String getColumnName(int column) {
return columnHeads[column].title;

public void removeAllData(){
employees = null;
public void update(ArrayList<Employee> employees){
public void clear()
employees = employees != null ? employees : new ArrayList<Employee>( );



public Object getValueAt(int rowIndex, int columnIndex) {
Employee emp =employees.get(rowIndex);

System.out.println("Inside getValueAt "+emp);

case EMPLOYEE_ID :return emp.getEmployeeId();
case EMPLOYEEE_NAME :return emp.getEmployeeName();
case DATE_OF_BIRTH :return emp.getDateOfBirth();
case ADDRESS :return emp.getAddress();
case CITY :return emp.getCity();
case PINCODE :return emp.getPinCode();
case CONTACT_NUMBER :return emp.getContactNumber();
case DEPARTMENT : return emp.getDepartment();
case DATE_OF_APPOINTMENT:return emp.getDateOfAppointment();
case PASSWORD :return emp.getPassword();

default: return "";

public Class<?> getColumnClass(int columnIndex) {

case EMPLOYEE_ID :return String.class;
case EMPLOYEEE_NAME :return String.class;
case DATE_OF_BIRTH :return Date.class;
case ADDRESS :return String.class;
case CITY :return String.class;
case PINCODE :return String.class;
case CONTACT_NUMBER :return String.class;
case DEPARTMENT : return String.class;
case DATE_OF_APPOINTMENT:return Date.class;
case PASSWORD :return String.class;

default: return Object.class;
//over ride the following method only if you want the table cells editable

/* public void setValueAt(Object value, int row, int col) {

Person p = data.get(row);
switch (col) {
case 0: break;
case 1: p.setFirstName( (String) value); break;
case 2: p.setSurName( (String) value); break;
case 3: p.getEventBirth().setDate( (Date) value); break;
case 4: p.getEventDeath().setDate( (Date) value); break;
case 5: p.setInfo( (String) value); break;
default: break;


public Employee getEmployeeAt( int row )
if( row >= getRowCount( ))
return null;

return employees.get(row);

public void insert(Employee employee)
// super.fireTableDataChanged();

public void update(Employee employee, int index)
employees.set(index, employee);
public void delete(int index)

public void setEmployees(ArrayList<Employee> employees)
this.employees = employees;

public ArrayList<Employee> getEm ployees()
return this.employees;



Ranganathan Kaliyur Mannar

Joined: Oct 16, 2003
Posts: 1097

Please UseCodeTags when posting code i.e use the [code] tag and not the [color] tag. Also, format the code properly and avoid empty lines. Basically, the idea is to make it easy for people who are trying to help you.

Coming to your problem, you have mentioned data is inserted in the last row. I am unable to understand what that means.
Do you mean to say that only the 10th row is filled with data and the rest are empty? You haven't shown the code of the Employee class and I have no idea if you have implemented a no-arg constructor (which in turn fills up the instance variables with some values).
Can you please provide more details?

L Sankaranarayanan

Joined: Dec 13, 2012
Posts: 11
Thanks Mr.Renganathan.

What I actually want is EmployeePickList Class with a JTable. The table must initially contain 10 empty rows. When we type some letters in the search TextField , employee names should appear (TextField is added with Document Listener) . By empty constructor I make 10 rows by default. Any how I found the solution by modifiying my codes in the insertEmployee method

corrected code segment

Thanks once again . Im new to this forum and so I didn't notice the code tag . I'll correct myself in future

[Thumbnail for employeesearch.JPG]

I agree. Here's the link:
subject: Adding Empty Rows initially when my table model uses ArrayList()<Employee>