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

Discussion in 'Java' started by clusardi2k@aol.com, Sep 21, 2012.

  1. Guest

    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);
    }
    }
     
    , Sep 21, 2012
    #1
    1. Advertising

  2. Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    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;
    }
     
    , Sep 21, 2012
    #2
    1. Advertising

  3. Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    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);
    }
    }
     
    , Sep 21, 2012
    #3
  4. Eric Sosman Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    On 9/21/2012 10:57 AM, 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
    d
     
    Eric Sosman, Sep 21, 2012
    #4
  5. Daniel Pitts Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    On 9/21/12 7:57 AM, 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.
     
    Daniel Pitts, Sep 21, 2012
    #5
  6. Lew Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    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
     
    Lew, Sep 21, 2012
    #6
  7. Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    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;
    }
     
    , Sep 21, 2012
    #7
  8. Lew Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    (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
     
    Lew, Sep 21, 2012
    #8
  9. Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    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. Whywould 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 blessingand my monster.

    Anyway, all of my web services are still working. Project 20 ago uses fewerweb 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 allthe 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 projectthat 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 21ago.

    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 postsin 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,
     
    , Sep 22, 2012
    #9
  10. Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    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. Whywould 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 blessingand my terrible monster.

    Anyway, all of my web services are still working. Project 21 ago uses fewerweb 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 allthe 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 projectthat 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 21ago.

    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 postsin 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,
     
    , Sep 22, 2012
    #10
  11. Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    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,
     
    , Sep 22, 2012
    #11
  12. Roedy Green Guest

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

    On Fri, 21 Sep 2012 07:57:55 -0700 (PDT), 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.
     
    Roedy Green, Sep 22, 2012
    #12
  13. Roedy Green Guest

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

    On Fri, 21 Sep 2012 10:32:15 -0700 (PDT), Lew <>
    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.
     
    Roedy Green, Sep 22, 2012
    #13
  14. Stuart Guest

    Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean

    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
     
    Stuart, Sep 23, 2012
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cluenek
    Replies:
    0
    Views:
    730
    cluenek
    Jul 18, 2003
  2. Bart_D

    Re: Edit a Boolean in JTable

    Bart_D, Apr 3, 2004, in forum: Java
    Replies:
    1
    Views:
    329
    Andrew Harker
    Apr 3, 2004
  3. J Leonard
    Replies:
    4
    Views:
    12,692
    Mark Space
    Jan 19, 2008
  4. Metre Meter
    Replies:
    7
    Views:
    379
    Metre Meter
    Aug 6, 2010
  5. Replies:
    2
    Views:
    462
Loading...

Share This Page