This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to select first row of list by default ?  RSS feed

 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
I want first value of the list to be selected when page loads ..
I am using following for this ...

jList1.setSelectedIndex(0);


but , its not working ...
is there any other way to achieve this ??


Thanks,
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That method only works after the frame has been packed ore made visible.

If you at trying to set the index before either of the above then you can use:

 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:That method only works after the frame has been packed ore made visible.

Not true; the data needs to be added, but the frame doesn't need to be visible. This little example proves that:
As long as the list's model has at least one element it will work. If it's empty, then yes, it will fail (or at least do nothing).
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not true; the data needs to be added, but the frame doesn't need to be visible


Actually thats the way I also thought it should work, but I did a quick test and it didn't work either.

Now I realize, I had some extra code in my "quick test" which caused it not to work. Thanks for correcting my statement.
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it worked... just wrote it after data has been added ..


Thanks,
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!