在Javas抽象表模型中显示某些列

在Javas抽象表模型中显示某些列,java,swing,Java,Swing,我试图使用JRadioButtons来显示抽象表中的特定列。 因此,我的表中有两列“已雇用的视频”和“未雇用的视频”。如果单击“显示所有雇用人员”单选按钮,我只想显示其中一列,如果用户单击“显示所有雇用人员”JRadioButton,它将显示这两列 非常感谢您的帮助。接下来,AbstractTableModel是一个存储数据的模型,JTable为您的模型提供了视图组件 因此,由于您对更改数据的显示方式感兴趣,因此可以调整视图组件(JTable): 接下来,AbstractTableModel是一

我试图使用JRadioButtons来显示抽象表中的特定列。 因此,我的表中有两列“已雇用的视频”和“未雇用的视频”。如果单击“显示所有雇用人员”单选按钮,我只想显示其中一列,如果用户单击“显示所有雇用人员”JRadioButton,它将显示这两列

非常感谢您的帮助。

接下来,
AbstractTableModel
是一个存储数据的模型,
JTable
为您的模型提供了视图组件

因此,由于您对更改数据的显示方式感兴趣,因此可以调整视图组件(
JTable
):

接下来,
AbstractTableModel
是一个存储数据的模型,
JTable
为模型提供视图组件

因此,由于您对更改数据的显示方式感兴趣,因此可以调整视图组件(
JTable
):


作为替代方法,您可以创建一个
TableColumnModel
,它提供了允许您隐藏/显示列的功能

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

/**
 * A simple column model to provide
 * easy visibility functionality, allowing columns to be easily hidden and shown
 */
public class MutableTableColumnModel extends DefaultTableColumnModel {

    private List<TableColumn> allTableColumns;

    public MutableTableColumnModel() {

        allTableColumns = new ArrayList<TableColumn>(10);

    }

    /**
     * Sets the visibility of the specified TableColumn. The call is ignored if
     * the TableColumn is not found in this column model or its visibility status
     * did not change.
     * <p>
     * @param column
     * @param visible its new visibility status
     */
    public void setColumnVisible(TableColumn column, boolean visible) {
        if (!visible) {
            super.removeColumn(column);
        } else {
            // find the visible index of the column:
            // iterate through both collections of visible and all columns, counting
            // visible columns up to the one that's about to be shown again
            int noVisibleColumns = tableColumns.size();
            int noInvisibleColumns = allTableColumns.size();
            int visibleIndex = 0;

            for (int invisibleIndex = 0; invisibleIndex < noInvisibleColumns; ++invisibleIndex) {
                TableColumn visibleColumn = (visibleIndex < noVisibleColumns ? (TableColumn) tableColumns.get(visibleIndex) : null);
                TableColumn testColumn = (TableColumn) allTableColumns.get(invisibleIndex);

                if (testColumn == column) {
                    if (visibleColumn != column) {
                        super.addColumn(column);
                        super.moveColumn(tableColumns.size() - 1, visibleIndex);
                    }
                    break;
                } else if (testColumn == visibleColumn) {
                    ++visibleIndex;
                }
            }
        }
    }

    /**
     * Makes all columns in this model visible
     */
    public void showAllColumns() {
        int noColumns = allTableColumns.size();

        for (int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
            TableColumn visibleColumn = (columnIndex < tableColumns.size() ? (TableColumn) tableColumns.get(columnIndex) : null);
            TableColumn invisibleColumn = (TableColumn) allTableColumns.get(columnIndex);

            if (visibleColumn != invisibleColumn) {
                super.addColumn(invisibleColumn);
                super.moveColumn(tableColumns.size() - 1, columnIndex);
            }
        }
    }

