Hello,
I have a table in my application, where the user could change the column
width using the mouse. These changes are lost when inserting a new row.
I'd like to save this changes, but could find a way to detect them.
If I us TableModelColumnListener, I could detect all changes in the
column width. But I need to detect only thos comming from mouse
interaction and the restore the columns width.
Any recommandations ? Carsten

Signature
Dipl. Ing. Carsten Zerbst | carsten.zerbst@atlantec-es.com
catalysoft.com - 17 Jul 2003 09:24 GMT
Carsten,
I believe the following is happening:
- When the new row is added, a table change event is fired from the table
model
- the event is received by the JTable and a new TableColumnModel is
generated from scratch, based on the table model
- since the TableColumnModel contains the widths of the columns (in the
TableColumn objects) the widths are being lost
An alternative approach to saving and restoring the column widths is to
switch off the automatic generation of the TableColumnModel
by calling setAutoCreateColumnsFromModel(false) on your JTable, or perhaps
subclassing JTable and overriding the
createDefaultColumnsFromModel() method to ensure that widths are preserved
when the new TableColumnModel is generated.
Simon
--
www.catalysoft.com - increasing the rate of your software development
> Hello,
>
[quoted text clipped - 9 lines]
> --
> Dipl. Ing. Carsten Zerbst | carsten.zerbst@atlantec-es.com
Pedro - 17 Jul 2003 09:48 GMT
> Hello,
>
[quoted text clipped - 6 lines]
>
> Any recommandations ? Carsten
Hi Carsten,
I've written an application which remembers both the width of each column and the order of the
columns (these will be restored when the user starts the application a next time).
For watching the order I've used a TableColumnModelListener (on the TableColumnModel).
For watching the column widths I'm using a PropertyChangeListener on each column.
The code implementing this is shown below.
HTH,
Pedro
--------------
<comment>
- jFiles is my JTable
- getPreferences() returns an object that stores the user's preferences and settings
- notRestoringColumnState is normally true, but only when the app starts and the user's settings are
restored it will be set to false (this prevents the settings from being overwritten).
- when the order of the columns changes, I also need to save the widths of the columns (!)
</comment>
<snip>
private void addColumnStateSupport()
{
TableColumnModel tcm = jFiles.getColumnModel();
//: add listener that watches the column order
tcm.addColumnModelListener(new TableColumnModelListener()
{
private int columnCount = jFiles.getColumnCount();
private int[] order = new int[columnCount];
private int[] width = new int[columnCount];
public void columnAdded(TableColumnModelEvent e)
{
// ignored as I will not be adding columns in my table
}
public void columnRemoved(TableColumnModelEvent e)
{
// ignored as I will not be removing columns in my table
}
public void columnMoved(TableColumnModelEvent e)
{
if (notRestoringColumnState)
{
TableColumnModel tcm = (TableColumnModel) e.getSource();
int n = tcm.getColumnCount();
if (n != columnCount)
{
columnCount = n;
order = new int[n];
width = new int[n];
}
// for each column, get its model index and its width
for (int i = 0; i < n; i++)
{
TableColumn tc = tcm.getColumn(i);
order[i] = tc.getModelIndex();
width[i] = tc.getPreferredWidth();
}
// store the order and the widths
getPreferences().setFilesColumnOrder(order);
getPreferences().setFilesColumnWidth(width);
}
}
public void columnMarginChanged(ChangeEvent e)
{
// ignored as I'm not changing column margins
}
public void columnSelectionChanged(ListSelectionEvent e)
{
// ignored as I'm not allowing columns to be selected
}
});
//: listener that watches the width of a column
PropertyChangeListener pcl = new PropertyChangeListener()
{
private int columnCount = jFiles.getColumnCount();
private int[] width = new int[columnCount];
public void propertyChange(PropertyChangeEvent evt)
{
if (notRestoringColumnState
&& evt.getPropertyName().equals("preferredWidth"))
{
TableColumnModel tcm = jFiles.getColumnModel();
int n = tcm.getColumnCount();
if (n != columnCount)
{
columnCount = n;
width = new int[n];
}
// for each column, get its width
for (int i = 0; i < n; i++)
{
width[i] = tcm.getColumn(i).getPreferredWidth();
}
// store the widths
getPreferences().setFilesColumnWidth(width);
}
}
};
//: add the column width lister to each column
for (Enumeration e = tcm.getColumns(); e.hasMoreElements();)
{
TableColumn tc = (TableColumn) e.nextElement();
tc.addPropertyChangeListener(pcl);
}
}
</snip
Alex Q - 29 Aug 2005 21:53 GMT
Pedro, I know it's been a while, but would you mind sharing how you then
proceed to load the saved preferences when restarting the app?
Adin A - 31 Aug 2005 15:45 GMT
This is how to set the saved width later:
YourTable.getColumnModel().getColumn(NumOfColumnFromLeft).setPreferredWidth
(SomeInterValueYouSaved);
>Pedro, I know it's been a while, but would you mind sharing how you then
>proceed to load the saved preferences when restarting the app?