From: hon123456 on
I got the following exception when I try to press Enter in the cell in
order to add a row when the Enter is pressed.

Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.justifyRows(Unknown Source)
at javax.swing.table.DefaultTableModel.insertRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at javax.swing.table.DefaultTableModel.addRow(Unknown Source)
at NumericColumnTester$TblModel.addRow(NumericColumnTester.java:115)
at NumericColumnTester
$MyKeyListener.keyPressed(NumericColumnTester.java:143)

Here is My code, what's wrong with my code:

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class NumericColumnTester extends JFrame {
protected TblModel model_001;

public NumericColumnTester() {
// JTable table = new JTable(new Object[][] {
// { "a", new Integer(1), "2" }, { "b", new Integer(3), "4" } },
// new String[] { "Text", "Num 1", "Num 2" });

String[]header = { "Text", "Num 1", "Num 2" };
List data = new ArrayList();
data.add(new Object[]{ "a", new Integer(1), "2" });
data.add(new Object[]{ "b", new Integer(3), "4" });
model_001 = new TblModel(data, header);
model_001.addColumn("Text");
model_001.addColumn("Num1");
model_001.addColumn("Num2");
JTable table = new JTable(model_001);
table.setRowSelectionAllowed(false);
table.setSurrendersFocusOnKeystroke(true);

TableColumnModel cm = table.getColumnModel();
cm.getColumn(1).setCellEditor(new MyCellEditor());
cm.getColumn(2).setCellEditor(new MyCellEditor());

JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
}

private class MyCellEditor extends DefaultCellEditor {

private final JTextField tf;

public MyCellEditor() {
super(new JTextField());
tf = (JTextField) getComponent();
tf.addKeyListener(new MyKeyListener());
setClickCountToStart(1);
}

public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected, int row, int column) {
tf.setText(value.toString());
return tf;
}

public Object getCellEditorValue() {
return tf.getText();
}
}

class TblModel extends DefaultTableModel {

private List data;
private String[] header;

public TblModel(List data, String[] header) {
this.data = data;
this.header = header;
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex == 1;
}

public int getColumnCount() {
return header == null ? 0 : header.length;
}

public int getRowCount() {
return data == null ? 0 : data.size();
}

public Object getValueAt(int rowIndex, int columnIndex) {
Object[] row = (Object[]) data.get(rowIndex);
return row[columnIndex];
}

public Class getColumnClass(int columnIndex) {
if (data == null) {
return Object.class;
}
Object[] row = (Object[]) data.get(0);
return row[columnIndex] == null ? Object.class :
row[columnIndex].getClass();
}

public String getColumnName(int column) {
return header[column];
}

public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Object[] row = (Object[]) data.get(rowIndex);
row[columnIndex] = aValue;
super.fireTableCellUpdated(rowIndex, columnIndex);
}

public void addRow(Object[] data){
super.addRow(data);

javax.swing.SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
fireTableRowsInserted(0,0);
}

});



}


}

private class MyKeyListener implements KeyListener {

public void keyPressed(KeyEvent e) {int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {


Toolkit.getDefaultToolkit().beep();
System.out.println("ENTER pressed");

// THIS line cause the exception

model_001.addRow(new Object[] {"","",""});

//
}


}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
if (!Character.isDigit(e.getKeyChar())) {
Toolkit.getDefaultToolkit().beep();
e.consume();
}
}



}

public static void main(String[] args) {
JFrame frame = new NumericColumnTester();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
From: John B. Matthews on
In article
<841f1b0a-d0d8-483d-b3f0-991ab2927691(a)a27g2000prj.googlegroups.com>,
hon123456 <peterhon321(a)yahoo.com.hk> wrote:

> I got the following exception when I try to press Enter in the cell in
> order to add a row when the Enter is pressed.
>
> Exception in thread "AWT-EventQueue-0"
> java.lang.ArrayIndexOutOfBoundsException: 2 >= 2

I get the same result as markspace. If I use an unbound key, I get your
result. The problem is invoking fireTableRowsInserted() for a second
time in addRow(). I'm not sure why you override addRow(), which already
provides the required notification.

I'm not sure of your goal, but here's a simplified example of adding a
row to a DefaultTableModel via mouse or keyboard:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class NumericColumnTester extends JFrame {

private DefaultTableModel model_001 = new DefaultTableModel();
private int i;

public NumericColumnTester() {
String[] header = {"Text", "Num 1", "Num 2"};
model_001.setColumnIdentifiers(header);
model_001.addRow(nextRow());
model_001.addRow(nextRow());
JTable table = new JTable(model_001);
table.setRowSelectionAllowed(false);
JScrollPane scrollPane = new JScrollPane(table);
this.add(scrollPane);

this.add(new JButton("Add Row") {
{
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
model_001.addRow(nextRow());
}
});
}
}, BorderLayout.SOUTH);

table.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
System.out.println(" Key: " + KeyEvent.getKeyText(key));
if (key == KeyEvent.VK_F16) {
model_001.addRow(nextRow());
}
}
});
}

private Object[] nextRow() {
return new Object[] {
Character.valueOf((char)('a' + i++)),
Integer.valueOf(i),
String.valueOf(i)
};
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new NumericColumnTester();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>