    /**
     * Maps the index of the column in the table model at
     * <code>modelColumnIndex</code> to the TableColumn object. There may me
     * multiple TableColumn objects showing the same model column, though this is
     * uncommon. This method will always return the first visible or else the
     * first invisible column with the specified index.
     *
     * @param modelColumnIndex index of column in table model
     * @return table column object or null if no such column in this column model
     */
    public TableColumn getColumnByModelIndex(int modelColumnIndex) {
        for (int columnIndex = 0; columnIndex < allTableColumns.size(); ++columnIndex) {
            TableColumn column = (TableColumn) allTableColumns.get(columnIndex);
            if (column.getModelIndex() == modelColumnIndex) {
                return column;
            }
        }
        return null;
    }

    /**
     * Checks wether the specified column is currently visible.
     *
     * @param aColumn column to check
     * @return visibility of specified column (false if there is no such column at
     * all. [It's not visible, right?])
     */
    public boolean isColumnVisible(TableColumn aColumn) {
        return (tableColumns.indexOf(aColumn) >= 0);
    }

    /**
     * Append <code>column</code> to the right of exisiting columns. Posts
     * <code>columnAdded</code> event.
     *
     * @param column The column to be added
     * @see #removeColumn
     * @exception IllegalArgumentException if <code>column</code> is
     * <code>null</code>
     */
    public void addColumn(TableColumn column) {
        allTableColumns.add(column);
        super.addColumn(column);
    }

    /**
     * Removes <code>column</code> from this column model. Posts
     * <code>columnRemoved</code> event. Will do nothing if the column is not in
     * this model.
     *
     * @param column the column to be added
     * @see #addColumn
     */
    public void removeColumn(TableColumn column) {
        int allColumnsIndex = allTableColumns.indexOf(column);
        if (allColumnsIndex != -1) {
            allTableColumns.remove(allColumnsIndex);
        }
        super.removeColumn(column);
    }

    /**
     * Moves the column from <code>oldIndex</code> to <code>newIndex</code>. Posts
     *  <code>columnMoved</code> event. Will not move any columns if
     * <code>oldIndex</code> equals <code>newIndex</code>.
     *
     * @param    oldIndex    index of column to be moved
     * @param    newIndex    new index of the column
     * @exception IllegalArgumentException    if either <code>oldIndex</code> or
     * <code>newIndex</code> are not in [0, getColumnCount() - 1]
     */
    public void moveColumn(int oldIndex, int newIndex) {
        if ((oldIndex < 0) || (oldIndex >= getColumnCount())
                || (newIndex < 0) || (newIndex >= getColumnCount())) {
            throw new IllegalArgumentException("moveColumn() - Index out of range");
        }

        TableColumn fromColumn = (TableColumn) tableColumns.get(oldIndex);
        TableColumn toColumn = (TableColumn) tableColumns.get(newIndex);

        int allColumnsOldIndex = allTableColumns.indexOf(fromColumn);
        int allColumnsNewIndex = allTableColumns.indexOf(toColumn);

        if (oldIndex != newIndex) {
            allTableColumns.remove(allColumnsOldIndex);
            allTableColumns.add(allColumnsNewIndex, fromColumn);
        }

        super.moveColumn(oldIndex, newIndex);
    }

    /**
     * Returns the total number of columns in this model.
     *
     * @param onlyVisible if set only visible columns will be counted
     * @return    the number of columns in the <code>tableColumns</code> array
     * @see    #getColumns
     */
    public int getColumnCount(boolean onlyVisible) {
        List<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
        return columns.size();
    }

    /**
     * Returns an <code>Enumeration</code> of all the columns in the model.
     *
     * @param onlyVisible if set all invisible columns will be missing from the
     * enumeration.
     * @return an <code>Enumeration</code> of the columns in the model
     */
    public Iterator<TableColumn> getColumns(boolean onlyVisible) {

        List<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
        return Collections.unmodifiableList(columns).iterator();

    }

