[groovy-user] how to clear all rows in a jtable ?

148 views
Skip to first unread message

citron

unread,
May 19, 2010, 3:51:50 PM5/19/10
to us...@groovy.codehaus.org

How can I clear all rows in a jTable with groovy?


This throws a class cast exception
((DefaultTableModel)table.getModel()).setNumRows(0);

Exception in thread "AWT-EventQueue-0"
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast
object 'groovy.model.DefaultTableModel@1bd19f4' with class
'groovy.model.DefaultTableModel' to class
'javax.swing.table.DefaultTableModel'
at
org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:321)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:602)
at
LogParser$_go_closure2_closure8_closure11_closure16_closure18.doCall(LogParser.groovy:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at
org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at groovy.lang.Closure.call(Closure.java:276)
at
org.codehaus.groovy.runtime.ConvertedClosure.invokeCustom(ConvertedClosure.java:51)
at
org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:79)
at $Proxy5.actionPerformed(Unknown Source)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at
java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



and this is not working
table.getModel().setNumRows(0)

Exception in thread "AWT-EventQueue-0"
org.codehaus.groovy.runtime.InvokerInvocationException:
groovy.lang.MissingMethodException: No signature of method:
groovy.model.DefaultTableModel.setNumRows() is applicable for argument
types: (java.lang.Integer) values: [0]
at
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:95)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at
org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at groovy.lang.Closure.call(Closure.java:276)
at
org.codehaus.groovy.runtime.ConvertedClosure.invokeCustom(ConvertedClosure.java:51)
at
org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:79)
at $Proxy5.actionPerformed(Unknown Source)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at
java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Caused by: groovy.lang.MissingMethodException: No signature of method:
groovy.model.DefaultTableModel.setNumRows() is applicable for argument
types: (java.lang.Integer) values: [0]
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)
at
org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at
LogParser$_go_closure2_closure8_closure11_closure16_closure18.doCall(LogParser.groovy:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
... 33 more





Thanks for any help!!

--
View this message in context: http://old.nabble.com/how-to-clear-all-rows-in-a-jtable---tp28613248p28613248.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


--
You received this message because you are subscribed to the Google Groups "Groovy Users" group.
To post to this group, send email to groov...@googlegroups.com.
To unsubscribe from this group, send email to groovy-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/groovy-user?hl=en.

citron

unread,
May 21, 2010, 5:29:49 AM5/21/10
to us...@groovy.codehaus.org

I guess its not possible to clear the contents of a Groovy JTable?
--
View this message in context: http://old.nabble.com/how-to-clear-all-rows-in-a-jtable---tp28613248p28631556.html

Tim Yates

unread,
May 21, 2010, 5:35:59 AM5/21/10
to us...@groovy.codehaus.org
Set num rows is marked as obsolete in the Java documentation (since 1.3)

http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html#setNumRows(int)

Have you tried what it suggests, and use setRowCount instead?

table.model.rowCount = 0

Tim

Ian Cairns

unread,
May 21, 2010, 5:36:46 AM5/21/10
to us...@groovy.codehaus.org
citron wrote:
> I guess its not possible to clear the contents of a Groovy JTable?

Just install a new, empty, model with JTable's setModel()?

Ian.

Tim Yates

unread,
May 21, 2010, 5:42:37 AM5/21/10
to us...@groovy.codehaus.org
Actually, ignore me

The first exception shows it's a groovy.model.DefaultTableModel, not a javax.swing.table.DefaultTableModel

So rowCount doesn't exist

I'd try Ian's suggestion

Tim

Anders Viklund

unread,
May 21, 2010, 6:57:38 AM5/21/10
to us...@groovy.codehaus.org
javax.swing.table.DefaultTableModel.getRowCount()                      exists
groovy.model.DefaultTableModel.getRowCount()                           exists

javax.swing.table.DefaultTableModel.setRowCount(int rowCount)     exists
groovy.model.DefaultTableModel.setRowCount(int rowCount)          does NOT exist, why?


From: tim....@gmail.com
Date: Fri, 21 May 2010 10:42:37 +0100
To: us...@groovy.codehaus.org
Subject: Re: [groovy-user] how to clear all rows in a jtable ?


Actually, ignore me

The first exception shows it's a groovy.model.DefaultTableModel, not a javax.swing.table.DefaultTableModel

So rowCount doesn't exist

I'd try Ian's suggestion

Tim

On Fri, May 21, 2010 at 10:35 AM, Tim Yates <tim....@gmail.com> wrote:
Set num rows is marked as obsolete in the Java documentation (since 1.3)

http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html#setNumRows(int)

Have you tried what it suggests, and use setRowCount instead?

table.model.rowCount = 0

Tim


On Fri, May 21, 2010 at 10:29 AM, citron <viklund...@hotmail.com> wrote:

I guess its not possible to clear the contents of a Groovy JTable?
--
View this message in context: http://old.nabble.com/how-to-clear-all-rows-in-a-jtable---tp28613248p28631556.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email






Hotmail: Trusted email with powerful SPAM protection. Sign up now.

Anders Viklund

unread,
May 21, 2010, 7:08:11 AM5/21/10
to us...@groovy.codehaus.org
Ian,

how would you change this code according your suggestion?

def swing = new SwingBuilder()
def tableData
def jtable

jtable = table(){
                           tableModel( list:tableData){
                                propertyColumn( header:"File", propertyName:"file", cellRenderer: new CustomTableCellRenderer(),editable: false )
                                propertyColumn( header:"Line", propertyName:"line", cellRenderer: new CustomTableCellRenderer(),preferredWidth:1,editable: false )
                                propertyColumn( header:"Date", propertyName:"date", cellRenderer: new CustomTableCellRenderer(),editable: false)
                                propertyColumn( header:"Location", propertyName:"location", cellRenderer: new CustomTableCellRenderer(),preferredWidth:5,editable: false )
                                propertyColumn( header:"Process", propertyName:"process", cellRenderer: new CustomTableCellRenderer(),editable: false)
                                propertyColumn( header:"ConversationId", propertyName:"conversationid", cellRenderer: new CustomTableCellRenderer(),editable: false)
                                propertyColumn( header:"Status", propertyName:"status", cellRenderer: new CustomTableCellRenderer(),editable: false)
                               
                           }  
                     }

...// Add row to table
def rows = jtable.getModel().getRowsModel().getValue()
def rowsModel = jtable.getModel().getRowsModel()
rows.add( [ file: file_, line:line_,date: date_,location:location_,process: process_,conversationid: conversationid_,status:status_ ] )
jtable.getModel().getRowsModel().setValue( rows )
jtable.getModel().fireTableDataChanged()
..

// Delete rows from table ??
...




> Date: Fri, 21 May 2010 10:36:46 +0100
> From: i...@iancairns.org
> To: us...@groovy.codehaus.org
> Subject: Re: [groovy-user] how to clear all rows in a jtable ?

>
> citron wrote:
> > I guess its not possible to clear the contents of a Groovy JTable?
>
> Just install a new, empty, model with JTable's setModel()?
>
> Ian.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>


Hotmail: Trusted email with powerful SPAM protection. Sign up now.

--

citron

unread,
May 21, 2010, 8:32:59 AM5/21/10
to us...@groovy.codehaus.org

Problem solved!

As usual, the solution is very simple with Groovy.


jtable.model.getRows().clear()
jtable.getModel().fireTableDataChanged()
--
View this message in context: http://old.nabble.com/how-to-clear-all-rows-in-a-jtable---tp28613248p28633187.html
Sent from the groovy - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Reply all
Reply to author
Forward
0 new messages