Simple JTable Question

J

JDany

When a user clicks on a table column heading s/he is allowed to move
the column and this generates a column moved event.

Well what I do is catch this "column moved" event and update my data
model to reflect the changes.

Then only problem I am having is after
I update the model I fire a table structure change event
"fireTableStructureChanged();" to let the gui know to update the values
but my program just crashes when I do this (See below search for ERROR).

If on the other hand I don't fire the event then everything is fine
but the display still shows the columns as "not moved" but internaly
(in the data model) they are. This is apparent if some other functions
fires a table structure change event.

So am i doing something wrong? should i be catching the colum move
somewhere else?

---- begin code ----
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.AbstractTableModel;

public class MyTable extends JFrame {

private TableDataModel dataModel = new TableDataModel();

public MyTable() {
initComponents();
postInitComponenets();
}

private void initComponents() {
javax.swing.JScrollPane scrollPane;

scrollPane = new javax.swing.JScrollPane();
table = new javax.swing.JTable();

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

table.setModel(dataModel);
table.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
table.setColumnSelectionAllowed(true);
scrollPane.setViewportView(table);

getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);

pack();
}

private void postInitComponenets() {
// Table listeners
table.getColumnModel().addColumnModelListener(dataModel);

// Fake data
dataModel.createTable(3,5);
dataModel.setValueAt("1", 0, 0);
dataModel.setValueAt("2", 0, 1);
dataModel.setValueAt("3", 0, 2);
dataModel.setValueAt("4", 0, 3);
dataModel.setValueAt("5", 0, 4);
}

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

public static void main(String args[]) {
MyTable rt = new MyTable();
rt.setLocationRelativeTo(null);
rt.show();
}

private class TableDataModel extends AbstractTableModel
implements TableColumnModelListener
{
private Vector TableColumns = new Vector();

public TableDataModel() {
}

public int getColumnCount() {
return TableColumns.size();
}

public int getRowCount() {
if ( TableColumns.size() == 0 ) {
return 0;
}
return ((Vector)TableColumns.elementAt(0)).size();
}

public Object getValueAt(int rowIndex, int columnIndex) {
return ((Vector)TableColumns.elementAt(columnIndex)).
elementAt(rowIndex);
}

public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
((Vector)TableColumns.elementAt(columnIndex)).
insertElementAt(aValue, rowIndex);

((Vector)TableColumns.elementAt(columnIndex)).
remove(rowIndex + 1);
}

public void createTable(int rows, int cols) {
for ( int i = 0; i < cols; i ++ ) {
Vector v = new Vector();
for ( int j = 0; j < rows; j++ )
v.addElement("");
TableColumns.addElement(v);
}
fireTableStructureChanged();
}

//*** TableColumnModelListener Interface ***//
public void columnMoved(TableColumnModelEvent e) {
if ( e.getFromIndex() == e.getToIndex() )
return;

int from = e.getFromIndex()<e.getToIndex()?
e.getFromIndex():e.getToIndex();
int to = e.getFromIndex()<e.getToIndex()?
e.getToIndex():e.getFromIndex();

Vector fv = (Vector)TableColumns.elementAt(from);
TableColumns.setElementAt(TableColumns.elementAt(to), from);
TableColumns.setElementAt(fv, to);

// ERROR: java.lang.ArrayIndexOutOfBoundsException: -1 < 0
fireTableStructureChanged();
}

public void columnAdded(TableColumnModelEvent e) {}
public void columnMarginChanged(ChangeEvent e) {}
public void columnRemoved(TableColumnModelEvent e) {}
public void columnSelectionChanged(ListSelectionEvent e) {}
}

private javax.swing.JTable table;
}
---- end code ----
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top