    /**
     * Returns the position of the first column whose identifier equals
     * <code>identifier</code>. Position is the the index in all visible columns
     * if <code>onlyVisible</code> is true or else the index in all columns.
     *
     * @param    identifier the identifier object to search for
     * @param    onlyVisible if set searches only visible columns
     *
     * @return    the index of the first column whose identifier equals
     * <code>identifier</code>
     *
     * @exception IllegalArgumentException if <code>identifier</code> is
     * <code>null</code>, or if no <code>TableColumn</code> has this
     * <code>identifier</code>
     * @see    #getColumn
     */
    public int getColumnIndex(Object identifier, boolean onlyVisible) {
        if (identifier == null) {
            throw new IllegalArgumentException("Identifier is null");
        }

        List<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
        int noColumns = columns.size();
        TableColumn column;

        for (int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
            column = (TableColumn) columns.get(columnIndex);

            if (identifier.equals(column.getIdentifier())) {
                return columnIndex;
            }
        }

        throw new IllegalArgumentException("Identifier not found");
    }

    /**
     * Returns the <code>TableColumn</code> object for the column at
     * <code>columnIndex</code>.
     *
     * @param    columnIndex    the index of the column desired
     * @param    onlyVisible    if set columnIndex is meant to be relative to all
     * visible columns only else it is the index in all columns
     *
     * @return    the <code>TableColumn</code> object for the column at
     * <code>columnIndex</code>
     */
    public TableColumn getColumn(int columnIndex, boolean onlyVisible) {

        return (onlyVisible ? tableColumns : allTableColumns).get(columnIndex);

    }

    /**
     * Returns all the columns
     *
     * @return
     */
    public TableColumn[] getAllColumns() {

        return allTableColumns.toArray(new TableColumn[allTableColumns.size()]);

    }

    public TableColumn[] getHiddenColumns() {

        List<TableColumn> lstColumns = new ArrayList<TableColumn>(10);

        for (TableColumn column : getAllColumns()) {

            if (!isColumnVisible(column)) {

                lstColumns.add(column);

            }

        }

        return lstColumns.toArray(new TableColumn[lstColumns.size()]);

    }

    public static MutableTableColumnModel createFromTableModel(TableModel model) {

        MutableTableColumnModel columnModel = new MutableTableColumnModel();
        if (model != null) {
            for (int i = 0; i < model.getColumnCount(); i++) {
                TableColumn newColumn = new TableColumn(i);
                columnModel.addColumn(newColumn);
            }
        }

        return columnModel;

    }

}
这可以应用于
JTable
,只需使用类似于

JTable table = new JTable();
table.setAutoCreateColumnsFromModel(false);
TableModel tm = ...;
table.setModel(tm);
table.setColumnModel(MutableTableColumnModel.createFromTableModel(tm));
MutableColumnModel cm = (MutableColumnModel)table.getColumnnModel();
cm.setColumnVisible(column, false);
然后可以使用以下方法更改列的可见性状态

JTable table = new JTable();
table.setAutoCreateColumnsFromModel(false);
TableModel tm = ...;
table.setModel(tm);
table.setColumnModel(MutableTableColumnModel.createFromTableModel(tm));
MutableColumnModel cm = (MutableColumnModel)table.getColumnnModel();
cm.setColumnVisible(column, false);

例如…

作为一种替代方法,您可以创建一个
TableColumnModel
,它提供了允许您隐藏/显示列的功能

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

/**
 * A simple column model to provide
 * easy visibility functionality, allowing columns to be easily hidden and shown
 */
public class MutableTableColumnModel extends DefaultTableColumnModel {

    private List<TableColumn> allTableColumns;

    public MutableTableColumnModel() {

        allTableColumns = new ArrayList<TableColumn>(10);

    }

