Why doesn't this code work? (Copying rows across JTables)
For a couple of weeks I have been attempting to copy the data in one row of one JTable to a second table, without success.
Usually I get an ArrayIndexOutOfBoundsException, even though the target table
contains more than enough rows to accomodate the copy.
Here's what I have done: I stripped down an app from a Java book, "Graphic Java
II - Swing," and added my row-copying code. ("interTableCopy()," line 97 if Table1Go.java)
I duplicated the code for Table1 (minus the row-copy code) to make Table2.
Each table is 20 rows by 6 columns.
The files are included below. Table1Go.java is 97 lines and Table2.java is 72 lines.
Here are the results:
Clicking on the "copy Row" button:
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: 5 >= 0
at java.util.Vector.setElementAt(Vector.java)
at Table1.interTableCopy(Table1Go.java:112)
at Table1$3.actionPerformed(Table1Go.java:83)
The key command "meta I":
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: 10 >= 0
at java.util.Vector.setElementAt(Vector.java)
at Table1.interTableCopy(Table1Go.java:112)
As an aside, adding a column to the target table (table2), then attempting the
row copy result in this:
Exception occurred during event dispatching:
java.lang.NullPointerException
at java.util.Vector.toString(Vector.java)
at java.lang.String.valueOf(Compiled Code)
at java.lang.StringBuffer.append(StringBuffer.java)
at Table1.interTableCopy(Table1Go.java:107)
If you have any solutions for this problem, I would be very grateful.
Code starts here --
[code]
/*
Table1Go.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
class Table1 extends JTable {
JFrame frame;
JTable table1;
JTable table2;
TableColumnModel t1ColumnModel;
TableColumnModel t2ColumnModel;
private int rows=20, cols=6;
private Object[] rowData = new Object[cols];
DefaultTableModel t1Model = new DefaultTableModel();
private JTable table = new JTable(t1Model);
public Table1() {
for(int c=0; c < cols; ++c)
t1Model.addColumn("Column " + Integer.toString(c));
for(int r=0; r < rows; ++r) {
for(int c=0; c < cols; ++c) {
rowData[c] = "(tONE: " + r + "," + c + ")";
}
t1Model.addRow(rowData);
}
table2 = new Table2(); // table2
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
frame.getContentPane().add(new ControlPanel(), BorderLayout.NORTH);
frame.setVisible( true );
}
class ControlPanel extends JPanel {
private JButton rowButton = new JButton("Add Row"), colButton = new JButton("Add Column"),
copyButton = new JButton("Copy Row");
public ControlPanel() {
add(rowButton);
add(colButton);
add(copyButton);
rowButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int rowCount = t1Model.getRowCount();
int colCount = t1Model.getColumnCount();
if(colCount > rowData.length)
rowData = new Object[colCount];
for(int c=0; c < colCount; ++c) {
rowData[c] = "(" + rowCount + "," +
c + ")";
}
t1Model.addRow(rowData);
}
});
colButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int colCount = t1Model.getColumnCount();
t1Model.addColumn("Column " + colCount);
// Bug: the call to sizeColumnsToFit()
// should not be necessary
table.sizeColumnsToFit(-1);
}
});
copyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
interTableCopy( 5, 5 );
}
});
ActionListener actionListenerAssign = new ActionListener() {
public void actionPerformed( ActionEvent actionEvent) {
interTableCopy( 5, 10 );
}
};
KeyStroke commandI = KeyStroke.getKeyStroke( "meta I" );
copyButton.registerKeyboardAction( actionListenerAssign, commandI, JComponent.WHEN_IN_FOCUSED_WINDOW );
}
}
public void interTableCopy( int rowNumber, int position ) {
//DefaultTableModel t1DataModel = (DefaultTableModel)table1.getModel();
Object t1SourceRowData = t1Model.getDataVector().elementAt(rowNumber);
DefaultTableModel t2DataModel = (DefaultTableModel)table2.getModel();
//System.out.println( "t1model = " + t1model );
//System.out.println( "t2Model = " + t2Model );
System.out.println( "t1SourceRowData = " + t1SourceRowData );
System.out.println( "rowNumber = " + rowNumber );
System.out.println( "position = " + position );
System.out.println( "" );
t2DataModel.getDataVector().setElementAt(t1SourceRowData, position);
t2DataModel.fireTableRowsUpdated(position, position);
table2.repaint();
}
}
public class Table1Go {
public static void main(String args[]) {
Table1 table1 = new Table1();
}
}
/*
Table2.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
class Table2 extends JTable {
private int rows=20, cols=6;
private Object[] rowData = new Object[cols];
DefaultTableModel t2Model = new DefaultTableModel();
private JTable table = new JTable(t2Model);
public Table2() {
for(int c=0; c < cols; ++c)
t2Model.addColumn("Column " + Integer.toString(c));
for(int r=0; r < rows; ++r) {
for(int c=0; c < cols; ++c) {
rowData[c] = "(t2: " + r + "," + c + ")";
}
t2Model.addRow(rowData);
}
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
frame.getContentPane().add(new ControlPanel(), BorderLayout.NORTH);
frame.setLocation( 200, 200 );
frame.setVisible( true );
}
class ControlPanel extends JPanel {
private JButton rowButton = new JButton("Add Row"), colButton = new JButton("Add Column");
public ControlPanel() {
add(rowButton);
add(colButton);
rowButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int rowCount = t2Model.getRowCount();
int colCount = t2Model.getColumnCount();
if(colCount > rowData.length)
rowData = new Object[colCount];
for(int c=0; c < colCount; ++c) {
rowData[c] = "(t2: " + rowCount + "," +
c + ")";
}
t2Model.addRow(rowData);
}
});
colButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int colCount = t2Model.getColumnCount();
t2Model.addColumn("Column " + colCount);
// Bug: the call to sizeColumnsToFit()
// should not be necessary
table.sizeColumnsToFit(-1);
}
});
}
}
}
[code]
Code ends here --

