| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.bss; |
| 27 |
|
| 28 |
import java.util.HashMap; |
| 29 |
import java.util.Map; |
| 30 |
|
| 31 |
import javax.swing.JTable; |
| 32 |
import javax.swing.table.TableColumn; |
| 33 |
import javax.swing.table.TableColumnModel; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class TableColumnHider { |
| 40 |
|
| 41 |
private JTable table; |
| 42 |
private TableColumnModel tcm; |
| 43 |
private Map<String, IndexedColumn> hidden = new HashMap<String, IndexedColumn>(); |
| 44 |
|
| 45 |
public TableColumnHider(JTable table) { |
| 46 |
this.table = table; |
| 47 |
this.tcm = this.table.getColumnModel(); |
| 48 |
} |
| 49 |
|
| 50 |
public void hide(String columnName) { |
| 51 |
int index = tcm.getColumnIndex(columnName); |
| 52 |
TableColumn column = tcm.getColumn(index); |
| 53 |
IndexedColumn ic = new IndexedColumn(index, column); |
| 54 |
if (hidden.put(columnName, ic) != null) { |
| 55 |
throw new IllegalArgumentException("Duplicate column name."); |
| 56 |
} |
| 57 |
tcm.removeColumn(column); |
| 58 |
} |
| 59 |
|
| 60 |
public void show(String columnName) { |
| 61 |
IndexedColumn ic = hidden.remove(columnName); |
| 62 |
if (ic != null) { |
| 63 |
tcm.addColumn(ic.column); |
| 64 |
int lastColumn = tcm.getColumnCount() - 1; |
| 65 |
if (ic.index < lastColumn) { |
| 66 |
tcm.moveColumn(lastColumn, ic.index); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
private static class IndexedColumn { |
| 72 |
|
| 73 |
private Integer index; |
| 74 |
private TableColumn column; |
| 75 |
|
| 76 |
public IndexedColumn(Integer index, TableColumn column) { |
| 77 |
this.index = index; |
| 78 |
this.column = column; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
} |