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||mylengt h.length== 0)
{
mylength=new int[columnNames.length];
for(int j=0;j<columnNames.length;j ++)
mylength[j]=150;
}
TableModel model = new TableModel(tableName,colum nNames,tab leId,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.removeColumnSelect ionInterva l(0,iColum nCount-1);
}
//
if (JTable1.getColumnCount() == 0)
{
for(int i = 0; i<mylength.length; i++)
{
TableColumn column = new TableColumn(i,mylength[i]) ;
JTable1.addColumn(column);
}
_sorter.addMouseListenerTo HeaderInTa ble(JTable 1);
}
}
---
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
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||mylengt
{
mylength=new int[columnNames.length];
for(int j=0;j<columnNames.length;j
mylength[j]=150;
}
TableModel model = new TableModel(tableName,colum
model.setMitarbeiterID(mi_
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.removeColumnSelect
}
//
if (JTable1.getColumnCount() == 0)
{
for(int i = 0; i<mylength.length; i++)
{
TableColumn column = new TableColumn(i,mylength[i])
JTable1.addColumn(column);
}
_sorter.addMouseListenerTo
}
}
---
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
>> 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
>> So, I can add all columns newly.
Well, then just newly create your JTable1 and the above is assured
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.swin g.JTable.A UTO_RESIZE _OFF);
JTable1.setAutoCreateColum nsFromMode l(false);
getViewport().add(JTable1) ;
JTable1.setBounds(0,0,20,4 0);
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?
javax.swing.JTable JTable1 = new javax.swing.JTable();
It is during intialization modifed
JTable1.setAutoResizeMode(
JTable1.setAutoCreateColum
getViewport().add(JTable1)
JTable1.setBounds(0,0,20,4
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.setAutoCreateColum nsFromMode l(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)
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.
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).
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks zzynx,
this is working now.
HStrix
this is working now.
HStrix
Thanks :)
removeColumnSelectionInter
So, that's not a remove of columns