f



CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean

Hello, I have discovered a hidden error. My project was working for awhile, but then I started to get the below error.

My error comes from the fact that I'm using a checkbox in a jtable, and I'm using the below "getColumnClass".

Thank you,

compile:
run:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
	at javax.swing.JTable$BooleanRenderer.getTableCellRendererComponent(JTable.java:5412)
	at javax.swing.JTable.prepareRenderer(JTable.java:5735)
	at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
	at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
	at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
	at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
	at javax.swing.JComponent.paintComponent(JComponent.java:778)
	at javax.swing.JComponent.paint(JComponent.java:1054)
	at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
	at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
	at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
	at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
	at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
	at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701)
	at java.awt.EventQueue.access$000(EventQueue.java:102)
	at java.awt.EventQueue$3.run(EventQueue.java:662)
	at java.awt.EventQueue$3.run(EventQueue.java:660)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:671)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)


--------
The below project runs perfectly. Without seeing my large project, how can I modify the below project to get my other large project to work.

package sorttable;

import javax.swing.*;
   import javax.swing.table.*;
   import java.awt.*;
import java.util.ArrayList;

   public class SortTable {
     public static void main(String args[]) {
       Runnable runner = new Runnable() {
        public void run() {
           JFrame frame = new JFrame("Sorting JTable");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           Object rows[][] = {
               {"VOD",  "Vodafone Group", 26.02, new Boolean(false), new Boolean(false)},
               {"SUNW", "Sun Microsystems", 3.86, new Boolean(true), new Boolean(false)},
               {"EBAY", "eBay", 41.57, new Boolean(false), new Boolean(false)},
               {"GOOG", "Google", 388.33, new Boolean(false), new Boolean(false)},
               {"MSFT", "Microsoft", 26.56, new Boolean(true), new Boolean(false)},
               {"NOK", "Nokia Corp", 17.13, new Boolean(false), new Boolean(false)},
               {"ORCL", "Oracle Corp.", 12.52, new Boolean(true), new Boolean(false)},
               {"TWX",  "Time Warner", 17.66, new Boolean(false), new Boolean(false)},
               {"AMZN", "Amazon", 41.28, new Boolean(false), new Boolean(false)},
               {"YHOO", "Yahoo!", 37.69, new Boolean(false), new Boolean(false)}
             };
           String columns[] = {"Symbol", "Name", "Price", "OK", "Cheap"};
           TableModel model =
               new DefaultTableModel(rows, columns) {
             public Class getColumnClass(int column) {
               Class returnValue;
               if ((column >= 0) && (column < getColumnCount())) {
                 returnValue = getValueAt(0, column).getClass();
               } else {
                 returnValue = Object.class;
               }
               return returnValue;
             }
           };

           JTable table = new JTable(model);
           RowSorter<TableModel> sorter =
             new TableRowSorter<TableModel>(model);
           
           java.util.List sortKeys = new ArrayList();
           sortKeys.add(new RowSorter.SortKey(3, SortOrder.ASCENDING));
           sorter.setSortKeys(sortKeys);

           table.setRowSorter(sorter);
           JScrollPane pane = new JScrollPane(table);
           frame.add(pane, BorderLayout.CENTER);
           frame.setSize(300, 150);
           frame.setVisible(true);
         }
       };
       EventQueue.invokeLater(runner);
     }
   }  

0
clusardi2k (508)
9/21/2012 2:57:55 PM
comp.lang.java.programmer 52711 articles. 1 followers. Post Follow

13 Replies
6575 Views

Similar Articles

[PageSpeed] 20

FYI: The following doesn't prevent the error message:

             public Class getColumnClass(int column) {
               Class returnValue;
               if (column > 2)
                 return Boolean.class;
               else if ((column >= 0) && (column < getColumnCount())) {
                 returnValue = getValueAt(0, column).getClass();
               } else {
                 returnValue = Object.class;
               }
               return returnValue;
             }
0
clusardi2k (508)
9/21/2012 3:01:16 PM
Within DefaultTableModel, I think I have to somehow redefine the below method. Do you have any suggestions on how to do this.

Thanks,

                   public Object getValueAt(int row,int column)   
                    {
                        switch(column)      
                        { 
                            case ...: return (a String);       
                        }   
                    } 
0
clusardi2k (508)
9/21/2012 3:32:57 PM
On 9/21/2012 10:57 AM, clusardi2k@aol.com wrote:
> [... an exception is thrown ...]]
> The below project runs perfectly. Without seeing my large project, how can I modify the below project to get my other large project to work.

     Are you serious?

     Q: My program X fails.  Here's a program Y that works.  I won't
        show you X, but please tell me how to make X work like Y.

     A: Copy Y to X.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid
