File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Deleting Row from JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Deleting Row from JTable" Watch "Deleting Row from JTable" New topic
Author

Deleting Row from JTable

Nasir Baig
Greenhorn

Joined: Apr 06, 2009
Posts: 4
Hi,

I just started working in JAVA. I am creating a simple GUI, in which, jTable displays the messages (id, text, etc...)

I am getting exception while removing rows from JTable.

I am using JTable to display the messages, which come very fast. (At once I can get 100 messages same time).

Basically when any message comes with type = ADD_MESSAGE, i add a row in jTable.
When any message comes with type = DELETE_MESSAGE, I remove the row from the jTable.

First I put all these messages into a queue and a separate thread handling this message_queue.


Here is the some part of my code:

message_queue # queue of messages...

For each message ... Thread calls a function call Handle_Message(message)

-----------------------------------------------------------------------------------------------------------------

It removes the row correctly without any error. But it throws an exception while painting a jTable, I guess,

Here is the output:

-----------------------------------------------------------------------------------------------------

Message: PUBLISH|ID=1022|NAME=message1|TYPE=ADD_MESSAGE|MESSAGE_ID=#698|
Row Added...
Message: PUBLISH|ID=1023|NAME=message2|TYPE=ADD_MESSAGE|MESSAGE_ID=#699|
Row Added...
Message: PUBLISH|ID=1024|NAME=message3|TYPE=ADD_MESSAGE|MESSAGE_ID=#700|
Row Added...
Message: PUBLISH|ID=1025|NAME=message4|TYPE=ADD_MESSAGE|MESSAGE_ID=#701|
Row Added...
Message: PUBLISH|ID=1026|NAME=message5|TYPE=ADD_MESSAGE|MESSAGE_ID=#702|
Row Added...
Message: PUBLISH|ID=1027|NAME=message6|TYPE=ADD_MESSAGE|MESSAGE_ID=#703|
Row Added...
Message: PUBLISH|ID=1028|NAME=message7|TYPE=ADD_MESSAGE|MESSAGE_ID=#704|
Row Added...
Message: PUBLISH|ID=1029|NAME=message8|TYPE=ADD_MESSAGE|MESSAGE_ID=#705|
Row Added...
Message: PUBLISH|ID=1030|NAME=message9|TYPE=ADD_MESSAGE|MESSAGE_ID=#706|
Row Added...
Message: PUBLISH|ID=1031|NAME=message10|TYPE=ADD_MESSAGE|MESSAGE_ID=#707|
Row Added...

Message: PUBLISH|ID=1032|NAME=message1|TYPE=DELETE_MESSAGE|MESSAGE_ID=#698|
Row Deleted...
Message: PUBLISH|ID=1033|NAME=message2|TYPE=DELETE_MESSAGE|MESSAGE_ID=#699|
Row Deleted...
Message: PUBLISH|ID=1034|NAME=message3|TYPE=DELETE_MESSAGE|MESSAGE_ID=#700|
Row Deleted...
Error in application
Java::JavaLang::ArrayIndexOutOfBoundsException - java.lang.ArrayIndexOutOfBoundsException: 7 >= 7
java.lang.ArrayIndexOutOfBoundsException: 7 >= 7
at java.util.Vector.elementAt(Vector.java:432)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:621)
at javax.swing.JTable.getValueAt(JTable.java:1903)
at javax.swing.JTable.prepareRenderer(JTable.java:3911)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2072)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1897)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:154)
at javax.swing.JComponent.paintComponent(JComponent.java:743)
at javax.swing.JComponent.paint(JComponent.java:1006)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JComponent._paintImmediately(JComponent.java:4890)
at javax.swing.JComponent.paintImmediately(JComponent.java:4676)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:477)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:114)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

