Welcome to Dream.In.Code
Become a Java Expert!

Join 149,478 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,655 people online right now. Registration is fast and FREE... Join Now!




JCheckBox as column renderer

 
Reply to this topicStart new topic

JCheckBox as column renderer, ClassCastException

gyron
17 May, 2007 - 06:54 AM
Post #1

D.I.C Head
**

Joined: 9 Jan, 2007
Posts: 61


My Contributions
I have a JTable that retrives values from a database table. All works fine except that i have a column whose data type is Boolean/tinyint(1) and would want a check box to be used as the renderer. I have overidden the getColumnClass method thus:

CODE

public Class getColumnClass(int c) {
      
       return getValueAt(0, c).getClass();
   }


the check box doesn't apear. if i try to use a checkbox as the renderer for that column, i get a ClassCastException!

I tried to explicitly state the class type like this:
CODE

public Class getColumnClass(int c) {
       if(c==10){
          
           Object value=getValueAt(0,c);
           Boolean bol=new Boolean(value.toString());
           return bol.getClass();
       }
       return getValueAt(0, c).getClass();
   }


And it pops up the following error:
java.lang.ClassCastException: java.lang.Integer
at javax.swing.JTable$BooleanRenderer.getTableCellRendererComponent(JTable.java:3614)
at javax.swing.JTable.prepareRenderer(JTable.java:3928)
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:142)
at javax.swing.JComponent.paintComponent(JComponent.java:742)
at javax.swing.JComponent.paint(JComponent.java:1005)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4963)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4916)
at javax.swing.JComponent._paintImmediately(JComponent.java:4859)
at javax.swing.JComponent.paintImmediately(JComponent.java:4666)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:451)
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:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

User is offlineProfile CardPM
+Quote Post

Programmist
RE: JCheckBox As Column Renderer
17 May, 2007 - 11:07 AM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Look through this tutorial and it will describe exactly how to do it:
http://java.sun.com/docs/books/tutorial/ui...ents/table.html
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month