Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Help with JTable ... ClassCastException (http://www.velocityreviews.com/forums/t946007-help-with-jtable-classcastexception.html)

Luiss 05-06-2012 10:03 AM

Help with JTable ... ClassCastException
 
Hi all,

I'm writing a GUI to show a JTable cointaining data from a db table
(after I'll add also buttons to Add, Update and Delete rows). I need to
have different table fields and not only strings.

I've used Hibernate to do the select and then passed the list to a
Vector containing ConfigPersonale objects (my table bean).

I get this exception running the GUI:

********************************
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
com.ale.ts.persistence.ConfigPersonale cannot be cast to java.util.Vector
at
javax.swing.table.DefaultTableModel.justifyRows(De faultTableModel.java:251)
at
javax.swing.table.DefaultTableModel.setDataVector( DefaultTableModel.java:207)
at javax.swing.table.DefaultTableModel.<init>(Default TableModel.java:142)
at
com.ale.ts.view.ConfigPersonaleFrame$1.<init>(Conf igPersonaleFrame.java:42)
at
com.ale.ts.view.ConfigPersonaleFrame.<init>(Config PersonaleFrame.java:43)
at com.ale.ts.view.TS_UI$3.actionPerformed(TS_UI.java :100)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at
javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at
javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton. java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Bas icMenuItemUI.java:809)
at
javax.swing.plaf.basic.BasicMenuItemUI$Handler.mou seReleased(BasicMenuItemUI.java:850)
at java.awt.Component.processMouseEvent(Component.jav a:6290)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:605 5)
at java.awt.Container.processEvent(Container.java:203 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:4653)
at java.awt.Container.dispatchEventImpl(Container.jav a:2097)
at java.awt.Component.dispatchEvent(Component.java:44 81)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4236)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4166)
at java.awt.Container.dispatchEventImpl(Container.jav a:2083)
at java.awt.Window.dispatchEventImpl(Window.java:2482 )
at java.awt.Component.dispatchEvent(Component.java:44 81)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:648)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:607)
at java.awt.EventQueue$1.run(EventQueue.java:605)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at
java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:621)
at java.awt.EventQueue$2.run(EventQueue.java:619)
at java.security.AccessController.doPrivileged(Native Method)
at
java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 618)
at
java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at
java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at
java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
********************************



****************************************
public class ConfigPersonaleFrame extends JFrame {

private JPanel contentPane;
private QueryConfigPersonale queryConfpers = new QueryConfigPersonale();
private JTable table;

public ConfigPersonaleFrame() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
setAlwaysOnTop(true);
setTitle("Configurazione Personale");
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

table = new JTable();
String[] columnNames_a = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q"};
Vector<String> columnNames = new
Vector<String>(Arrays.asList(columnNames_a));
Vector<ConfigPersonale> dataTable = queryConfpers.getConfigPersonale();

table.setModel(new DefaultTableModel(
dataTable, columnNames) {
Class[] columnTypes = new Class[] {
Integer.class, String.class, String.class, Double.class,
Double.class, Integer.class, Double.class, String.class, Double.class,
Double.class, Double.class, Double.class, Double.class, Double.class,
Double.class, Boolean.class, Timestamp.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
});

contentPane.add(table);

//TODO: Add + Update + Delete
}
}

*********************************************

public class QueryConfigPersonale {
private Session session;
private Query query;
private List<ConfigPersonale> configPersList;

//select * from ConfigPersonale
@SuppressWarnings("unchecked")
public Vector<ConfigPersonale> getConfigPersonale() {
session = SessionFactoryUtil.getSessionFactory().getCurrentS ession();
session.beginTransaction();
query = session.createQuery("from ConfigPersonale");

configPersList = query.list();
session.getTransaction().commit();

return new Vector<ConfigPersonale>(configPersList);
}
}

*********************************************

Please could you help ?

Thanks and best regards,
Luiss

Stefan Ram 05-06-2012 11:09 AM

Re: Help with JTable ... ClassCastException
 
Luiss <luiss@aol.com> writes:
>new DefaultTableModel(dataTable, columnNames)


dataTable dovrebbe essere un vettore di vettori,
non un vettore di ConfigPersonale.


Luiss 05-06-2012 10:18 PM

Re: Help with JTable ... ClassCastException
 
Il 06/05/2012 13:09, Stefan Ram ha scritto:
> Luiss<luiss@aol.com> writes:
>> new DefaultTableModel(dataTable, columnNames)

>
> dataTable dovrebbe essere un vettore di vettori,
> non un vettore di ConfigPersonale.
>


ottimo grazie mille

Roedy Green 05-07-2012 02:14 AM

Re: Help with JTable ... ClassCastException
 
On Sun, 06 May 2012 12:03:46 +0200, Luiss <luiss@aol.com> wrote,
quoted or indirectly quoted someone who said :

>Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:


see
http://mindprod.com/jgloss/runerrorm...SCASTEXCEPTION

for general information on them.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Programmers love to create simplified replacements for HTML.
They forget that the simplest language is the one you
already know. They also forget that their simple little
markup language will bit by bit become even more convoluted
and complicated than HTML because of the unplanned way it grows.
..

Lew 05-07-2012 03:43 AM

Re: Help with JTable ... ClassCastException
 
On 05/06/2012 03:03 AM, Luiss wrote:
> Hi all,
>
> I'm writing a GUI to show a JTable cointaining data from a db table (after
> I'll add also buttons to Add, Update and Delete rows). I need to have
> different table fields and not only strings.
>
> I've used Hibernate to do the select and then passed the list to a Vector
> containing ConfigPersonale objects (my table bean).
>
> I get this exception running the GUI:
>
> ********************************
> Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
> com.ale.ts.persistence.ConfigPersonale cannot be cast to java.util.Vector


Don't use 'Vector' anyway, and do perform all GUI work on the Event Dispatch
Thread.

In this code:

> public class QueryConfigPersonale {
> private Session session;
> private Query query;
> private List<ConfigPersonale> configPersList;
>
> //select * from ConfigPersonale
> @SuppressWarnings("unchecked")
> public Vector<ConfigPersonale> getConfigPersonale() {
> session = SessionFactoryUtil.getSessionFactory().getCurrentS ession();
> session.beginTransaction();
> query = session.createQuery("from ConfigPersonale");
>
> configPersList = query.list();
> session.getTransaction().commit();
>
> return new Vector<ConfigPersonale>(configPersList);
> }
> }


you use old-fashioned Hibernate ('Session' instead of 'EntityManager'). Use
the JPA style.

Don't suppress unchecked warnings. That's very bad. How do you know it's safe
to do that? You have absolutely no documentation in your code to explain why
it's safe. That's bad.

The Hibernate query returns a list already. Why convert it to a 'Vector'?

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg


All times are GMT. The time now is 01:57 PM.

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