我想在表中添加一些列(Swing JTable)。其中一些将具有默认大小(例如250),其他将被隐藏(因此它们的大小将为0)。我使用这段代码:
model = new DefaultTableModel();
table = new JTable(model);
setAutoResizeMode(AUTO_RESIZE_OFF);
for (int i = 1; i < COLUMN_NAMES.length; i++) {
model.addColumn(COLUMN_NAMES[i]);
if (show[i]) show(index);
else hide(index);
}
........
private void hide(int index) {
TableColumn column = getColumnModel().getColumn(index);
column.setMinWidth(0);
column.setMaxWidth(0);
column.setWidth(0);
column.setPreferredWidth(0);
doLayout();
}
private void show(int index) {
final int width = 250;
column.setMinWidth(15);
column.setMaxWidth(width);
column.setWidth(width);
column.setPreferredWidth(width);
doLayout();
}
问题是当显示表格时,显示所有列(没有隐藏),它们的大小不是250,但它们的大小都相同。
如何获得想要的效果?
答案 0 :(得分:16)
我想你想隐藏你要设置的列
时,你的方法命名不正确column.setMinWidth(0);
column.setMaxWidth(0);
column.setPreferredWidth(0);
但是在您的代码中,当您想要显示完全相反的列时,您正在执行此操作。
此外,无需在TableColumn上调用此“setWidth”方法,请阅读TableColumn#setWidth(int)。
答案 1 :(得分:12)
JTable#removeColumn仅从JTable视图中移除列,更多信息请参见此example
答案 2 :(得分:7)
而不是重新发明轮子你可能会考虑使用支持隐藏列的JXTable(在the SwingX project中),与ui-control一起使用以允许用户动态地隐藏/显示它们 - 以及一堆其他有用的东西:)。
答案 3 :(得分:6)
尝试这样的事情,例如:
myTableModel = new DefaultTableModel();
myTableModel.setColumnIdentifiers(new Object[]{"ID", "Name"});
JTable myTable = new JTable(myTableModel);
// remember to save the references
TableColumn myTableColumn0 = guiLoteryNumbersTable.getColumnModel().getColumn(0);
TableColumn myTableColumn1 = guiLoteryNumbersTable.getColumnModel().getColumn(1);
//...
// remove temporary the column ("hide")
myTable.getColumnModel().removeColumn(myTableColumn1);
// then you restore that column when you need it ("show")
myTable.getColumnModel().addColumn(myTableColumn1);
这是隐藏专栏的最佳方法。
答案 4 :(得分:0)
HashMap<String,TableColumn> hashMap_columns = new HashMap<String,TableColumn>();
DefaultTableColumnModel defaultTableColumnModel = (DefaultTableColumnModel)jtable.getColumnModel();
Enumeration<TableColumn> enumeration = defaultTableColumnModel.getColumns();
while (enumeration.hasMoreElements())
{
TableColumn tableColumn = enumeration.nextElement();
hashMap_columns.put((String)tableColumn.getIdentifier(),tableColumn);
}
public void setColumnVisible(String identifier, boolean setVisible)
{
TableColumn tableColumn = hashMap_columns.get(identifier);
if (setVisible)
{
// using a sorted map removes the need to check column index/position
SortedMap<Integer,TableColumn> sortedMap = new TreeMap<Integer,TableColumn>();
// retreive all visible columns
Enumeration<TableColumn> enumeration = defaultTableColumnModel.getColumns();
while (enumeration.hasMoreElements())
{
TableColumn column = enumeration.nextElement();
sortedMap.put(column.getModelIndex(),column);
}
// add the column of interest to the sorted map
sortedMap.put(tableColumn.getModelIndex(),tableColumn);
// remove all visible columns
for (TableColumn column: sortedMap.values())
{
defaultTableColumnModel.removeColumn(column);
}
// add all previously visible columns as well as the column of interest
for (TableColumn column: sortedMap.values())
{
defaultTableColumnModel.addColumn(column);
}
}
else
{
defaultTableColumnModel.removeColumn(tableColumn);
}
}
答案 5 :(得分:0)
public class TableColumnHider {
private final JTable table;
private final TableColumnModel tcm;
private final Map hiddenColumns;
public TableColumnHider(JTable table) {
this.table = table;
tcm = table.getColumnModel();
hiddenColumns = new HashMap();
}
public void hide(String columnName, String keySig) {
int index = tcm.getColumnIndex(columnName);
TableColumn column = tcm.getColumn(index);
hiddenColumns.put(columnName, column);
hiddenColumns.put(keySig + columnName, new Integer(index));
tcm.removeColumn(column);
}
public void show(String columnName, String keySig) {
Object o = hiddenColumns.remove(columnName);
if (o == null) {
return;
}
tcm.addColumn((TableColumn) o);
o = hiddenColumns.remove(keySig + columnName);
if (o == null) {
return;
}
int column = ((Integer) o).intValue();
int lastColumn = tcm.getColumnCount() - 1;
if (column < lastColumn) {
tcm.moveColumn(lastColumn, column);
}
}
}