• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

JTable  RSS feed

Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 (result1.next()) {
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
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
global solutions you can do in your home or backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!