Saturday 15 February 2014

Java: JTable example with TableModel

This article describes about the JTable without using the Table model. In order to use a Table Model we will first write a class for a table model. The table will display the data from a database table named "customer". Customer table has three columns {account_number, date, name}.

import java.util.Vector;
import javax.swing.table.AbstractTableModel;

public class CustomerTableModel extends AbstractTableModel
{
private static final long serialVersionUID = -7174062695601475724L;
public static final int ACCOUNT_NUMBER = 0;
public static final int DATE = 1;
public static final int NAME = 2;
private String[] columnNames;
private Vector<Customer> data;

public CustomerTableModel(Vector<Customer> customerVector, String[] columns)
{
columnNames = columns;
data = new Vector<Customer>(customerVector);
}

public String getColumnName(int column)
{
return columnNames[column];
}

@Override
public int getColumnCount()
{
return columnNames.length;
}

@Override
public int getRowCount()
{
return data.size();
}

@Override
public Object getValueAt(int row, int column)
{
Customer record = (Customer) data.get(row);
switch (column)
{
case ACCOUNT_NUMBER:
return record.getAccountNumber();
case DATE:
return record.getDate();
case NAME:
return record.getCustomerName();
default:
return new Object();
}
}

}

Above class extends AbstractTableModel. This table model will be used by a JTable.

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

@SuppressWarnings("serial")
public class CustomJTable extends JFrame
{
public CustomJTable(CustomerTableModel model)
{
super("JTable Example");
JPanel tablePanel = new JPanel(new BorderLayout());
JTable table = new JTable();
table.setModel(model);
tablePanel.add(new JScrollPane(table));
add(tablePanel);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
pack();
}
}

Now we will write a handler  class which will retrieve data from the database table. For the handler class check this post where we retrieve data from MySQL table 

Now we will write a driver class that will run the program and display our output.

import java.util.Vector;

public class Driver
{
public static void main(String[] args)
{
CustomerHandler custHandler = new CustomerHandler();
java.util.ArrayList<Customer> customerList = custHandler.getAllCustomers();

if (customerList == null || customerList.isEmpty())
{
customerList = new java.util.ArrayList<Customer>();
}
String[] columnNames =
{ "accountNumber", "date", "customerName", "customerAddress", "advance", "nicNumber", "telNumber", "connectionType", "connectionFee" };

CustomerTableModel tableModel = new CustomerTableModel(new Vector<Customer>(customerList), columnNames);
CustomJTable table = new CustomJTable(tableModel);
}
}

Notice that the Driver class uses CustomerHandler and Customer class which are taken from this post.
It is inappropriate in a way to display the table from the same class but in order to make  it shorted we can do it. A new class can be added which generates the GUI for the JTable and driver can call it.

Here is the output from our program.

The output shows more columns then the ones in the Table model. It is because I have removed many columns from the class to make it shorter. I hope this is helpful. If not then let me know what is missing and I will try to add it.

No comments:

Post a Comment