    /**
     * Sets the visibility of the specified TableColumn. The call is ignored if
     * the TableColumn is not found in this column model or its visibility status
     * did not change.
     * <p>
     * @param column
     * @param visible its new visibility status
     */
    public void setColumnVisible(TableColumn column, boolean visible) {
        if (!visible) {
            super.removeColumn(column);
        } else {
            // find the visible index of the column:
            // iterate through both collections of visible and all columns, counting
            // visible columns up to the one that's about to be shown again
            int noVisibleColumns = tableColumns.size();
            int noInvisibleColumns = allTableColumns.size();
            int visibleIndex = 0;

            for (int invisibleIndex = 0; invisibleIndex < noInvisibleColumns; ++invisibleIndex) {
                TableColumn visibleColumn = (visibleIndex < noVisibleColumns ? (TableColumn) tableColumns.get(visibleIndex) : null);
                TableColumn testColumn = (TableColumn) allTableColumns.get(invisibleIndex);

                if (testColumn == column) {
                    if (visibleColumn != column) {
                        super.addColumn(column);
                        super.moveColumn(tableColumns.size() - 1, visibleIndex);
                    }
                    break;
                } else if (testColumn == visibleColumn) {
                    ++visibleIndex;
                }
            }
        }
    }

    /**
     * Makes all columns in this model visible
     */
    public void showAllColumns() {
        int noColumns = allTableColumns.size();

        for (int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
            TableColumn visibleColumn = (columnIndex < tableColumns.size() ? (TableColumn) tableColumns.get(columnIndex) : null);
            TableColumn invisibleColumn = (TableColumn) allTableColumns.get(columnIndex);

            if (visibleColumn != invisibleColumn) {
                super.addColumn(invisibleColumn);
                super.moveColumn(tableColumns.size() - 1, columnIndex);
            }
        }
    }

    /**
     * Maps the index of the column in the table model at
     * <code>modelColumnIndex</code> to the TableColumn object. There may me
     * multiple TableColumn objects showing the same model column, though this is
     * uncommon. This method will always return the first visible or else the
     * first invisible column with the specified index.
     *
     * @param modelColumnIndex index of column in table model
     * @return table column object or null if no such column in this column model
     */
    public TableColumn getColumnByModelIndex(int modelColumnIndex) {
        for (int columnIndex = 0; columnIndex < allTableColumns.size(); ++columnIndex) {
            TableColumn column = (TableColumn) allTableColumns.get(columnIndex);
            if (column.getModelIndex() == modelColumnIndex) {
                return column;
            }
        }
        return null;
    }

    /**
     * Checks wether the specified column is currently visible.
     *
     * @param aColumn column to check
     * @return visibility of specified column (false if there is no such column at
     * all. [It's not visible, right?])
     */
    public boolean isColumnVisible(TableColumn aColumn) {
        return (tableColumns.indexOf(aColumn) >= 0);
    }

    /**
     * Append <code>column</code> to the right of exisiting columns. Posts
     * <code>columnAdded</code> event.
     *
     * @param column The column to be added
     * @see #removeColumn
     * @exception IllegalArgumentException if <code>column</code> is
     * <code>null</code>
     */
    public void addColumn(TableColumn column) {
        allTableColumns.add(column);
        super.addColumn(column);
    }

    /**
     * Removes <code>column</code> from this column model. Posts
     * <code>columnRemoved</code> event. Will do nothing if the column is not in
     * this model.
     *
     * @param column the column to be added
     * @see #addColumn
     */
    public void removeColumn(TableColumn column) {
        int allColumnsIndex = allTableColumns.indexOf(column);
        if (allColumnsIndex != -1) {
            allTableColumns.remove(allColumnsIndex);
        }
        super.removeColumn(column);
    }