Message: PUBLISH|ID=1035|NAME=message4|TYPE=DELETE_MESSAGE|MESSAGE_ID=#701|
Row Deleted...
Message: PUBLISH|ID=1036|NAME=message5|TYPE=DELETE_MESSAGE|MESSAGE_ID=#702|
Row Deleted...
Message: PUBLISH|ID=1037|NAME=message6|TYPE=DELETE_MESSAGE|MESSAGE_ID=#703|
Row Deleted...
Message: PUBLISH|ID=1038|NAME=message7|TYPE=DELETE_MESSAGE|MESSAGE_ID=#704|
Row Deleted...
Message: PUBLISH|ID=1039|NAME=message8|TYPE=DELETE_MESSAGE|MESSAGE_ID=#705|
Row Deleted...
Message: PUBLISH|ID=1040|NAME=message9|TYPE=DELETE_MESSAGE|MESSAGE_ID=#706|
Row Deleted...
Message: PUBLISH|ID=1041|NAME=message10|TYPE=DELETE_MESSAGE|MESSAGE_ID=#707|
Row Deleted...

----------------------------------------------------------------------------------------------------------------

But if I put wait (or sleep) for one sec, I don't see any exception. But that delays an app, if there are
more messages coming.

Please let me know if anyone know how to fix this.

In order to do that I created MyTableModel(extending DefaultTableModel) so that I can catch the exception
in ValueAt() method.

But some how it's not calling getValueAt( ) method from MyTableModel,but it is calling from DefaultTableModel.

Please let me know.

Thanks

Nas

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2247
    
    7

Are you getting the error only when you try to delete the first or last table row? If so you need to cater this in your Remove_Row method. Do check your for loop < vs <=


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Nasir Baig
Greenhorn

Joined: Apr 06, 2009
Posts: 4
Thanks for reply,

As I said I don't get any error in remove code. It removes the row correctly. Here is the loop:



I get an exception when one of java thread try to repaint the table after removing row.

----------------------------------------------------------------------------------------------------------------
Message: PUBLISH|ID=1032|NAME=message1|TYPE=DELETE_MESSAGE|MESSAGE_ID=#698|
Row Deleted...
Message: PUBLISH|ID=1033|NAME=message2|TYPE=DELETE_MESSAGE|MESSAGE_ID=#699|
Row Deleted...
Message: PUBLISH|ID=1034|NAME=message3|TYPE=DELETE_MESSAGE|MESSAGE_ID=#700|
Row Deleted...
Java::JavaLang::ArrayIndexOutOfBoundsException - java.lang.ArrayIndexOutOfBoundsException: 7 >= 7
java.lang.ArrayIndexOutOfBoundsException: 7 >= 7
at java.util.Vector.elementAt(Vector.java:432)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:621)

at javax.swing.JTable.getValueAt(JTable.java:1903)
at javax.swing.JTable.prepareRenderer(JTable.java:3911)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2072)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1897)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:154)
at javax.swing.JComponent.paintComponent(JComponent.java:743)
at javax.swing.JComponent.paint(JComponent.java:1006)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JComponent._paintImmediately(JComponent.java:4890)
at javax.swing.JComponent.paintImmediately(JComponent.java:4676)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:477)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:114)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Message: PUBLISH|ID=1035|NAME=message4|TYPE=DELETE_MESSAGE|MESSAGE_ID=#701|
Row Deleted...
Message: PUBLISH|ID=1036|NAME=message5|TYPE=DELETE_MESSAGE|MESSAGE_ID=#702|
Row Deleted...
Message: PUBLISH|ID=1037|NAME=message6|TYPE=DELETE_MESSAGE|MESSAGE_ID=#703|
Row Deleted...
Message: PUBLISH|ID=1038|NAME=message7|TYPE=DELETE_MESSAGE|MESSAGE_ID=#704|
Row Deleted...
Message: PUBLISH|ID=1039|NAME=message8|TYPE=DELETE_MESSAGE|MESSAGE_ID=#705|
Row Deleted...
Message: PUBLISH|ID=1040|NAME=message9|TYPE=DELETE_MESSAGE|MESSAGE_ID=#706|
Row Deleted...
Message: PUBLISH|ID=1041|NAME=message10|TYPE=DELETE_MESSAGE|MESSAGE_ID=#707|
Row Deleted...
---------------------------------------------------------------------------------------------------------------

Please let me know.

Thanks

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Deleting Row from JTable
 
Similar Threads
jtable
regarding JTable data
Validate JTable edit
How do I implement my own table model (using JTable)?
jTable, Table Models, Data : Can anyone explain how to use them?