JTable.removeColumn() - works as advertised?

F

Fritz

What does Jtable's removeColumn() method do? Well, according to
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html, it
removes a column from a table, but *not* from the underlying model:

<Quoting j2se API documentation>
removeColumn

public void removeColumn(TableColumn aColumn)

Removes aColumn from this JTable's array of columns. Note: this
method does not remove the column of data from the model; it just
removes the TableColumn that was responsible for displaying it.
</Quoting j2se API documentation>

Everything I've found in related newsgroups with Google searches agrees
with the official documentation. However, in my experience, it really
does remove the column from the model as well. Here's a simple example
that completely illustrates what I'm talking about. The first output
statement says "numColumns is 4", and the second one says "numColumns
is 0".

/*
* TableFrustration.java
*
* Created on March 25, 2005, 2:23 PM
*/
import javax.swing.table.*;

public class TableFrustration extends javax.swing.JFrame {

public TableFrustration() {
TableColumnModel columnModel = null;
TableColumn column = null;
int numColumns = 0;
int i = 0;

initComponents();

columnModel = jTable1.getColumnModel();
numColumns = columnModel.getColumnCount();
System.out.println ("numColumns is " + numColumns);

for (i = 0; i < numColumns; i++) {
column = columnModel.getColumn(0);
jTable1.removeColumn(column);
}

numColumns = columnModel.getColumnCount();
System.out.println ("numColumns is " + numColumns);
}

private void initComponents() {
jTable1 = new javax.swing.JTable();

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
getContentPane().add(jTable1, java.awt.BorderLayout.CENTER);

pack();
}

private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

public static void main(String args[]) {
new TableFrustration().show();
}

private javax.swing.JTable jTable1;
}
/*
* End of code
*/

So...what gives? Is there something about tables and table models that
I'm missing, or is the documentation really inconsistent with the
actual behavior of JTable?

ff
 
F

Fritz

Never mind. TableModel, TableColumnModel, ColumnModel....I'm a little
bit dazed and confused here.

The problem is that I'm getting the column count from a
TableColumnModel as opposed to a TableModel. I gotta go cogitate on
this some more.

(sigh.)
 
R

Richard Wheeldon

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top