Cannot add Rows java.lang.ArrayIndexOutOfBoundsException: 2 >= 2

Discussion in 'Java' started by hon123456, May 14, 2010.

  1. hon123456

    hon123456 Guest

    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);
    }
    }
     
    hon123456, May 14, 2010
    #1
    1. Advertising

  2. hon123456

    markspace Guest

    Re: Cannot add Rows java.lang.ArrayIndexOutOfBoundsException: 2 >=2

    hon123456 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.



    Sorry, I don't get an error. The cursor just moves to the next cell,
    that's all.
     
    markspace, May 14, 2010
    #2
    1. Advertising

  3. hon123456

    markspace Guest

    Re: Cannot add Rows java.lang.ArrayIndexOutOfBoundsException: 2 >=2

    hon123456 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.



    Sorry, I don't get an error. The cursor just moves to the next cell,
    that's all.
     
    markspace, May 14, 2010
    #3
  4. In article
    <>,
    hon123456 <> 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>
     
    John B. Matthews, May 14, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arjen Hoekstra
    Replies:
    0
    Views:
    566
    Arjen Hoekstra
    Aug 2, 2005
  2. Simon Bieri
    Replies:
    2
    Views:
    4,720
    Simon Bieri
    Jun 15, 2004
  3. Replies:
    3
    Views:
    2,515
    Tumbleweed
    Jul 1, 2004
  4. Harald Kirsch
    Replies:
    4
    Views:
    2,843
    Harald Kirsch
    Aug 31, 2004
  5. Replies:
    13
    Views:
    1,428
    Stuart
    Sep 23, 2012
Loading...

Share This Page