    /**
     * Moves the column from <code>oldIndex</code> to <code>newIndex</code>. Posts
     *  <code>columnMoved</code> event. Will not move any columns if
     * <code>oldIndex</code> equals <code>newIndex</code>.
     *
     * @param    oldIndex    index of column to be moved
     * @param    newIndex    new index of the column
     * @exception IllegalArgumentException    if either <code>oldIndex</code> or
     * <code>newIndex</code> are not in [0, getColumnCount() - 1]
     */
    public void moveColumn(int oldIndex, int newIndex) {
        if ((oldIndex < 0) || (oldIndex >= getColumnCount())
                || (newIndex < 0) || (newIndex >= getColumnCount())) {
            throw new IllegalArgumentException("moveColumn() - Index out of range");
        }

        TableColumn fromColumn = (TableColumn) tableColumns.get(oldIndex);
        TableColumn toColumn = (TableColumn) tableColumns.get(newIndex);

        int allColumnsOldIndex = allTableColumns.indexOf(fromColumn);
        int allColumnsNewIndex = allTableColumns.indexOf(toColumn);

        if (oldIndex != newIndex) {
            allTableColumns.remove(allColumnsOldIndex);
            allTableColumns.add(allColumnsNewIndex, fromColumn);
        }

        super.moveColumn(oldIndex, newIndex);
    }

    /**
     * Returns the total number of columns in this model.
     *
     * @param onlyVisible if set only visible columns will be counted
     * @return    the number of columns in the <code>tableColumns</code> array
     * @see    #getColumns
     */
    public int getColumnCount(boolean onlyVisible) {
        List<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
        return columns.size();
    }

    /**
     * Returns an <code>Enumeration</code> of all the columns in the model.
     *
     * @param onlyVisible if set all invisible columns will be missing from the
     * enumeration.
     * @return an <code>Enumeration</code> of the columns in the model
     */
    public Iterator<TableColumn> getColumns(boolean onlyVisible) {

        List<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
        return Collections.unmodifiableList(columns).iterator();

    }

    /**
     * Returns the position of the first column whose identifier equals
     * <code>identifier</code>. Position is the the index in all visible columns
     * if <code>onlyVisible</code> is true or else the index in all columns.
     *
     * @param    identifier the identifier object to search for
     * @param    onlyVisible if set searches only visible columns
     *
     * @return    the index of the first column whose identifier equals
     * <code>identifier</code>
     *
     * @exception IllegalArgumentException if <code>identifier</code> is
     * <code>null</code>, or if no <code>TableColumn</code> has this
     * <code>identifier</code>
     * @see    #getColumn
     */
    public int getColumnIndex(Object identifier, boolean onlyVisible) {
        if (identifier == null) {
            throw new IllegalArgumentException("Identifier is null");
        }

        List<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
        int noColumns = columns.size();
        TableColumn column;

        for (int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
            column = (TableColumn) columns.get(columnIndex);

            if (identifier.equals(column.getIdentifier())) {
                return columnIndex;
            }
        }

        throw new IllegalArgumentException("Identifier not found");
    }

    /**
     * Returns the <code>TableColumn</code> object for the column at
     * <code>columnIndex</code>.
     *
     * @param    columnIndex    the index of the column desired
     * @param    onlyVisible    if set columnIndex is meant to be relative to all
     * visible columns only else it is the index in all columns
     *
     * @return    the <code>TableColumn</code> object for the column at
     * <code>columnIndex</code>
     */
    public TableColumn getColumn(int columnIndex, boolean onlyVisible) {

        return (onlyVisible ? tableColumns : allTableColumns).get(columnIndex);

    }

    /**
     * Returns all the columns
     *
     * @return
     */
    public TableColumn[] getAllColumns() {

        return allTableColumns.toArray(new TableColumn[allTableColumns.size()]);

    }

    public TableColumn[] getHiddenColumns() {

        List<TableColumn> lstColumns = new ArrayList<TableColumn>(10);

        for (TableColumn column : getAllColumns()) {

            if (!isColumnVisible(column)) {

                lstColumns.add(column);

            }

        }

        return lstColumns.toArray(new TableColumn[lstColumns.size()]);

    }

