How to make a columns in JTable Invisible for Swing Java

前端 未结 8 1417
半阙折子戏
半阙折子戏 2020-12-03 04:44

I have designed one GUI in which I have used one JTable from which I have to make 2 columns invisible . How should I do that ?

相关标签:
8条回答
  • 2020-12-03 04:48

    I tried 2 possible solutions that both work, but got some issue with the 1st solution.

       table.removeColumn(table.getColumnModel().getColumn(4));
    

    or

       table.getColumnModel().getColumn(4).setMinWidth(0);
       table.getColumnModel().getColumn(4).setMaxWidth(0);
       table.getColumnModel().getColumn(4).setWidth(0);
    

    In my recent case, I preferred the 2nd solution because I added a TableRowSorter.

       TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
       table.setRowSorter(sorter);
    

    When using table.removeColumn(table.getColumnModel().getColumn(4)), it will physically remove the column from the view/table so you cannot use table.getValueAt(row, 4) - it returns ArrayIndexOutOfBounds. The only way to get the value of the removed column is by calling table.getModel().getValueAt(table.getSelectedRow(),4). Since TableRowSorter sorts only what's on the table but not the data in the DefaultTableModel object, the problem is when you get the value after sorting the records - it will retrieve the data from DefaultModelObject, which is not sorted.

    So I used the 2nd solution then used table.getValueAt(table.getSelectedRow(),4);

    The only issue I see with the 2nd approach was mentioned by @camickr: "When you set the width to 0, try tabbing, when you hit the hidden column focus disappears until you tab again. This will confuse users."

    0 讨论(0)
  • 2020-12-03 04:50

    Set the min, max and "normal" width to 0:

    jTable.getColumn("ABC").setMinWidth(0); // Must be set before maxWidth!!
    jTable.getColumn("ABC").setMaxWidth(0);
    jTable.getColumn("ABC").setWidth(0);
    

    Note: Since you can't set a maxWidth < minWidth, you need to change minWidth, first (javadoc). Same is true for width.

    The second approach is to extend TableColumnModel and override all the methods to create the illusion (for the JTable) that your model doesn't have those two columns.

    So when you hide a column, you must return one less when the table asks for the number of columns and when it asks for the column X, you may have to add +1 to the column index (depending on whether it is to the left or right of the hidden column), etc.

    Let your new table model forward all method calls (with the corrected indexes, etc) to the actual column model and use the new table model in the JTable.

    0 讨论(0)
  • 2020-12-03 04:53

    First remove the column from the view

     table.removeColumn(table.getColumnModel().getColumn(4));
    

    Then retrieve the data from the model.

    table.getModel().getValueAt(table.getSelectedRow(),4);
    

    One thing to note is that when retrieving the data, it must be retrieve from model not from the table.

    0 讨论(0)
  • 2020-12-03 04:53

    You could create a subclass of the model, and override TableModel.getColumnCount as follows:

    int getColumnCount() {
        return super.getColumnCount()-2;
    }
    

    The last two columns would then not be displayed in the JTable.

    0 讨论(0)
  • 2020-12-03 04:58

    I have tried them all: they don't help. The BEST way ever is to make a new table model without the column you want to delete. This is how you do it:

    table = (DefaultTableModel) <table name>.getModel();
    DefaultTableModel table1 = new DefaultTableModel();
    Vector v = table.getDataVector();
    Vector v1 = newvector(v,<column index you want to delete>);
    
    Vector newvector(Vector v,int j){
        Vector v1= new Vector();
        try{
            Vector v2;
            Object[] o = v.toArray();
            int i =0;
            while(i<o.length){
                v2 = (Vector) o[i];
                v2.remove(j);
                v1.add(v2);
                i++;
            }
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null,"Error in newvector \n"+e);
        }
        return v1;
    }
    
    Vector getColumnIdentifiers(int i) {
        Vector columnIdentifiers = new Vector();
        int j=0;
        while(j<i){
            columnIdentifiers.add(("c"+(j+1)));
            j++;
        }
        return columnIdentifiers;
    }
    table1.setDataVector(v1,getColumnIdentifiers((<column count>-1)));
    <table name>.setModel(table1);
    
    0 讨论(0)
  • 2020-12-03 05:02

    Remove the TableColumn from the TableColumnModel.

    TableColumnModel tcm = table.getColumnModel();
    tcm.removeColumn( tcm.getColumn(...) );
    

    If you need access to the data then you use table.getModel().getValueAt(...).

    For a more complex solution that allows the user to hide/show columns as they wish check out the Table Column Manager.

    0 讨论(0)
提交回复
热议问题