0
esosman2 (3096)
9/21/2012 3:57:52 PM
On 9/21/12 7:57 AM, clusardi2k@aol.com wrote:
> Hello, I have discovered a hidden error. My project was working for awhile, but then I started to get the below error.
>
> My error comes from the fact that I'm using a checkbox in a jtable, and I'm using the below "getColumnClass".
>
> Thank you,
>
> compile:
> run:
> Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
You are returning "Boolean.class" somewhere, but the actual content of 
the cell is a String.  That is *exactly* what the exception says. Your 
other program isn't updating the column with anything but a Boolean.

Make sure nothing in your code puts a String where it doesn't belong. 
Unit tests can be a helpful tool in that regard.

0
9/21/2012 5:31:39 PM
On Friday, September 21, 2012 7:57:55 AM UTC-7, (unknown) wrote:
> Hello, I have discovered a hidden error. My project was working for awhile, but then I started to get the below error.
> [snip] --------
> 
> The below project runs perfectly. Without seeing my large project, how can I modify the below project to get my other large project to work.

I'm pretty sure you need to see your large project in order to get it to work.

[snip]
>              public Class getColumnClass(int column) {
>                Class returnValue;
>            java.util.List sortKeys = new ArrayList();

Don't use raw types.

-- 
Lew
0
lewbloch (1508)
9/21/2012 5:32:15 PM
On Friday, September 21, 2012 1:31:41 PM UTC-4, Daniel Pitts wrote:
> Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: 
> java.lang.String cannot be cast to java.lang.Boolean You are 
> returning "Boolean.class" somewhere, but the actual content of the cell is a 
> String.

If I use the below code I don't get the error, but I get "false" strings in the column.
             public Class getColumnClass(int column) { 
               Class returnValue; 
               if (column == 1) returnValue = String.class;
               else if ((column >= 1) && (column < getColumnCount())) { 
                 returnValue = getValueAt(0, column).getClass(); 
               } else { 
                 returnValue = Object.class; 
               } 
               return returnValue; 
             } 
0
clusardi2k (508)
9/21/2012 6:43:04 PM
(unknown) wrote:
> If I use the below code I don't get the error, but I get "false" strings in the column.
> 
>              public Class getColumnClass(int column) { 
> 
>                Class returnValue; 

Don't use raw types.

-- 
Lew
0
lewbloch (1508)
9/21/2012 7:21:30 PM
It's possible that what appears to be a checkbox problem is a side-effect o=
f a bigger problem.

What can I say. I am lost! Basically, one possibility is that my large proj=
ect was working one second and it wasn't the next second. What could have g=
one wrong.

I'm going to walk down and try the following reasoning path.

Since I have been working on my toy project for months, I have a series of =
old/saved backup copies of my earlier projects.

Basically, it appears that 20 of my last projects are no longer working. Th=
ey probably were working, and now they are giving the same error mentioned =
in my first posting above. I would not have saved non-working projects. Why=
 would I save non-working projects. So, 20 of my backup copies are not work=
ing, but they were.

But, saved backup project 21 ago is still working! This is both my blessing=
 and my monster.

Anyway, all of my web services are still working. Project 20 ago uses fewer=
 web services than project 21 ago. I.E.: My problem is not with my web serv=
ices. They all work.

Here is what I have done. After making the java code in the non-working pro=
ject 20 ago look exactly like the java code in the working project 21 ago, =
I looked at a windiff of the two projects. Windiff allows me to compare all=
 the files in the two projects. I use windiff a lot.

(This new project is used in option (2) below.)

The 2 projects only differ in the non-java code now, but the non-working pr=
oject 20 ago is still not working! =20
But, when I compare the resulting new modified old 20 project ago with the =
already working 21 project ago some of the "non-java code" still differs. A=
gain, this new modified project is not working. It gives the same error mes=
sage that I have been seeing when this problem first reared its head. I.E.:=
 A checkbox in a JTable gives an error message.

At this juncture, I have three options to try. It appears to be the wisest =
things to try. I have partly started option (1). Options (1) and (3) are ve=
ry similar.

(1) Throwing away the NetBeans form files produced by the NetBeans Design v=
iew, quickly copy and paste all the 'java code' from my most recent project=
 that appears to have just stop working to the working old backup project 2=
1 ago.

(2) Over write the 2 non-java files (dot xml files) in the new modified pro=
ject with the non-java files in the old working 21 project ago.

(3) Carefully preserving the NetBeans form files produced by the NetBeans D=
esign view, copy and paste all the 'java code' from my most recent project =
that appears to have just stop working to the working old backup project 21=
 ago.

That's what I'm going to try today. Again, I am lost! I feel I'm hitting a =
dead-end. Yesterday, I followed the recommended Internet solution for my ch=
eckbox in a JTable error message, and it did not work, see my earlier posts=
 in this thread. Today, I'm going to try the above 3 options. It's the wise=
st thing I can try unless you have better ideas.

What do you think. Is NetBeans flaky. Could there be problems in the java/N=
etBeans libraries. Did I find a bug. Did I do something that I shouldn't ha=
ve.

I'm still looking for a really good idea that will help me.

Thank you,




0
clusardi2k (508)
9/22/2012 4:16:40 AM
It's possible that what appears to be a checkbox problem is a side-effect o=
f a bigger problem.=20

What can I say. I am lost! Basically, one possibility is that my large proj=
ect was working one second and it wasn't the next second. What could have g=
one wrong.=20

I'm going to walk down and try the following reasoning path.=20

Since I have been working on my toy project for months, I have a series of =
old/saved backup copies of my earlier projects.=20

Basically, it appears that 20 of my last projects are no longer working. Th=
ey probably were working, and now they are giving the same error mentioned =
in my first posting above. I would not have saved non-working projects. Why=
 would I save non-working projects. So, 20 of my backup copies are not work=
ing, but they were.=20

But, saved backup project 21 ago is still working! This is both my blessing=
 and my terrible monster.=20

Anyway, all of my web services are still working. Project 21 ago uses fewer=
 web services than project 20 ago. I.E.: My problem is not with my web serv=
ices. They all work.=20

Here is what I have done. After making the java code in the non-working pro=
ject 20 ago look exactly like the java code in the working project 21 ago, =
I looked at a windiff of the two projects. Windiff allows me to compare all=
 the files in the two projects. I use windiff a lot.=20

(This new project is used in option (2) below.)=20

The 2 projects only differ in the non-java code now, but the non-working pr=
oject 20 ago is still not working!  =20
But, when I compare the resulting new modified old 20 project ago with the =
already working 21 project ago some of the "non-java code" still differs. A=
gain, this new modified project is not working. It gives the same error mes=
sage that I have been seeing when this problem first reared its head. I.E.:=
 A checkbox in a JTable gives an error message.=20

At this juncture, I have three options to try. It appears to be the wisest =
things to try. I have partly started option (1). Options (1) and (3) are ve=
ry similar.=20

(1) Throwing away the NetBeans form files produced by the NetBeans Design v=
iew, quickly copy and paste all the 'java code' from my most recent project=
 that appears to have just stop working to the working old backup project 2=
1 ago.=20

(2) Over write the 2 non-java files (dot xml files) in the new modified pro=
ject with the non-java files in the old working 21 project ago.=20

(3) Carefully preserving the NetBeans form files produced by the NetBeans D=
esign view, copy and paste all the 'java code' from my most recent project =
that appears to have just stop working to the working old backup project 21=
 ago.=20

That's what I'm going to try today. Again, I am lost! I feel I'm hitting a =
dead-end. Yesterday, I followed the recommended Internet solution for my ch=
eckbox in a JTable error message, and it did not work, see my earlier posts=
 in this thread. Today, I'm going to try the above 3 options. It's the wise=
st thing I can try unless you have better ideas.=20

What do you think. Is NetBeans flaky. Could there be problems in the java/N=
etBeans libraries. Did I find a bug. Did I do something that I shouldn't ha=
ve.=20

I'm still looking for a really good idea that will help me.=20

Thank you,
0
clusardi2k (508)
9/22/2012 4:23:29 AM
The project that was not working yesterday is working today. I made no changes to the project at all. The exact same project that was giving me an error all day yesterday is working today.

What do you think about this. What should I do. How should I investigate this further. Should I be weary of using this project in the future.

Thank you,
0
clusardi2k (508)
9/22/2012 3:22:44 PM
On Fri, 21 Sep 2012 07:57:55 -0700 (PDT), clusardi2k@aol.com wrote,
quoted or indirectly quoted someone who said :

>new Boolean(false)
Just a thought. These would be more efficiently coded as
Boolean.FALSE
-- 
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex. 


0
see_website (5876)
9/22/2012 10:30:41 PM
On Fri, 21 Sep 2012 10:32:15 -0700 (PDT), Lew <lewbloch@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>
>I'm pretty sure you need to see your large project in order to get it to work.

What OP needs to do is create SSCCE that is small, but has enough of
the logic of the big project to demonstrate the error.  Without seeing
your code, and without being able to run it, all we can do is guess
what you might have done wrong. That is a rather large number of
possibilities.

see http://mindprod.com/jgloss/sscce.html
-- 
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex. 


0
see_website (5876)
9/22/2012 10:37:33 PM
On 9/22/12 clusardi2k wrote:
> The project that was not working yesterday is working today.
 > I made no changes to the project at all. The exact same project
> that was giving me an error all day yesterday is working today.
>
> What do you think about this. What should I do. How should I
> investigate this further. Should I be weary of using this
> project in the future.
>
> Thank you,


Don't give up hope!

It's only natural that every once in a while you have to struggle with 
your toolchain. Especially if you have to work with Eclipse. My 2 cents: 
If anything stops working unexpectedly,
(1) restart Eclipse,
(2) clean any caches of Eclipse and restart it,
(3) do a complete checkout of the last revision of your project into 
another directory and restart Eclipse,
(4) start again with step (1).

Regards,
Stuart

0
DerTopper (398)
9/23/2012 2:44:23 PM
Reply: