PDA

View Full Version : How to refresh a List backed by DynamicListModel



manifoldronin
Nov 13th, 2004, 05:09 PM
I have this account list value model:


public abstract class AccountListValueModel extends AbstractValueModel {

private List accounts_;;

public void refresh() {
accounts_ = getAccountDAO().findAllAccounts();
fireValueChanged();
}

public Object getValue() {
return accounts_;
}

public void setValue(Object value) {
accounts_ = (List)value;
}

public abstract IAccountDAO getAccountDAO();
}


(The abstract method is actually a look-up) An instance of this class is passed to a DynamicListModel which is then used to back a JList. Somewhere else I have:


public static class NewAccountCmdExe implements ActionCommandExecutor {

private AccountListValueModel listModel_;

public NewAccountCmdExe(AccountListValueModel listModel) {
listModel_ = listModel;
}

/***/
public void execute() {
final Account newOne = new Account();
final SwingFormModel model = SwingFormModel.createFormModel(newOne);
FormBackedDialogPage dlgPg = new FormBackedDialogPage(
new AccountForm(model));
TitledPageApplicationDialog dlg = new TitledPageApplicationDialog(
dlgPg) {

protected boolean onFinish() {
model.commit();
listModel_.getAccountDAO().saveAccount(newOne);
listModel_.refresh();
return true;
}
};
dlg.setCloseAction(CloseAction.DISPOSE);

dlg.showDialog();
}
}


I thought that call to refresh() above would trigger a chain of reaction and end up with the actual JList being refreshed, but it actually doesn't, while the new account is indeed saved in the DB. I can even see DynamicListModel logging a line about the value changed event being fired.

Maybe I'm using it in the wrong way?
Thanks.

Keith Donald
Nov 13th, 2004, 07:26 PM
Can you post your AccountForm code? I suspect the problem is there.

I'd favor using the factory methods of SwingFormModel to create a bound list, if you're not already doing that.

Keith

manifoldronin
Nov 13th, 2004, 10:17 PM
Here's AccountForm. But it's for adding a new account. The JList of all accounts isn't on any form. It's directly inserted into a JTabbedPane, which is in the default view of the application.



public class AccountForm extends AbstractForm {

private static final String FORM_PAGE_ID = "accountProp";

public AccountForm(SwingFormModel formModel) {
super(formModel, FORM_PAGE_ID);
}

protected JComponent createFormControl() {
FormLayout layout = new FormLayout("left:pref, 5dlu, pref:grow");
BeanFormBuilder formBuilder = new JGoodiesBeanFormBuilder(
getFormModel(), layout);
formBuilder.add("name");
formBuilder.add("description");
return formBuilder.getForm();
}
}


Thanks.

manifoldronin
Nov 15th, 2004, 09:04 PM
Keith, I looked through petclinic again but didn't see list models being used anywhere. It's not mentioned on the Wiki either. Any more thoughts on this? Thanks.

afida
Nov 16th, 2004, 01:09 AM
One thing you can do is create AccountsForm which has your JList of accounts. And your AccountForm can look something like this,


public class AccountsForm extends AbstractForm {

private static final String FORM_PAGE_ID = "accountList";

public AccountForm(SwingFormModel formModel) {
super(formModel, FORM_PAGE_ID);
}

protected JComponent createFormControl() {
RefreshableValueHolder refreshableValueHolder = new RefreshableValueHolder(new Block() {
public Object call(Object object) {
return acounts;
}
}, true);

accountsList = getFormModel().createBoundList("account", refreshableValueHolder, "name");
TableLayoutBuilder builder = new TableLayoutBuilder();
builder.cell(accountsList, "colGrId=ctrColumn");
return formBuilder.getForm();
}
}

You can add a ValueChangeListenert to form model to refresh every time the model changes.

Hope that helps.

Amad

Keith Donald
Nov 16th, 2004, 07:29 AM
Mr,

I've noted this as an issue for investigation: for now I'd try Amad's suggestion. From what I can tell your code looks correct.

Best,
Keith