wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes JTable addColumns not showing up in table 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 addColumns not showing up in table" Watch "JTable addColumns not showing up in table" New topic
Author

JTable addColumns not showing up in table

Gary Frick
Greenhorn

Joined: Sep 13, 2002
Posts: 28
When I attempt to add columns and headers to my dynamic table, I get no errors, but then again I get nothing showing up as a table either. However if I define some inital dummy columnNames in the String array within the model, it adds columns, but they are all of the same header. What is my basic problem or misunderstanding? I'm not sure if I have included enough code snippets below, if not, let me know and I will supply, otherwise that may be part of the problem.
Appreciatively,
Gary

== Define JTable
table = new JTable(tblmodel);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setPreferredScrollableViewportSize(new Dimension(FORMWIDTH+200,325));
table.setBackground(Color.white);

== Add table headers
TableColumnModel tcm = table.getColumnModel();
for(int i=0; i<table.getColumnCount();i++)
{
TableColumn tc = tcm.getColumn(i);
tcm.addColumn(tc);
tc.setHeaderValue(colNames[i+1]);
}
== Define Table Model
class ResultSetTableModel extends AbstractTableModel {

ResultSetMetaData metadata; // Additional information about the results
int numcols, numrows; // How many rows and columns in the table

String[] columnNames = {};
Object[][] data = {};
.
.
.
Gary Frick
Greenhorn

Joined: Sep 13, 2002
Posts: 28
Problem not fixed, but I did discover that I was using the wrong indexer for the 'for' loop. Now I'm getting an array index out of bounds on the
TableColumn tc = tcm.getColumn(i);
This is where I think I am misunderstanding the requirement. How do you do a getColumn on something that doesn't exists, but yet it is needed to add the column?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I was just going through my notes (I haven't worked with Swing lately) and found that it was more complicated then I thought. I ended up having to create a subclass of AbstractTableModel. This was for a JavaUnzip utility similar to WinZip. Here is the code for the AbstractTableModel class:

To use this I did this (zipTable is a JTable):
zipModel = new ZipTableModel();
zipModel.setTableColumns(zipTable);
zipTable.setModel(zipModel);
and then after the user selects a file to unzip:
File f = fileChooser.getSelectedFile();
zipData.setArchiveData(f);
zipTable.tableChanged(new TableModelEvent(zipData));
I hope this helps.
By the way, I got most of this code from the O'Reilly Swing book.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Gary Frick
Greenhorn

Joined: Sep 13, 2002
Posts: 28
Great example! This did the trick. Thanks again.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Great! I'm really glad that I was able to help you! Now that you know where we are be sure to come back to visit us again!
 
 
subject: JTable addColumns not showing up in table
 
Similar Threads
jtable
JTable
JTable
JTable help
JTable problem