Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   CheckBox in Column of JTable: Exception: java.lang.String cannot becast to java.lang.Boolean (http://www.velocityreviews.com/forums/t952507-checkbox-in-column-of-jtable-exception-java-lang-string-cannot-becast-to-java-lang-boolean.html)

clusardi2k@aol.com 09-21-2012 02:57 PM

CheckBox in Column of JTable: Exception: java.lang.String cannot becast 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.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:778)
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:728)
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);
}
}


clusardi2k@aol.com 09-21-2012 03:01 PM

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;
}

clusardi2k@aol.com 09-21-2012 03:32 PM

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);
}
}

Eric Sosman 09-21-2012 03:57 PM

Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean
 
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

Daniel Pitts 09-21-2012 05:31 PM

Re: CheckBox in Column of JTable: Exception: java.lang.String cannotbe cast to java.lang.Boolean
 
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.


Lew 09-21-2012 05:32 PM

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

clusardi2k@aol.com 09-21-2012 06:43 PM

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;
}

Lew 09-21-2012 07:21 PM

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

clusardi2k@aol.com 09-22-2012 04:16 AM

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,





clusardi2k@aol.com 09-22-2012 04:23 AM

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,


All times are GMT. The time now is 09:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.