Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > CheckBox in Column of JTable: Exception: java.lang.String cannot becast to java.lang.Boolean

Reply
Thread Tools

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

 
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      09-21-2012
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.getTableCellRen dererComponent(JTable.java:5412)
at javax.swing.JTable.prepareRenderer(JTable.java:573 5)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Basi cTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Bas icTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTab leUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.ja va:161)
at javax.swing.JComponent.paintComponent(JComponent.j ava:77
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintToOffscreen(JComponent .java:5221)
at javax.swing.RepaintManager$PaintManager.paintDoubl eBuffered(RepaintManager.java:1482)
at javax.swing.RepaintManager$PaintManager.paint(Repa intManager.java:1413)
at javax.swing.RepaintManager.paint(RepaintManager.ja va:1206)
at javax.swing.JComponent._paintImmediately(JComponen t.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent .java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:72
at javax.swing.RepaintManager.prePaintDirtyRegions(Re paintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManag er.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run( RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava: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.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 671)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.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_CLOS E);
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);
}
}

 
Reply With Quote
 
 
 
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      09-21-2012
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;
}
 
Reply With Quote
 
 
 
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      09-21-2012
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);
}
}
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      09-21-2012
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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-21-2012
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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-21-2012
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
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      09-21-2012
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;
}
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-21-2012
(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
 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      09-22-2012
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,




 
Reply With Quote
 
clusardi2k@aol.com
Guest
Posts: n/a
 
      09-22-2012
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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
disable checkbox list checkbox Vikram ASP .Net 1 01-25-2006 02:59 PM
Text on Checkbox below the checkbox tshad ASP .Net 0 04-14-2005 11:26 PM
Cannot add a nested relation or an element column to a table containing a SimpleContent column. Random ASP .Net 1 11-19-2004 11:36 PM
Templated column cannot access datasource column ASP .Net 2 11-10-2003 03:58 PM
Convert an MS Access Yes/No column to a checkbox column in C# datagrid Gregory Rampton ASP .Net Datagrid Control 0 08-06-2003 04:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57