TableColumnModelをXMLファイルで保存、復元する
Total: 3617
, Today: 1
, Yesterday: 1
Posted by aterai at
Last-modified:
Summary
JTable
のヘッダからTableColumnModel
を取得し、XMLEncoder
とXMLDecoder
を使って、XML
ファイルで保存、復元します。
Screenshot

Advertisement
Source Code Examples
class TableColumnModelPersistenceDelegate extends DefaultPersistenceDelegate {
@Override protected void initialize(
Class<?> type, Object oldInstance, Object newInstance, Encoder encoder) {
super.initialize(type, oldInstance, newInstance, encoder);
DefaultTableColumnModel m = (DefaultTableColumnModel) oldInstance;
for (int col = 0; col < m.getColumnCount(); col++) {
Object[] o = {m.getColumn(col)};
encoder.writeStatement(new Statement(oldInstance, "addColumn", o));
}
}
}
View in GitHub: Java, KotlinExplanation
JTableHeader
からTableColumnModel
を取得してXMLEncoder
でXML
ファイルに保存- 保存した
XML
ファイルをXMLDecoder
で復元することでマウスドラッグによる列の入れ替え、幅の変更、カラム名の変更など復元