You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by drf <da...@gmail.com> on 2010/09/01 13:15:57 UTC

DropDownChoice - how to select default item

I am having a problem that my DropDownChoice is not selecting the item I
expect, only "Choose one". Here is the code:
...
private List<Long> accountList = new ArrayList<Long>();
private Long selectedAccount;

public AccountsDropDownChoice(String id, final Component component) {
   super(id);
   initializeAccounts();
   selectedAccount = getDefaultAccount();

   final DropDownChoice<Long> dropDown = new
DropDownChoice<Long>("accountsDropDown",
                                                                                   
new PropertyModel(this, "selectedAccount"),
                                                                                   
accountsList) {

   }

The value selectedAccount is being updated correctly when an item in the
dropdown is selected, but the dropdown is not defaulting to the value in
that field to begin with. If anyone can help it is very appreciated.
-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-how-to-select-default-item-tp2402960p2402960.html
Sent from the Wicket - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DropDownChoice - how to select default item

Posted by Sven Meier <sv...@meiers.net>.
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<sv...@meiers.net>  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: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>>      
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>    


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DropDownChoice - how to select default item

Posted by T Ames <ta...@gmail.com>.
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 <sv...@meiers.net> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DropDownChoice - how to select default item

Posted by drf <da...@gmail.com>.
have a breakpoint on that line -
getDefaultAccount returns what I expect


On Wed, Sep 1, 2010 at 5:21 PM, Sven Meier [via Apache Wicket]
<ml...@n4.nabble.com> 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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
>
> ________________________________
> View message @
> http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-how-to-select-default-item-tp2402960p2403250.html
> To unsubscribe from DropDownChoice - how to select default item, click here.
>

-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-how-to-select-default-item-tp2402960p2403304.html
Sent from the Wicket - User mailing list archive at Nabble.com.

Re: DropDownChoice - how to select default item

Posted by Sven Meier <sv...@meiers.net>.
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DropDownChoice - how to select default item

Posted by drf <da...@gmail.com>.
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

-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-how-to-select-default-item-tp2402960p2403224.html
Sent from the Wicket - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DropDownChoice - how to select default item

Posted by Michael O'Cleirigh <mi...@rivulet.ca>.
  Hello,

You should use a DropDownChoice<Account> and use the constructor that 
takes an IChoiceRenderer<Account> which allows you to define:
1. the text that is output for the account
2. the value that is output in the html for the account.  This would be 
the place to pull out the long.

I think the problem with your code is how the selected account is 
fetched.   It looks to me like you are using the value of 
selectedAccount within the constructor.

Since the data backing the value doesn't exist until render time it is 
always null; and causes the drop down to show "Choose One".

You should wrap the getDefaultAccount() method with an IModel<Account> 
implementation so that it can return the model value at render time.

One method for cases like this if you are using Spring is to inject the 
accountsService bean using @SpringBean and then get account list using a 
method like: List<Accounts>getAccountsForUser(...).  You set the 
possibilities of the drop down to be the List<Accounts> and you set the 
current account to be the first item from the list.

Regards,

Mike


> No, it does not appear to be working
> I have an Account object, but for the dropdown display I am just
> extracting a list of Long values. Is that the correct thing to do?
> The accountList v accountsList is just a typing error in the email
>
>
>
> On Wed, Sep 1, 2010 at 2:26 PM, Sven Meier [via Apache Wicket]
> <ml...@n4.nabble.com>  wrote:
>> Should be working fine.
>>
>> Are you sure you're showing the actual code, i.e. accountList vs
>> accountsList ?
>>
>> BTW are you accounts really just represented as Longs?
>>
>> Regards
>>
>> Sven
>>
>> On 09/01/2010 01:15 PM, drf wrote:
>>> I am having a problem that my DropDownChoice is not selecting the item I
>>> expect, only "Choose one". Here is the code:
>>> ...
>>> private List<Long>    accountList = new ArrayList<Long>();
>>> private Long selectedAccount;
>>>
>>> public AccountsDropDownChoice(String id, final Component component) {
>>>      super(id);
>>>      initializeAccounts();
>>>      selectedAccount = getDefaultAccount();
>>>
>>>      final DropDownChoice<Long>    dropDown = new
>>> DropDownChoice<Long>("accountsDropDown",
>>>
>>> new PropertyModel(this, "selectedAccount"),
>>>
>>> accountsList) {
>>>
>>>      }
>>>
>>> The value selectedAccount is being updated correctly when an item in the
>>> dropdown is selected, but the dropdown is not defaulting to the value in
>>> that field to begin with. If anyone can help it is very appreciated.
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>>
>> ________________________________
>> View message @
>> http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-how-to-select-default-item-tp2402960p2402968.html
>> To unsubscribe from DropDownChoice - how to select default item, click here.
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DropDownChoice - how to select default item

Posted by drf <da...@gmail.com>.
Thanks

No, it does not appear to be working
I have an Account object, but for the dropdown display I am just
extracting a list of Long values. Is that the correct thing to do?
The accountList v accountsList is just a typing error in the email



On Wed, Sep 1, 2010 at 2:26 PM, Sven Meier [via Apache Wicket]
<ml...@n4.nabble.com> wrote:
> Should be working fine.
>
> Are you sure you're showing the actual code, i.e. accountList vs
> accountsList ?
>
> BTW are you accounts really just represented as Longs?
>
> Regards
>
> Sven
>
> On 09/01/2010 01:15 PM, drf wrote:
>> I am having a problem that my DropDownChoice is not selecting the item I
>> expect, only "Choose one". Here is the code:
>> ...
>> private List<Long>  accountList = new ArrayList<Long>();
>> private Long selectedAccount;
>>
>> public AccountsDropDownChoice(String id, final Component component) {
>>     super(id);
>>     initializeAccounts();
>>     selectedAccount = getDefaultAccount();
>>
>>     final DropDownChoice<Long>  dropDown = new
>> DropDownChoice<Long>("accountsDropDown",
>>
>> new PropertyModel(this, "selectedAccount"),
>>
>> accountsList) {
>>
>>     }
>>
>> The value selectedAccount is being updated correctly when an item in the
>> dropdown is selected, but the dropdown is not defaulting to the value in
>> that field to begin with. If anyone can help it is very appreciated.
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
>
> ________________________________
> View message @
> http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-how-to-select-default-item-tp2402960p2402968.html
> To unsubscribe from DropDownChoice - how to select default item, click here.
>

-- 
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DropDownChoice-how-to-select-default-item-tp2402960p2402993.html
Sent from the Wicket - User mailing list archive at Nabble.com.

Re: DropDownChoice - how to select default item

Posted by Sven Meier <sv...@meiers.net>.
Should be working fine.

Are you sure you're showing the actual code, i.e. accountList vs 
accountsList ?

BTW are you accounts really just represented as Longs?

Regards

Sven

On 09/01/2010 01:15 PM, drf wrote:
> I am having a problem that my DropDownChoice is not selecting the item I
> expect, only "Choose one". Here is the code:
> ...
> private List<Long>  accountList = new ArrayList<Long>();
> private Long selectedAccount;
>
> public AccountsDropDownChoice(String id, final Component component) {
>     super(id);
>     initializeAccounts();
>     selectedAccount = getDefaultAccount();
>
>     final DropDownChoice<Long>  dropDown = new
> DropDownChoice<Long>("accountsDropDown",
>
> new PropertyModel(this, "selectedAccount"),
>
> accountsList) {
>
>     }
>
> The value selectedAccount is being updated correctly when an item in the
> dropdown is selected, but the dropdown is not defaulting to the value in
> that field to begin with. If anyone can help it is very appreciated.
>    


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DropDownChoice - how to select default item

Posted by andrea del bene <an...@libero.it>.
Hi drf!
Have you checked the generated HTML? Is there any  'selected' attribute 
in your <option> tags?
> I am having a problem that my DropDownChoice is not selecting the item I
> expect, only "Choose one". Here is the code:
> ...
> private List<Long>  accountList = new ArrayList<Long>();
> private Long selectedAccount;
>
> public AccountsDropDownChoice(String id, final Component component) {
>     super(id);
>     initializeAccounts();
>     selectedAccount = getDefaultAccount();
>
>     final DropDownChoice<Long>  dropDown = new
> DropDownChoice<Long>("accountsDropDown",
>
> new PropertyModel(this, "selectedAccount"),
>
> accountsList) {
>
>     }
>
> The value selectedAccount is being updated correctly when an item in the
> dropdown is selected, but the dropdown is not defaulting to the value in
> that field to begin with. If anyone can help it is very appreciated.
>    


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org