    public static MutableTableColumnModel createFromTableModel(TableModel model) {

        MutableTableColumnModel columnModel = new MutableTableColumnModel();
        if (model != null) {
            for (int i = 0; i < model.getColumnCount(); i++) {
                TableColumn newColumn = new TableColumn(i);
                columnModel.addColumn(newColumn);
            }
        }

        return columnModel;

    }

}
这可以应用于
JTable
,只需使用类似于

JTable table = new JTable();
table.setAutoCreateColumnsFromModel(false);
TableModel tm = ...;
table.setModel(tm);
table.setColumnModel(MutableTableColumnModel.createFromTableModel(tm));
MutableColumnModel cm = (MutableColumnModel)table.getColumnnModel();
cm.setColumnVisible(column, false);
然后可以使用以下方法更改列的可见性状态

JTable table = new JTable();
table.setAutoCreateColumnsFromModel(false);
TableModel tm = ...;
table.setModel(tm);
table.setColumnModel(MutableTableColumnModel.createFromTableModel(tm));
MutableColumnModel cm = (MutableColumnModel)table.getColumnnModel();
cm.setColumnVisible(column, false);

例如…

另一种方法是创建一个类来管理列的可见性

看看这本书。您可以在任何
JTable
上使用
TableColumnManager
,只需一行代码


TableColumnManager
还允许用户通过右键单击表格标题来隐藏/显示
TableColumns

另一种方法是创建一个类来管理列的可见性

看看这本书。您可以在任何
JTable
上使用
TableColumnManager
,只需一行代码


TableColumnManager
还允许用户通过右键单击表格标题来隐藏/显示
TableColumns

您还可以定义一个
TableColumnModel
,它具有“隐藏”和“显示”列功能……短期内需要做更多工作,长期收益,只是说-作为一种替代方法;)因此,我应该在table类之外进行此操作,并将其添加到DataManager类中,在DataManager类中,我将所有内容添加到框架中?您还可以定义一个
TableColumnModel
,它具有“隐藏”和“显示”列功能…短期内需要更多工作,长期需要更多工作,只是说-作为一种替代方法;)因此,我应该在table类之外执行此操作,并将其添加到DataManager类中,在该类中,我将所有内容添加到框架?+1中,但我认为
createFromTableModel(…)
方法需要使用TableModel中的值设置TableColumn的标题值:
newColumn.setHeaderValue(model.getColumnName(i))
@camickr是的,我想是的,直接从
JTable
createDefaultColumnsFromModel
方法中撕下代码(仅此示例;)+1,但我认为
createFromTableModel(…)
方法需要使用TableModel中的值设置TableColumn的标题值:
newColumn.setHeaderValue(model.getColumnName(I))
@camickr是的,我想是的,直接从
JTable
createDefaultColumnsFromModel
方法中撕下代码(仅此示例;)+我喜欢那样。我想知道是否最好创建一个“代理”列模型并将其应用到表中,或者是否会推断出有太多干扰。唯一的问题是如果你有几十个专栏的话:P(是的,我工作的地方,几十个是一个小集合)@MadProgrammer,
唯一的问题是如果你有几十个专栏的话
-这不是关于这个的吗。我想我可以用Darryl来限制弹出菜单的大小。我可能不会太担心,但我建议我们像Windows资源管理器那样做,只需要有限数量的“基本/通用”列,然后提供一个“更多…”选项,显示与所有列的对话,但这是我们的需要+1我喜欢。我想知道是否最好创建一个“代理”列模型并将其应用到表中,或者是否会推断出有太多干扰。唯一的问题是如果你有几十个专栏的话:P(是的,我工作的地方,几十个是一个小集合)@MadProgrammer,
唯一的问题是如果你有几十个专栏的话
-这不是关于这个的吗。我想我总是可以使用Darryl来限制弹出菜单的大小。我可能不会太担心,但我建议我们做Windows资源管理器所做的事情,所以限制“基本/通用”列的数量,然后提供一个“更多…”选项,显示与所有列的对话,但这是我们的需要