无知分享 http://blog.sciencenet.cn/u/monicashu 知识是个填不满的大坑 无知的精卫还是每日填着

博文

JTable

已有 14433 次阅读 2010-9-29 19:09 |个人分类:Experiments|系统分类:科研笔记| java, JTable, swing

最近想做个小工具来做结果分析,就尝试下swing了。



这个里面信息和例子还是比较丰富的



EN:http://www.java2s.com/Tutorial/Java/0240__Swing/Catalog0240__Swing.htm



CN:http://www.java2s.com/CN/Tutorial/Java/0240__Swing/Catalog0240__Swing.htm



有时候在google上折腾半天,暮然回首,这儿一小例清晰列出,无语。





不过Jtable上的例子还是不够,列的插入和删除找了些代码粘上用,还是有bug。



今儿下午认真debug了下,写了点笔记混在代码里如下。






/**

 * A table usually has two view: 


 *  1. default table model(row model), specified by coder, with table header(use func tableModel.getColumnIdentifiers()) and row data(use func tableModel.getDataVector();


 *  2. column model(use func: getColunmModel), generated by table itself, maybe using it for view.


 *   


 * The provided func table.removeColumn() only remove column in column model, which hided the column from view.


 * But the column's data remain unchanged in row model, also the column's name and position in table header.


 * So, to actually delete the column from table, we not only remove from column view, but also update the default table model.


 * 


 *  There are also view of sorter(sorter.getTableModel), 


 */


@SuppressWarnings("unchecked")


public void removeColumnAndData(JTable table, MyDefaultTableModel clusterWholeText, int vColIndex){   


  


        TableColumn col = table.getColumnModel().getColumn(vColIndex);   


        int columnModelIndex = col.getModelIndex();//column may have different index in view and table header of model?


        Vector data = clusterWholeText.getDataVector();//default model's row data


        Vector colIds = clusterWholeText.getColumnIdentifiers();//default model's table header


  


        //Remove the column from the table view(column model)   


        table.removeColumn(col);   


  


        //Remove the column header from the table model   


        colIds.removeElementAt(columnModelIndex);   


  


        //Remove the column data   


        for(int r=0; r

         Vector row = (Vector)data.get(r);   


         row.removeElementAt(columnModelIndex);   


        }   


        clusterWholeText.setDataVector(data, colIds); //automatically correct the model column's index in the TableColumn objects .  


 


        clusterWholeText.fireTableStructureChanged();  


        fitTableColumns(sentenceTable);


}







public void insertColumnAndData(JTable sentenceTable,


MyDefaultTableModel clusterWholeText, RowSorter sorter, String colName, ArrayList colData, int pos) {





Vector colIds = clusterWholeText.getColumnIdentifiers();


colIds.add(pos, colName);





Vector data = clusterWholeText.getDataVector();//default model's row data


for(int r=0; r

{


Vector row = (Vector)data.get(r);


int modelIndexOfRow = sorter.convertRowIndexToModel(r);


String value=colData.get(modelIndexOfRow).toString();


row.add(pos, value.length()<5?value:value.substring(0, 5));


}


clusterWholeText.setDataVector(data, colIds);       


    


clusterWholeText.fireTableStructureChanged();  


        fitTableColumns(sentenceTable);


}







layout一如几年前的混乱,下次有心情了再折腾了。








https://blog.sciencenet.cn/blog-248173-368238.html

上一篇:WinEdt相关
收藏 IP: .*| 热度|

0

发表评论 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 11:28

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部