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(DefaultTableModel.java:251)
at
javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.java:207)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:142)
at
com.ale.ts.view.ConfigPersonaleFrame$1.<init>(ConfigPersonaleFrame.java:42)
at
com.ale.ts.view.ConfigPersonaleFrame.<init>(ConfigPersonaleFrame.java:43)
at com.ale.ts.view.TS_UI$3.actionPerformed(TS_UI.java:100)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:357)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)
at
javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)
at java.awt.Component.processMouseEvent(Component.java:6290)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6055)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4653)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
at java.awt.Container.dispatchEventImpl(Container.java:2083)
at java.awt.Window.dispatchEventImpl(Window.java:2482)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java: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.doIntersectionPrivilege(AccessControlContext.java:87)
at
java.security.AccessControlContext$1.doIntersectionPrivilege(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.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:618)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.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().getCurrentSession();
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