Simple JTable example using AbstractTableModel

Simple JTable example using AbstractTableModel



The following example demonstrates the JTable example using AbstractTableModel , before going to the example lets see what is abstracttablemodel and how we use abstract table model in jtable.

As AbstractTableModel implements TableModel interface, it provides default implementation of the methods in TableModel interface.
Use of AbstractTableModel
1. Its one of the task is to manage the listeners.
2. The other task is to ease the generation of table model events and dispatching them to the listeners.


To creates our own AbstractTableModel, we need to implement three methods :

public int getRowCount();
public int getColumnCount();
public Object getValueAt(int row, int column);


Following example of JTable using AbstractTableModel have following steps :

1. Create a SimpleDataModel which extends AbstractTableModel.
2. Create an instance of JTable and pass object of SimpleDataModel to the JTable.
3. In following example SimpleTableMdoel use Vector to store the collection data.
4. Example of how to create and use SimpleTableModel and JTable as below :



public class SimpleJTableExample extends JFrame
{

private JTable e_simpleTable; //JTable table

private SimpleTableModel e_simpleTableModel; //JTable model

public SimpleJTableExample()
{
super("JTable Example using abstract table model");

e_simpleTableModel = new SimpleTableModel(getTempData());//Creating table model

e_simpleTable = new JTable(e_simpleTableModel); //Creating JTable

JScrollPane scrollPane = new JScrollPane(e_simpleTable);

getContentPane().add(scrollPane);
}

private Vector getTempData()
{
//Employee Data in vector
Vector employeeData = new Vector(10, 10);

employeeData.addElement(new EmployeeData
( "Mike", "Manager",
"mike1", "mike2"));
employeeData.addElement(new EmployeeData
( "Nathan", "HR admin",
"nathan1", "nathan2"));
employeeData.addElement(new EmployeeData
("Bennie", "Sales",
"ben1", "ben2"));


return employeeData;
}

public static void main(String[] arg)
{
SimpleJTableExample tableEx = new SimpleJTableExample();

tableEx.setVisible(true);
tableEx.setSize(new Dimension(600, 300));
tableEx.validate();
}

public class SimpleTableModel extends AbstractTableModel
{

public static final int E_NAME_COL = 0;
public static final int E_DES_COL = 1;
public static final int E_USER_COL = 2;
public static final int E_PASS_COL = 3;

public String[] e_colNames = {"Employee Name",
"Employee Designation",
"User Name",
"Password"};

public Class[] e_colTypes = {String.class,
String.class,
String.class,
String.class};


Vector eDataVector;


public SimpleTableModel(Vector macDataVector)
{
super();

eDataVector = macDataVector;
}


public int getColumnCount()
{
return e_colNames.length;
}

public int getRowCount()
{
return eDataVector.size();
}

public void setValueAt(Object value, int row, int col)
{
EmployeeData eData = (EmployeeData)(eDataVector.elementAt(row));

switch(col)
{
case E_NAME_COL : eData.setEmployeeName((String) value);
break;
case E_USER_COL : eData.setUserName((String) value);
break;
case E_DES_COL : eData.setDesignation((String) value);
break;
case E_PASS_COL : eData.setPassword((String) value);
break;
}
}


public String getColumnName(int col)
{
return e_colNames[col];
}

public Class getColumnClass(int col)
{
return e_colTypes[col];
}
public Object getValueAt(int row, int col)
{
EmployeeData eData = (EmployeeData)(eDataVector.elementAt(row));

switch(col)
{
case E_NAME_COL : return eData.getEmployeeName();
case E_USER_COL : return eData.getUsername();
case E_DES_COL : return eData.getDesignation();
case E_PASS_COL : return eData.getPassword();
}

return new String();
}
}

public class EmployeeData {
private String e_employeeName;
private String e_designation;
private String e_username;
private String e_password;

public EmployeeData()
{
}

public EmployeeData(String employeeName,
String designation, String username,
String password)
{
e_employeeName = employeeName;
e_designation = designation;
e_username = username;
e_password = password;

}

public String getEmployeeName()
{
return e_employeeName;
}

public String getDesignation()
{
return e_designation;
}

public String getUsername()
{
return e_username;
}

public String getPassword()
{
return e_password;
}


public void setEmployeeName(String employeeName)
{
e_employeeName = employeeName;
}

public void setUserName(String username)
{
e_username = username;
}

public void setDesignation(String designation)
{
e_designation = designation;
}

public void setPassword(String password)
{
e_password = password;
}
}
}
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment