//表格的类
package JTSwin.lc.vv;
import java.util.*;
import javax.swing.table.*;
public class MyJTable extends AbstractTableModel{
public final static int FIRST_NAME = 0;
public final static int SEX = 1;
public final static int DOB = 2;//出生日期
public final static int CERTIFICATTON = 3;//证书
public final static int SALARY = 4;//薪水
public final static boolean PROGRAMMER = true;//程序员
public final static boolean INSTRUCTOR = false;//讲师
//列标题
public final static String[] columHeaders = {
"姓名","性别","出生日期","是否是程序员","薪水"
};
//表格数据
public Object[] [] dataValues =
{
{
"李操","男",
new GregorianCalendar(1988,Calendar.JANUARY,8).getTime(),
new Boolean(INSTRUCTOR),new Float(90000)
},
{
"朱磊","男",
new GregorianCalendar(1988,Calendar.JULY,10).getTime(),
new Boolean(INSTRUCTOR),new Float(80000)
},
{
"蒋伟","男",
new GregorianCalendar(1988,Calendar.APRIL,20).getTime(),
new Boolean(PROGRAMMER),new Float(50000)
},
{
"陈定","男",
new GregorianCalendar(1988,Calendar.APRIL,20).getTime(),
new Boolean(INSTRUCTOR),new Float(50000)
},
{
"PA","男",
new GregorianCalendar(1988,Calendar.APRIL,20).getTime(),
new Boolean(PROGRAMMER),new Float(50000)
},
{
"余时隆","男",
new GregorianCalendar(1988,Calendar.APRIL,20).getTime(),
new Boolean(INSTRUCTOR),new Float(50000)
},
};
//构造器
public MyJTable(){
}
//表格属性
public int getColumnCount() {//返回表的列数
return dataValues[0].length;
}
public int getRowCount() {//返回表的行数
return dataValues.length;
}
public Object getValueAt(int row, int column) {//返回列和行的单元格的值
return dataValues[row][column];
}
public String getColumName(int col){//标题名字
return columHeaders[col];
}
public Class getColClass(int col){//构造返回类型
Class colDataType = super.getColumnClass(col);//返回传进来的对象的类
if(col == FIRST_NAME||col == SEX){
colDataType = java.lang.String.class;
}
if(col == DOB){
colDataType = java.util.Date.class;
}
if(col == CERTIFICATTON){
colDataType = java.lang.Boolean.class;
}
if(col == SALARY){
colDataType = Float.class;
}
return colDataType;
}
}
//调用的类
package JTSwin.lc.vv;
import java.awt.*;
import javax.swing.*;
public class TestMyJTable extends JFrame {
JTable mt;
public TestMyJTable(){
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
MyJTable tmt = new MyJTable();
mt = new JTable(tmt);
JScrollPane jsp = new JScrollPane(mt);
pane.add(jsp,BorderLayout.CENTER );
}
public static void main(String[] args) {
TestMyJTable tm = new TestMyJTable();
tm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tm.setTitle("人员表");
tm.setSize(500,150);
tm.setVisible(true);
}
}
问题如题~求助,谢谢各位高手了
------解决方案--------------------------------------------------------
为方便说明,先做如下改动:
一.MyJTable中的
//表格数据
public Object[] [] dataValues
改为:
//表格数据
public static Object[] [] dataValues
二.TestMyJTable的构造函数中的
mt = new JTable(tmt);
改为
mt = new JTable(MyJTable.dataValues, MyJTable.columHeaders);
然后运行,OK了。