public class SMAdmMgrRepView extends javax.swing.JFrame {
public SMAdmMgrRepView() {
String tSQL = "Select * from TMSM_Staff";
Object [] [] data = null;
try {
ResultSet rs = SMDatabase.RunSQL(tSQL);
ResultSetMetaData metaData = rs.getMetaData();
int numberOfColumns = metaData.getColumnCount();
Vector columnNames = new Vector();
for (int column = 0; column < numberOfColumns; column++) {
columnNames.addElement(metaData.getColumnLabel(column + 1));
}
Vector rows = new Vector();
while (rs.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= numberOfColumns; i++) {
newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}
tblMgrRepList.setModel(rows, columnNames); // <<< referencing incorrectly?
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
initComponents();
//set the default column widths
tblMgrRepList.setAutoResizeMode(tblMgrRepList.AUTO_RESIZE_OFF);
TableColumn col = null;
//.... column width and titles code left out for brevity
}
2 replies to this topic
#1
Posted 09 March 2011 - 03:35 PM
I'm still trying to wrap my head around the use of the jTable. I have connectivity to my SQL database, and I'm pulling data. However, my issue is with trying to get the resultset into the existing table. I've seen many examples of creating the table from the data. But, since I'm creating the table with the GUI tools in Netbeans, I just need to pass the data reference to the table. Here's a snippit of my code and a comment on the reference to the table. Any pointers would be greatly appreciated.
|
|
|
#2
Posted 09 March 2011 - 03:57 PM
If tblMgrRepList is a JTable, then you're not using the setModel method correctly. You'll need to create a TableModel object, and place your Vector of Vectors in there, along with the header names. This isn't as hard as it sounds, just use a DefaultTableModel, there's a constructor for using a Vector of data and a Vector of column names.
EDIT: Also, as a side note, JTable is just an object to represent the view of a TableModel object, nothing else. You should manage the data directly with the TableModel and the JTable pretty much takes care of itself. Every time you modify the TableModel's data, the change will reflect on the JTable automatically.
EDIT: Also, as a side note, JTable is just an object to represent the view of a TableModel object, nothing else. You should manage the data directly with the TableModel and the JTable pretty much takes care of itself. Every time you modify the TableModel's data, the change will reflect on the JTable automatically.
Wow I changed my sig!
#3
Posted 10 March 2011 - 09:33 AM
Thanks for the great tips. I'll give this a shot.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









