• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTable

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
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
 
What's brown and sticky? ... a stick. Or a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic