kooyee ‘s blog

开源软件, 众人努力的结晶, 全人类的共同财富
posts - 103, comments - 55, trackbacks - 0, articles - 66
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

[Swing] Table 操作

Posted on 2008-02-29 21:07 kooyee 阅读(274) 评论(0)  编辑  收藏 所属分类: Swing/Applet

关于table的使用方法可以参考sun的官方网站:

http://java.sun.com/docs/books/tutorial/uiswing/components/table.html



这里补充的主要是model的使用,去自定义table的format。

在DefaultTableModel 基础上改进model。 在定义new DefaultTableModel时修改override它的内部method

部分代码:

String[] columnNames = {"ID""Date""Value""Qty""Total"}
            Vector columnNamesV 
= new Vector(Arrays.asList(columnNames));
            Vector rowData 
= new Vector();

table.setModel(
new javax.swing.table.DefaultTableModel(rowData,
     columnNamesV) 
{

//通过这个method使table能够根据cell的object类型来决定显示格式,例如boolean以checkbox格式显示,而不是true/false的文字。
    public Class getColumnClass(int c) {
              Vector col 
= (Vector)getDataVector().get(0);
     
return col.get(c).getClass();
          }


//使全部cell为不能编辑
    public boolean isCellEditable(int rowIndex, int columnIndex) {
     
return false;
    }

   }
);



只有注册用户登录后才能发表评论。


网站导航: