This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable" Watch "JTable" New topic


Richard Larocque

Joined: May 10, 2001
Posts: 6
I've been trying to get a JTable to appear. Unfortunately only a blank screen appears. I have 6 records in my test database. When I checked my dataModel with getRowCount and getColumnCount, it does show my 6 records and my 6 columns. What am I missing?
Enclosed is a copy of my code.

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.sql.*; //for jdbc
import java.math.*; //for jdbc
import javax.swing.JTable.*;
import javax.swing.event.*;
import javax.swing.table.*;//Added for the TableModel
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import java.util.Vector;
import java.awt.Dimension;
import javax.swing.border.*;
public class test {
JPanel maincustPanel;
ResultSetMetaData header_items;
Container content;
Connection con;
ResultSet result1 = null;
String driver="", user="", passwd="", connect="";
String[] columnNames = {};
Vector rows = new Vector();
static String[] ConnectOptionNames = {"Connect"};
public static void main (String[] args) {
test frame = new test();
public test() {
JTable custTbl;
ResultSetMetaData headerItems;
try {
catch(Exception e) {
System.out.println("Error setting native LAF: " + e);
JFrame myframe = new JFrame("Test");
content = myframe.getContentPane();
// Avec Swing, toutes les operations sont fait sur le container.
myframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
JDBCUtilities JU = new JDBCUtilities();
driver = "oracle.jdbc.driver.OracleDriver";
connect = "jdbc racle:thin:@ncrlarocquer:1521 ra816ie";
user = "system";
passwd = "manager";
con = JU.Connect(driver,connect,user,passwd);
try {
if(con.getMetaData().supportsResultSetConcurrency(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE)) {
PreparedStatement pstmt = con.prepareStatement(
"Select customer_name,street_address,city,province,postal_code,tel from ncrlarocquer.customers",
result1 = pstmt.executeQuery();
Statement stmt = con.createStatement();
result1 = stmt.executeQuery("Select customer_name,street_address,city,province,postal_code,tel from ncrlarocquer.customers");
headerItems = result1.getMetaData();
int numberOfColumns = headerItems.getColumnCount();
columnNames = new String[numberOfColumns];
for (int column=0; column < numberOfColumns; column++) {
columnNames[column] = headerItems.getColumnLabel(column+1);
rows = new Vector();
while ( {
Vector newRow = new Vector();
for (int i=1; i<= headerItems.getColumnCount(); i++) {
catch (SQLException e) {
System.out.println (e.getErrorCode() + ":" + e.getSQLState() + ":" + e.getMessage());
TableModel dataModel = new AbstractTableModel() {
//Les trois prochaines methodes doivent etre crees.
public int getColumnCount() {
return columnNames.length;
public int getRowCount() {
return rows.size();
public Object getValueAt(int aRow, int aColumn) {
Vector row = (Vector)rows.elementAt(aRow);
return row.elementAt(aColumn);
//Les 4 prochaines methodes sont optionnelles, mais on devrait toujours les ameliorer.
public String getColumnName(int column) {
if (columnNames[column] != null)
return columnNames[column];
return "";
public Class getColumnClass(int c) {
return getValueAt(0,c).getClass();
public boolean isCellEditable(int row, int col) {
return true;
public void setValueAt(Object aValue, int row, int column) {
Vector dataRow = (Vector)rows.elementAt(row);
}; //end of dataModel
custTbl = new JTable(dataModel);
custTbl.setPreferredScrollableViewportSize(new Dimension(500, 70));
custTbl.setBorder(new BevelBorder(BevelBorder.LOWERED));
maincustPanel = new JPanel();
maincustPanel.setLayout(new FlowLayout());
JPanel headerPanel = new JPanel();
JLabel HeaderLbl = new JLabel("JTable Customers");
HeaderLbl.setFont(new Font("Dialog", Font.BOLD, 24));
JScrollPane custScroll = new JScrollPane(custTbl);
} //end of class
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: JTable