日期:2014-05-20  浏览次数:20804 次

郁闷3天--一个JAVA问题:表格不能显示在Frame中
各位高手:我是个初学者,不要见笑啊!我的程序如下,编译没问题,但是运行时,表格不能显示。谁能帮我解决啊!!!先谢谢了!!!
import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
import   javax.swing.table.AbstractTableModel;
public   class   JdbcTest   {

JFrame   frm=null;
JScrollPane   jsp=null;
JTable   tb=null;
JComboBox   cmb=null;

JdbcTest()   {//构造函数
//   TODO   Auto-generated   constructor   stub
frm=new   JFrame( "JDBC   Test ");
Container   cp=frm.getContentPane();
MyTable   tbm=new   MyTable();
tb=new   JTable(tbm);
cmb=new   JComboBox();
cmb.addItem( "man ");
cmb.addItem( "woman ");
tb.getColumnModel().getColumn(2).setCellEditor(new   DefaultCellEditor(cmb));
tb.setPreferredScrollableViewportSize(new   Dimension(300,200));
jsp=new   JScrollPane(tb);
frm.setBounds(100,100,500,400);
cp.add(jsp);
frm.setVisible(true);
frm.addWindowListener(new   WindowAdapter(){
public   void   windowClosing(WindowEvent   e)
{
System.exit(0);
}
});

}//构造函数

public   static   void   main(String[]   args)   {
//   TODO   Auto-generated   method   stub
new   JdbcTest();
}
class   MyTable   extends   AbstractTableModel//定义表格
{
String[]   head={ "MNo ", "MName ", "Sex ", "Age ", "Class "};
Object[][]   datacol={
{1, "jake ",cmb,22, "0633032 "},
{2, "lucy ",cmb,21, "0633031 "}
};
public   int   getColumnCount()
{
return   head.length;
}
public   int   getRowCount()
{
return   datacol.length;
}
public   Object   getValueAt(int   row,int   col)
{
return   datacol[row][col];
}
public   Class   getColumnClass(int   col)
{
return   getValueAt(0,col).getClass();
}
public   String   getColumnName(int   col)
{
return   head[col];
}
public   boolean   isCellEditable(int   row,int   col)
{
return   true;
}
public   void   setValueAt(Object   value,int   row,int   col)
{
datacol[row][col]=value;
fireTableCellUpdated(row,col);
}
}//定一表格

}


------解决方案--------------------
有異常你沒看見麼?

getColumnClass()方法更改如下:

public Class getColumnClass(int col)
{
if(getValueAt(0,col) != null)
return getValueAt(0,col).getClass();
else
return Object.class;
}
------解决方案--------------------
你把cmb当成值赋了,cmd只是编辑器,它的值 "man "和 "woman“才是它的值.
结帖吧