使用预定义模型显示JTable中的特定列

时间:2015-09-27 05:02:19

标签: java swing jtable

我有一个带有10列标题的JTable:“A”,“d”,“e”,“f”,“B”,“g”,“h”,“C”,“i”,“j “。我希望在第一个视图中JTable只显示”A“,”B“,”C“,当我点击ViewAll按钮时我有两个JButton所有列显示,当点击隐藏Jtable时只显示三列”A“,” B“,”C“headers.how可以做到吗?我的GridTableModel是:

public abstract class GridTableModel<T> extends AbstractTableModel {

private static final long serialVersionUID = 4283080272635443348L;

private List<T> rows = new ArrayList<T>();

/**
 * The property used to find real index of rows that currently are shown to user. 
 */
private int offset;

public abstract String[] getColumnNames();

@Override
public String getColumnName(int column) {
    return getColumnNames()[column];
}

@Override
public int getColumnCount() {
    return getColumnNames().length;
}

@Override
public int getRowCount() {
    return rows == null ? 0 : rows.size();
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    if (rows.size() > 0) {
        return getValueAt(rows.get(rowIndex), rowIndex, columnIndex);
    }
    return null;
}

public void setData(List<T> results, int offset) {
    this.rows = results;
    this.offset = offset;
    fireTableDataChanged();
}

public T get(int row) {
    return rows.size() > 0 ? rows.get(row) : null;
}

public abstract Object getValueAt(T t, int rowIndex, int columnIndex);

public List<T> get(int[] rows) {
    List<T> list = new ArrayList<T>();
    for (int row : rows) {
        list.add(this.rows.get(row));
    }
    return list;
}

public int getOffset() {
    return offset;
}

public void setOffset(int offset) {
    this.offset = offset;
}

public List<T> getRows() {
    return rows;
}

@Override
public Class<?> getColumnClass(int columnIndex) {
    if (1==1) {
        return Object.class;
    }
    return getValueAt(0, columnIndex).getClass();
}

}

0 个答案:

没有答案