14. 59. 2. 用抽象表创建一个简单table Creating simple JTable using AbstractTableModel

import java.awt.Dimension;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;


public class CreateAbstractTableModel extends JFrame{
	private JTable m_simpleTable;
	private SimpleTableModel m_simpleTableModel;
	
	public CreateAbstractTableModel(){//开始构造
		Vector dummyMacData = new Vector(10,10);//dummy:虚拟的
		dummyMacData.addElement(new Data(new Integer(200),"李白","张灵","杜康","刘生"));//Data的构造方法
		dummyMacData.addElement(new Data(new Integer(105), "诗人", "卖歌","写作", "舞蹈"));
		m_simpleTableModel = new SimpleTableModel(dummyMacData);
		m_simpleTable = new JTable(m_simpleTableModel);
		JScrollPane scrollPane = new JScrollPane(m_simpleTable);
		getContentPane().add(scrollPane);
		
		int rows = m_simpleTable.getRowCount();
		int cols = m_simpleTable.getColumnCount();
		Object o = new Object();
		for(int i = 0; i < rows; i++){
			for(int j = 0; j < cols; j++){
				o = m_simpleTable.getValueAt(i, 0);
				
			}
			//System.out.println(o);
		}	
		System.out.println(o);
	}

	public static void main(String[] args) {
		CreateAbstractTableModel m = new CreateAbstractTableModel();
		m.setVisible(true);
	    m.setSize(new Dimension(300, 100));
	    m.validate();
	}

	class SimpleTableModel extends AbstractTableModel{//继承抽象的表模型
		public String[] m_colNames = {"A","B","C","D","E"};//表头列名
		public Class[] m_colTypes = {Integer.class,String.class,String.class,String.class,String.class,String.class};
		Vector m_macDataVector;
		

		public SimpleTableModel(Vector macDataVector){
			super();
			m_macDataVector = macDataVector;
		}
		
		
		public int getColumnCount() {//拿到列数
			return m_colNames.length;
		}

		public int getRowCount() {//拿到行数
			return m_macDataVector.size();
		}

		public void setValueAt(Object value,int row,int col){//设置单元格的值
			Data macData = (Data)m_macDataVector.elementAt(row);//返回指定索引处的组件
			switch(col){
			case 0:macData.setA((Integer)value); break;//把a设置进第一个单元格
			case 1:macData.setB((String)value); break;
			case 2:macData.setC((String)value); break;
			case 3:macData.setD((String)value); break;
			case 4:macData.setE((String)value); break;
			}
		}
		
		public String getColumnName(int col){//获得表名
			return m_colNames[col];
		}
		
		public Class getColumnClass(int col){//返回表类
			return m_colTypes[col];
		}
		
		public Object getValueAt(int row, int col) {//拿到单元格的值
			Data macData = (Data)m_macDataVector.elementAt(row);//返回指定索引处的组件
			switch(col){
			case 0: return macData.getA();
			case 1:return macData.getB();
			case 2:return macData.getC();
			case 3:return macData.getD();
			case 4:return macData.getE();
			}
			return new String();
		}
		
	}
}

class Data{//Data类
	private Integer a;
	private String b;
	private String c;
	private String d;
	private String e;
	public Data(){//空构造
	}
	
	public Data(Integer aa,String bb,String cc,String dd,String ee){//五个参数的构造
		a = aa;
		b = bb;
		c = cc;
		d = dd;
		e = ee;
	}
	
	public Integer getA(){//拿到a
		return a;
	}
	public String getB(){
		return b;
	}
	public String getC(){
		return c;
	}
	public String getD(){
		return d;
	}
	public String getE(){
		return e;
	}
	
	public void setA(Integer aa){//设置a
		a = aa;
	}
	public void setB(String bb){
		b = bb;
	}
	public void setC(String cc){
		c = cc;
	}
	public void setD(String dd){
		d = dd;
	}
	public void setE(String ee){
		e = ee;
	}
}



你可能感兴趣的:(String,vector,object,table,Integer,Class)