This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable

 
Rangarajan Balasubramanian
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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)
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • 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.
 
Palash Ray
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic