|||
最近想做个小工具来做结果分析,就尝试下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
Vector
//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
Vector
colIds.add(pos, colName);
Vector
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一如几年前的混乱,下次有心情了再折腾了。
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-25 10:13
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社