0

I have a JTable with 10 columns headers: "A","d","e","f","B","g","h","C","i","j".I want in first view JTable Show only "A","B","C" and I have two JButton when clicked on ViewAll Button all columns show and when clicked on hide Jtable show only three columns with "A","B","C" headers.how can do it?my GridTableModel is:

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();
}

}

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
Fereshteh
  • 55
  • 8
  • 1
    JTable can do it, but you're going have to do some work to make it happen. Basically, your TableModel needs to hold ALL the data you want to display. You then need to add/remove TableColumn's from the ColumnModel – MadProgrammer Sep 27 '15 at 05:42
  • If this is not a [duplicate](http://stackoverflow.com/q/10088853/230513), please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that shows your current approach. – trashgod Sep 27 '15 at 13:15

0 Answers0