Of course you can use "this" inside the constructor.

I've thrown together the following example and it works (of course):

public class AccountPage extends WebPage
{

    private List<Long> accountList = new ArrayList<Long>();

    private Long selectedAccount;

    public AccountPage()
    {
        initializeAccounts();

        selectedAccount = getDefaultAccount();

final DropDownChoice<Long> dropDown = new DropDownChoice<Long>("accountsDropDown", new PropertyModel<Long>(this, "selectedAccount"), accountList);
        add(dropDown);
    }

    private Long getDefaultAccount()
    {
        return 3l;
    }

    private void initializeAccounts()
    {
        accountList = Arrays.asList(1L, 2L, 3L, 4L, 5L);
    }
}

On 09/01/2010 04:40 PM, T Ames wrote:
Mike may have been referring to the usage of "this" when he was
talking about the constructor.  I create drop downs in constructors
with no issue, I have not however used a "this" with a property model.

Wild guess:  possibly java is in the process of constructing the
object, but DropDownChoice cannot access the "this" yet?

On occasion I am using drop downs without choice renderer and setting
the initial value OK.


On Wed, Sep 1, 2010 at 10:20 AM, Sven Meier<[email protected]>  wrote:
Hi,

your dropdown accesses the selected account via a model, so it should work
regardless when you initialize the selectedAccount variable.

Put a breakpoint on that line and check the return value of
getDefaultAccount().

BTW using Model<Account>  and IChoiceRenderer gives you some advantages
(always up-to-date account list, don't keep selected account in session,
...).

Sven

On 09/01/2010 04:02 PM, drf wrote:
Mike

Thanks - not sure if the problem is with the fact that the code is in the
constructor:
1) the In Action book seems to setup dropdowns ect in the constructor -
where else would I define the drop down?
2) the debugger and syout both show that selectedAccount has the correct
value prior to constructing the dropdown.

Re IChoseRenderer, the dropdown is displaying the correct values and also
updating selectedAccount correctly. So what does IChoseRenderer apart from
saving the effort to extract the raw account numbers - are you brining in
IChoseRenderer as good advice or are you saying that the fact I am not
using
it is part of the issue?

I need to understand Models better, but I am wondering - do I have to
write
another class to wrap Account with a Model implementation just to do this
simple thing? Also, if the selectedAccount is correctly set before calling
the constructor, will this still help?

David



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to