Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable addColumns not showing up in table

 
Gary Frick
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gary Frick
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great example! This did the trick. Thanks again.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic