Skip to main content

JXTable: Restore Column Settings (view) after TableModel Change

7 replies [Last post]
cronid
Offline
Joined: 2010-08-12

Hey Guys,

i tried to restore these setting: column visibility, column width, sort state of columns and column viewIndex.

Visibility, sort state and width are not a problem, but the view index is hard to solve.

At this time i got this:
// FilterObject store the related settings
// jt is the JXTable
private void saveSettings() {
FilterObject fo = getFilterObject(jt.getModel());
List columns = jt.getColumns(true);
if (fo == null) {
fo = new FilterObject(
null,
null,
jt.getRowSorter().getSortKeys(),
jt.getColumnModel(),
columns,
getIdentifiers(columns)
);
setFilterObject(jt.getModel(), fo);
} else {
fo.setSortKeys(jt.getRowSorter().getSortKeys());
fo.setColumnModel(jt.getColumnModel());
fo.setColumns(columns);
fo.setColumnIdentifiers(getIdentifiers(columns));
}
}

public Object[] getIdentifiers(List columns) {
Object[] viewColumnObjects = new Object[columns.size()];
int viewColumnIndex = 0;
boolean visibleState = true;
for (int i = 0; i < columns.size(); i++) {
if (columns.get(i) instanceof TableColumnExt) {
TableColumnExt tcExt = (TableColumnExt) columns.get(i);
visibleState = tcExt.isVisible();
// unsuccessful hack, idea was to save the visibility state, then turn it to true,
// so that is visible again and convertColumnIndexToView() doesnt return -1 and
// to re-set the old visibility state, but it returns -1 :-/
if (!visibleState) {
tcExt.setVisible(true);
}
viewColumnIndex = jt.convertColumnIndexToView(tcExt.getModelIndex());
if (viewColumnIndex != -1 && viewColumnObjects[viewColumnIndex] == null) {
viewColumnObjects[viewColumnIndex] = tcExt.getIdentifier();
}
tcExt.setVisible(visibleState);
}
}
return viewColumnObjects;
}

My idea was to get the identifiers in order of viewColumns (viewIndex via "convertColumnIndexToView()" method from jtable) and use them to restore the order by using "jt.setColumnSequence()". That work, if all viewColumns are visible, but if there is one hidden, then "convertColumnIndexToView()" returns -1 (not the problem) and if i restore this column, then set the visibility to true, the column is set to the last position. :-/

How to solve that?

BTW: sry for my terrible english. :-(

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kleopatra
Offline
Joined: 2003-06-11

would you please format your code - maybe I'll have a look then

CU
Jeanette

you can do so by taggin it with [ code ] .... [ /code ] - without the spaces. Please complain to the site admin that this isn't documented anywhere ;-)

cronid
Offline
Joined: 2010-08-12

Okay, i have to invoke "jt.revalidate();" after setting the visibility, then it works.

But now, there is another bug. :-/

If i restore these columns (with hidden ones), then the order of visibility-control-menu-items in the ColumnControl is not the same as in columnModel. Hmm ...

To Karl:
I write an application for adress-related work. Something like a CRM or so ... and the data is presented by a JXTable. There are different views, lets say like a view for all persons, all firms or all adress you got. These views are presented by an list. Because i'm very lazy, i use the same JXTable for all views and change the table model if the user want to see another view. That is why i need to restore some view-related settings.

Message was edited by: cronid

Message was edited by: cronid

kschaefe
Offline
Joined: 2006-06-08

Are you resetting the TableModel or changing the data in the model when you switch address views?

Karl

cronid
Offline
Joined: 2010-08-12

The "change method" looks like this:
public void changeModelTo(String text, boolean isTopLevelNode) {
// first save old settings
saveSettings();
if (isTopLevelNode) {
if (text.contains(LocalizationStrings.getString("database.firm"))) {
setTableModel(AppModels.getModel("firm"));
} else if (text.contains(LocalizationStrings.getString("database.person"))) {
setTableModel(AppModels.getModel("person"));
} else if (text.contains(LocalizationStrings.getString("database.project"))) {
setTableModel(AppModels.getModel("project"));
} else if (text.contains(LocalizationStrings.getString("database.note_rel"))) {
setTableModel(AppModels.getModel("note_rel"));
}
} else {
//TODO das verlangte model ist ein filter model
}
// restore the right settings
loadSettings();
}

Well, it isnt "resetting the table model", just replace it with another one. The "old table model" is still needed. Generating a new table model each time the user is changing the view sounds to me non-effective. With "view" i mean the data that the user want to see.

kschaefe
Offline
Joined: 2006-06-08

You should read the documentation for ColumnFactory. That should allow you to rebuild the column model correctly.

Karl

kschaefe
Offline
Joined: 2006-06-08

Why are you "restoring" these settings? Are you persisting between sessions (app shutdowns)? Are you trying to recreate a table in the same session? What's the core problem?

Karl

cronid
Offline
Joined: 2010-08-12

// unsuccessful hack, idea was to save the visibility state, then turn it to true,
// so that is visible again and convertColumnIndexToView() doesnt return -1 and
// to re-set the old visibility state, but it returns -1 :-/
if (!visibleState) {
tcExt.setVisible(true);
}
viewColumnIndex = jt.convertColumnIndexToView(tcExt.getModelIndex());
if (viewColumnIndex != -1 && viewColumnObjects[viewColumnIndex] == null) {
viewColumnObjects[viewColumnIndex] = tcExt.getIdentifier();
}
tcExt.setVisible(visibleState);
}
}
return viewColumnObjects;
}

My idea was to get the identifiers in order of viewColumns (viewIndex via "convertColumnIndexToView()" method from jtable) and use them to restore the order by using "jt.setColumnSequence()". That works, if all viewColumns are visible, but if there is one hidden, then "convertColumnIndexToView()" returns -1 (not the problem) and if i restore this column, then set the visibility to true, the column is set to the last position. :-/

How to solve that?

BTW: sry for my terrible english. :-(