Link to home
Start Free TrialLog in
Avatar of HStrix
HStrix

asked on

Java: remove columns in a JTable

Hello experts,
in my java application I use the following routine.
The routine works fine if no of columns = zero.
---
    public void iniTable(String tableName, int [] mylength, String [] columnNames, String tableId, String mi_id,Vector data)
    {
      if(mylength==null||mylength.length==0)
      {
        mylength=new int[columnNames.length];
        for(int j=0;j<columnNames.length;j++)
          mylength[j]=150;
      }
      TableModel model = new TableModel(tableName,columnNames,tableId,data);
      model.setMitarbeiterID(mi_id);
      model.getData();
      setModel(model);
      _sorter = new SorterModel(model);
      JTable1.setModel(_sorter);

      //             here I'm going to check if there already columns exist
        int iColumnCount = JTable1.getColumnCount();
        if (iColumnCount == 0)
        {
        } else {
          // delete all existing columns
          JTable1.removeColumnSelectionInterval(0,iColumnCount-1);
        }
      //

      if (JTable1.getColumnCount() == 0)
      {
          for(int i = 0; i<mylength.length; i++)
          {
              TableColumn column = new TableColumn(i,mylength[i]);
              JTable1.addColumn(column);
          }
          _sorter.addMouseListenerToHeaderInTable(JTable1);
      }
    }
---
My target is, to check for the existing number of columns and remove all.
So, I can add all columns newly.
But it doesn't work.

  If anyone can help me
  please supply your ideas.

    Thank you for any help.


   HStrix
Avatar of zzynx
zzynx
Flag of Belgium image

>>  JTable1.removeColumnSelectionInterval(0,iColumnCount-1);
removeColumnSelectionInterval deselects the columns from index0 to index1, inclusive
So, that's not a remove of columns

>> My target is, to check for the existing number of columns and remove all.
>> So, I can add all columns newly.

Well, then just newly create your JTable1 and the above is assured
Avatar of HStrix
HStrix

ASKER

The definition of JTable1 is defined in the class as
   javax.swing.JTable JTable1 = new javax.swing.JTable();

It is during intialization modifed
   JTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
   JTable1.setAutoCreateColumnsFromModel(false);
   getViewport().add(JTable1);
   JTable1.setBounds(0,0,20,40);  

So I don't know how to newly create JTable1 and ensure that it is used as required.

My first idea was to remove the columns, but how?

>> JTable1.setAutoCreateColumnsFromModel(false);
Why is this?
Remove it and each call to

     JTable1.setModel(_sorter);

will guarantee that the JTable has the right columns (corresponding with the model)



You don't have to bother about removing and adding columns. Let java do that for you.
Avatar of HStrix

ASKER

I'm sorry, this does not work because the entire app is based on this behaviour.
It doesn't work anymore in this case.
So I only see the way to remove the columns (I don't know how).
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HStrix

ASKER

Thanks zzynx,
this is working now.

   HStrix
Thanks :)