• Post Reply Bookmark Topic Watch Topic
  • New Topic

exception while creating JTables  RSS feed

 
Karan Raj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I am getting the following exception with my program.please chek this once.
------------------------------------------------------------
Exception in thread "main" java.lang.ClassCastException
at javax.swing.table.DefaultTableModel.justifyRows DefaultTableModel.jav
a:238)
at javax.swing.table.DefaultTableModel.setDataVector(DefaultTableModel.j
ava:194)
at javax.swing.table.DefaultTableModel.<init>(DefaultTableModel.java:131
)
at guidemo.<init>(guidemo.java:37)
at guidemo.main(guidemo.java:60)
----------------------------------------------------------------
i am tried to create a table with vectors.my code is as follows
code:-
-----
class guidemo extends JFrame{
private JTable data_tab;
private JLabel ptitle;
private Container c;
private Vector vdata;
private Vector vcolname;

public guidemo(){
super("EST Project Demo");
DefaultTableModel dtm;
c=getContentPane();
c.setLayout(null);
c.setSize(300,200);//intialising the contentpane

ptitle=new JLabel("EST project Details");
ptitle.setBounds(60,30,150,40);
c.add(ptitle);
setTableData();//intialising the vectors with some data
dtm=new DefaultTableModel(vdata,vcolname);
data_tab=new JTable(dtm);//setting table model
//data_tab.setPreferredScrollableViewportSize(new Dimension(500, 100));
c.add(data_tab);

WindowListener wndCloser = new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}

public static void main(String[] args)
{
System.out.println("Hello World!");
setDefaultLookAndFeelDecorated(true);
new guidemo();
}

public void setTableData(){//intialise vectors with some data
vdata=new Vector();
vcolname=new Vector();
vdata.removeAllElements();
vdata.addElement(new dat("Eliuh",90,900,"uy89"));
vdata.addElement(new dat("Eryuh",920,900,"uy89"));
vdata.addElement(new dat("Elddh",940,900,"uy89"));
vdata.addElement(new dat("Elideu",90,900,"uy89"));
vdata.addElement(new dat("Elwciuh",760,900,"uy89"));
vdata.addElement(new dat("Elhhiuh",90,900,"uy89"));
vdata.addElement(new dat("Elicuh",100,900,"uy89"));
vdata.addElement(new dat("Eliuech",90,900,"uy89"));
vcolname.addElement(new cname("NAME","QStart","Qend","PID"));
}
}

class dat{
String name,gid;
int val,val2;
public dat(String n,int v,int v1,String g){
name=n;
val=v;
val2=v1;
gid=g;
}
}
class cname{
String s1,s2,s3,s4;
cname(String q, String w, String t, String y){
s1=q;
s2=w;
s3=t;
s4=y;
}
}
----------------------------------------------------------
I am not able to understand why i am getting that exception.if any body have solution for my problem it will be greatly helpful for me.
thanks,
karan
 
Vijay
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got rid of the bug in ur code. The following code should compile.
KRR

 
Joe Gilvary
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception occurs when doing the layout, and it's a ClassCastException. I think you need to write your own TableCellRenderer to display your dat and cname objects because they are not the kinds of objects that JTable knows how to display by default, e.g., String, Boolean, or Number.
HTH,
Joe
 
Karan Raj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Thanks for ur help.
vijay u r code is working fine..but i am not getting the column names for that table.
i am just learning jtables .I am not getting column names when i try to create a table with vectors.since a week i am trying this.
Can anyone explain what are specific things i need to take care while creating a table.

--karan
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I tried vijay's code but could only get the first column to show. Maybe I copied it wrong, or someone can spot my problem. I don't see any need for any special renderer. The last two methods merely generate the vectors and look ok to me. I used a little print loop to prove that vdata is ok.
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karan,
I then reduced the size of your program by setting up arrays with a minimum support program. You need the scroll pane to show the headings but again I am not sure why it does not scroll when you reduce the JFrame size with the mouse.

[ March 20, 2004: Message edited by: Eddie Vanda ]
 
Eddie Vanda
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karan,
Sorry to take up more space, but I found the problem with the column count. You had added a vector level to the column names that was not necessary. I have also added a scroll pane so that your headings show up.
 
Karan Raj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vanda,
Thanks for your help. your solution is very clear
Karan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!