JTables + JDBC: Pt. 4 Putting it all Together

This is part 4 in the series on JTables and JDBC

<< Pt. 3 Connecting to your database through JDBC

Alright, let’s put this JTable together! Using the code from the previous section, we can now populate our custom AbstractTableModel by pulling information from a remote database.  Don’t worry, its a lot easier than it looks.


Let’s start by looking at a very simple TableModel that would do the trick:

import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.table.AbstractTableModel;

public class MyTableModel extends AbstractTableModel{

	private Object[][] content;
	private String[] colNames;

	public MyTableModel () {
		try {
			Connection conn = DatabaseData.connect();

			content = getTableContent(conn, "TABLE_NAME");
			colNames = getTableColumnNames(conn, "TABLE_NAME");

			disconnect(conn);
		}
		catch (SQLException sqx) {
			content = new Object [][] {{""}};
			colNames = new String [] {"Error"};
			System.err.println("Could not pull data from database");
		}
	}

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

	public int getRowCount() {
		return content.length;
	}

	public Object getValueAt(int arg0, int arg1) {
		return content[arg0][arg1];
	}

	public boolean isCellEditable(int row, int col) {
		return true;
	}
	public void setValueAt(Object aValue, int row, int col) {
		content[row][col] = aValue;
	}

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

I would first like to draw your attention to the constructor. In it, we initialize both content and colNames by calling the methods created in the previous section. You’ll also notice that I left out these methods in the MyTableModel class. You can either add the required methods to this class or place the code in a separate class. Personally, I chose to put all of the database code into a separate class because I found that I tended to reuse the code a lot.

The rest of the code is relatively straightforward. You can perform a few tweaks on it here and there to suite your needs, but aside from that you’re good to go.

To create your JTable, I offer you the following sample code:

JFrame myFrame = new JFrame("My Table");

MyTableModel mt = new MyTableModel();
JTable jt = new JTable(mt);
JScrollPane jsp = new JScrollPane(jt);

myFrame.add(jsp);
myFrame.setVisible(true);

First, we create a new instance of MytableModel and pass that into the constructor of a new JTable. The JTable is then placed inside a new JScrollPane, which is then placed in a new JFrame for your viewing pleasure.

Although the JScrollPane seems unnecessary, I was unable to see my table’s column headers if I did not do so.

And there you have it! A JTable that can display data from a database table. Stay tuned for more JTable tricks later on this week.

This entry was posted in Java, Tech and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>