[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

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
0
HStrix
Asked:
HStrix
  • 6
  • 3
1 Solution
 
zzynxSoftware engineerCommented:
>>  JTable1.removeColumnSelectionInterval(0,iColumnCount-1);
removeColumnSelectionInterval deselects the columns from index0 to index1, inclusive
So, that's not a remove of columns

0
 
zzynxSoftware engineerCommented:
>> 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
0
 
HStrixAuthor Commented:
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?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zzynxSoftware engineerCommented:
>> 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)



0
 
zzynxSoftware engineerCommented:
You don't have to bother about removing and adding columns. Let java do that for you.
0
 
HStrixAuthor Commented:
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).
0
 
zzynxSoftware engineerCommented:
>> I don't know how

TableColumnModel model = yourTable.getColumnModel();
for (int i=model.getColumnCount()-1; i>=0; i--) {
   TableColumn col = model.getColumn(i);
   model.removeColumn(col);
}
0
 
HStrixAuthor Commented:
Thanks zzynx,
this is working now.

   HStrix
0
 
zzynxSoftware engineerCommented:
Thanks :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now