wood burning stoves*
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
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable" Watch "JTable" New topic
Author

JTable

Richard Larocque
Greenhorn

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.
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 {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
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) {
System.exit(0);
}
});
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",
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
result1 = pstmt.executeQuery();
}
else
{
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++) {
newRow.addElement(result1.getObject(i));
}
rows.addElement(newRow);
}
}
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];
else
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);
dataRow.setElementAt(aValue,column);
}
}; //end of dataModel
custTbl = new JTable(dataModel);
custTbl.setPreferredScrollableViewportSize(new Dimension(500, 70));
custTbl.setBorder(new BevelBorder(BevelBorder.LOWERED));
custTbl.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
custTbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
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));
headerPanel.add(HeaderLbl);
maincustPanel.add(headerPanel);
JScrollPane custScroll = new JScrollPane(custTbl);
maincustPanel.add(custScroll);
content.add(maincustPanel);
myframe.pack();
myframe.setVisible(true);
}
} //end of class
 
 
subject: JTable
 
Similar Threads
JTable
jtable
JTable in a JInternalFrame
iscelleditable is not working in jtable
JTable