• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBounds Exception

 
khad M
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a JTable with 3 coulumns and 4 rows. I have also added a button which when clicked removes the selected row.
Now the problem is, if i select the last row and press the button the row gets deleted. But after this if i select any other row it throws an ArrayIndexOutOfBoundsException.
The following is the stact trace i got for the exception
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: 4 >= 4
at java.util.Vector.elementAt(Vector.java:417)
at javax.swing.table.DefaultTableModel.setValueAt(DefaultTableModel.java
:674)
at javax.swing.JTable.setValueAt(JTable.java:1734)
at javax.swing.JTable.editingStopped(JTable.java:2989)
at javax.swing.AbstractCellEditor.fireEditingStopped(AbstractCellEditor.
java:112)
at javax.swing.AbstractCellEditor.stopCellEditing(AbstractCellEditor.jav
a:68)
at javax.swing.JTable.editCellAt(JTable.java:2359)
at javax.swing.plaf.basic.BasicTableUI$MouseInputHandler.mousePressed(Ba
sicTableUI.java:464)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:22
1)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:22
0)
at java.awt.Component.processMouseEvent(Component.java:3714)
at java.awt.Component.processEvent(Component.java:3546)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2595)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2210)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1200)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:319)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:10
3)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:84)
The following is the code i have used

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.border.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class MultiLineCellExample extends JApplet {
static int columnCount=4;
/* static int rowCount=3;*/
public void init() {
main();
}
public void start() {
}
void main() {
try
{

DefaultTableModel dm = new DefaultTableModel() {
public Class getColumnClass(int columnIndex) {
return String.class;
}
};
dm.setDataVector(new Object[][]{
{"a","a","a"},
{"b","b","b"},
{"c","c","c"},
{"d","d","d"},
{"A\nA","B\nB","C\nC"}
},
new Object[]{"1","2","3"});

JTable table = new JTable( dm );
MultiLineCellEditor editor = new MultiLineCellEditor(table);
table.setDefaultEditor(String.class,editor);

TablePopupListener pop = new TablePopupListener(table);
table.addMouseListener(pop);

table.setAutoCreateColumnsFromModel(false) ;

JScrollPane scroll = new JScrollPane( table );

scroll.setBorder(new javax.swing.border.TitledBorder("Table"));


JPanel des = new JPanel() ;
des.setLayout(new BorderLayout());

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new GridLayout(7, 1));
buttonPanel.setBorder(new javax.swing.border.TitledBorder("Buttons"));
buttonPanel.setBackground(Color.gray);
/*buttonPanel.add(createButton(roct));
buttonPanel.add(createButton(acoct));
buttonPanel.add(createButton(coroct));*/

AddRowAction act = new AddRowAction(table);
InsertRowAction irt = new InsertRowAction(table);
InsertColAction ict = new InsertColAction(table);
AddColAction acoct = new AddColAction(table);
RemoveColAction coroct = new RemoveColAction(table);
RemoveRowAction roct = new RemoveRowAction(table);
//((DefaultTableModel)table.getModel()).addTableModelListener(roct);

buttonPanel.add(new JButton(act));
buttonPanel.add(new JButton(acoct));
buttonPanel.add(new JButton(ict));
buttonPanel.add(new JButton(irt));
buttonPanel.add(new JButton(coroct));
buttonPanel.add(new JButton(roct));

des.add(buttonPanel, BorderLayout.EAST);
des.add(scroll, BorderLayout.CENTER);



setContentPane(des);
}
catch (Exception e)
{
System.out.println("ArrayIndexOutOfBoundsException in main");
}
}
}

The code used for removing the row is here

/* Remove rows */
class RemoveRowAction extends AbstractAction implements ActionListener, TableModelListener{
private JTable tab;
RemoveRowAction(JTable tabtest) {
this.tab = tabtest;
putValue(Action.NAME, "Remove Selected Row");
}
public void actionPerformed(ActionEvent e) {
int row = tab.getSelectedRow() ;
int rowCount = tab.getRowCount();
//System.out.println("row = "+row);
if(row >=0 && row < rowCount)
{
((DefaultTableModel)tab.getModel()).removeRow(row);
tab.validate();

}
System.out.println("row = "+row);
System.out.println("rowCount = "+rowCount);
}

Can any one help me how to handle this exception
[ November 01, 2002: Message edited by: khad M ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to post your code before anyone can diagnose this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!