Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #18877 > unrolled thread

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

Started byclusardi2k@aol.com
First post2012-09-21 07:57 -0700
Last post2012-09-22 15:30 -0700
Articles 14 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean clusardi2k@aol.com - 2012-09-21 07:57 -0700
    Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean clusardi2k@aol.com - 2012-09-21 08:01 -0700
      Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean clusardi2k@aol.com - 2012-09-21 08:32 -0700
    Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-21 11:57 -0400
    Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-09-21 10:31 -0700
      Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean clusardi2k@aol.com - 2012-09-21 11:43 -0700
        Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean Lew <lewbloch@gmail.com> - 2012-09-21 12:21 -0700
          Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean clusardi2k@aol.com - 2012-09-21 21:16 -0700
          Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean clusardi2k@aol.com - 2012-09-21 21:23 -0700
            Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean clusardi2k@aol.com - 2012-09-22 08:22 -0700
              Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean Stuart <DerTopper@web.de> - 2012-09-23 16:44 +0200
    Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean Lew <lewbloch@gmail.com> - 2012-09-21 10:32 -0700
      Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean Roedy Green <see_website@mindprod.com.invalid> - 2012-09-22 15:37 -0700
    Re: CheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean Roedy Green <see_website@mindprod.com.invalid> - 2012-09-22 15:30 -0700

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

Fromclusardi2k@aol.com
Date2012-09-21 07:57 -0700
SubjectCheckBox in Column of JTable: Exception: java.lang.String cannot be cast to java.lang.Boolean
Message-ID<e4bc8305-301d-461e-a218-5c0529c1cc67@googlegroups.com>
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);
     }
   }  

[toc] | [next] | [standalone]


#18878

Fromclusardi2k@aol.com
Date2012-09-21 08:01 -0700
Message-ID<c3c901b2-2a78-405c-bd60-baccc79cd848@googlegroups.com>
In reply to#18877
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;
             }

[toc] | [prev] | [next] | [standalone]


#18879

Fromclusardi2k@aol.com
Date2012-09-21 08:32 -0700
Message-ID<efe6469e-af3d-4980-b3f1-1d1314ea548e@googlegroups.com>
In reply to#18878
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);       
                        }   
                    } 

[toc] | [prev] | [next] | [standalone]


#18880

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-09-21 11:57 -0400
Message-ID<k3i2q3$reg$1@dont-email.me>
In reply to#18877
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

[toc] | [prev] | [next] | [standalone]


#18885

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2012-09-21 10:31 -0700
Message-ID<0w17s.127$l36.73@newsfe20.iad>
In reply to#18877
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.

[toc] | [prev] | [next] | [standalone]


#18889

Fromclusardi2k@aol.com
Date2012-09-21 11:43 -0700
Message-ID<9db3f5bc-d91a-4452-8bf6-8825d994038e@googlegroups.com>
In reply to#18885
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; 
             } 

[toc] | [prev] | [next] | [standalone]


#18890

FromLew <lewbloch@gmail.com>
Date2012-09-21 12:21 -0700
Message-ID<913cd3a2-5fea-4d32-a989-e87e267d9853@googlegroups.com>
In reply to#18889
(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

[toc] | [prev] | [next] | [standalone]


#18893

Fromclusardi2k@aol.com
Date2012-09-21 21:16 -0700
Message-ID<a36dce34-1981-450a-a73e-41672759b681@googlegroups.com>
In reply to#18890
It's possible that what appears to be a checkbox problem is a side-effect of a bigger problem.

What can I say. I am lost! Basically, one possibility is that my large project was working one second and it wasn't the next second. What could have gone 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. They 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 working, 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 services. They all work.

Here is what I have done. After making the java code in the non-working project 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 project 20 ago is still not working!  
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. Again, this new modified project is not working. It gives the same error message 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 very similar.

(1) Throwing away the NetBeans form files produced by the NetBeans Design view, 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 21 ago.

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

(3) Carefully preserving the NetBeans form files produced by the NetBeans Design 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 checkbox 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 wisest thing I can try unless you have better ideas.

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

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

Thank you,



[toc] | [prev] | [next] | [standalone]


#18894

Fromclusardi2k@aol.com
Date2012-09-21 21:23 -0700
Message-ID<06496dff-884c-45fd-93e9-d45b657dc813@googlegroups.com>
In reply to#18890
It's possible that what appears to be a checkbox problem is a side-effect of a bigger problem. 

What can I say. I am lost! Basically, one possibility is that my large project was working one second and it wasn't the next second. What could have gone 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. They 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 working, but they were. 

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

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 services. They all work. 

Here is what I have done. After making the java code in the non-working project 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 project 20 ago is still not working!   
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. Again, this new modified project is not working. It gives the same error message 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 very similar. 

(1) Throwing away the NetBeans form files produced by the NetBeans Design view, 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 21 ago. 

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

(3) Carefully preserving the NetBeans form files produced by the NetBeans Design 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 checkbox 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 wisest thing I can try unless you have better ideas. 

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

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

Thank you,

[toc] | [prev] | [next] | [standalone]


#18895

Fromclusardi2k@aol.com
Date2012-09-22 08:22 -0700
Message-ID<cdefaed4-7460-4548-96e3-6c9a3b05b5dc@googlegroups.com>
In reply to#18894
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,

[toc] | [prev] | [next] | [standalone]


#18898

FromStuart <DerTopper@web.de>
Date2012-09-23 16:44 +0200
Message-ID<k3n787$t92$1@dont-email.me>
In reply to#18895
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

[toc] | [prev] | [next] | [standalone]


#18886

FromLew <lewbloch@gmail.com>
Date2012-09-21 10:32 -0700
Message-ID<0f42c3f6-d9d7-46ce-aa22-547632743a7c@googlegroups.com>
In reply to#18877
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

[toc] | [prev] | [next] | [standalone]


#18897

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-09-22 15:37 -0700
Message-ID<99fs58ho61jndp301nm1e0ndj5jju5hvcg@4ax.com>
In reply to#18886
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. 

[toc] | [prev] | [next] | [standalone]


#18896

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-09-22 15:30 -0700
Message-ID<bves581rcefcjabk81la20so5ae75vf1po@4ax.com>
In reply to#18877
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. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web