• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

JTable

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to pass Vectors to a JTable. When i am passing a vector i get a ClassCastException. How to go about it.

// for creating table headers
Vector column=new Vector(3);
column.addElement("Policy No");
column.addElement("Policy Name");
column.addElement("Policy Type");

Vector fields=new Vector(10,2);

ResultSet rs=stmt.executeQuery("select pno,ptype,pcode from customer");

while(rs.next()) {
String pno=rs.getString(1);
String ptype=rs.getString(2);
String pcode=rs.getString(3);
System.out.println(pno);

fields.addElement(pno);
fields.addElement(ptype);
fields.addElement(pcode);
}

Vector rowData=new Vector(fields);

DefaultTableModel dtm=new DefaultTableModel();
table=new JTable(dtm); // Exception occured
dtm.setDataVector(rowData,column);

jsp = new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);



//Exception Occured
java.lang.ClassCastException
at javax.swing.table.DefaultTableModel.justifyRows(Unknown Source)
at javax.swing.table.DefaultTableModel.setDataVector(Unknown Source)
at com.classes.ui.CustomerDetails.viewCustPolicy(CustomerDetails.java:81
8)
at com.classes.ui.CustomerDetails.actionPerformed(CustomerDetails.java:4
42)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Unknow
n Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems you have some problem in using JTable. Please come to here:
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
for more details.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use a Vector of Vector.

Vector cellsInRow1 = new Vector();
cellsInRow1.addElement("1");
cellsInRow1.addElement("2");
cellsInRow1.addElement("3");
cellsInRow1.addElement("4);

Vector rows = new Vector();
rows.addElement(cellsInRow1);

And then in the table model, pass rows as the argument.
 
Destroy anything that stands in your way. Except this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic