Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2005

Tip: Looking for answers? Try searching our database.

How to scroll to element of list?

Thread view: 
Johannes Beekhuizen - 01 Jan 2002 05:00 GMT
Hallo,

I'm playing around with a JList and would like the scroll window to move to a
specific index at start-up. I thought that ensureIndexIsVisible might do the
trick, but it seems not to. It happens for example when I show 5 elements, the
number of elements is 11 and the selected index is the 8th element.
Any help will make me grateful.

Groetjes,

          Hans.

=== import ===
   protected JPanel createChoicePanel() {
       // cut away the filling of the list ...
       // Create the list and put it in a scroll pane.
       choiceList = new JList(listModel);
       choiceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
       choiceList.setSelectedIndex(selectedIndex);
       choiceList.ensureIndexIsVisible(selectedIndex);
       choiceList.setVisibleRowCount(numshow);
       choiceList.addListSelectionListener(this);
       JScrollPane listScrollPane = new JScrollPane(choiceList);
       // Lay out everything.
       p.add(listScrollPane, BorderLayout.LINE_START);
       p.setAlignmentX(Component.LEFT_ALIGNMENT);
       return p;
   }
=== tropmi ===
Vova Reznik - 08 Sep 2005 18:21 GMT
>         choiceList.ensureIndexIsVisible(selectedIndex);
>         choiceList.setVisibleRowCount(numshow);
>         choiceList.addListSelectionListener(this);
>         JScrollPane listScrollPane = new JScrollPane(choiceList);
>         // Lay out everything.
>         p.add(listScrollPane, BorderLayout.LINE_START);

ensureIndexIsVisible should be used after adding to Container

JScrollPane listScrollPane = new JScrollPane(choiceList);
choiceList.ensureIndexIsVisible(selectedIndex);
Johannes Beekhuizen - 01 Jan 2002 05:00 GMT
Hallo Vova,

Op 08 Sep 05 schreef Vova Reznik aan All:

>>         choiceList.ensureIndexIsVisible(selectedIndex);
>>         choiceList.setVisibleRowCount(numshow);
>>         choiceList.addListSelectionListener(this);
>>         JScrollPane listScrollPane = new JScrollPane(choiceList);
>>         // Lay out everything.

VR> ensureIndexIsVisible should be used after adding to Container

Tss... So simple! Thank you very much.

Groetjes,

          Hans.
Roedy Green - 09 Sep 2005 04:30 GMT
> VR> ensureIndexIsVisible should be used after adding to Container

Is it that or does it also require addNotify to be done?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Michael Dunn - 08 Sep 2005 18:32 GMT
> Hallo,
>
[quoted text clipped - 8 lines]
>
> Groetjes,
<code snipped>

ensureIndexIsVisible() works OK in this

import javax.swing.*;
import java.awt.*;
class Testing extends JFrame
{
 public Testing()
 {
   setLocation(200,100);
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   DefaultListModel lm = new DefaultListModel();
   JList list = new JList(lm);
   JScrollPane sp = new JScrollPane(list);
   sp.setPreferredSize(new Dimension(100,150));
   for(int x = 0; x < 50; x++) lm.addElement(""+x);
   list.ensureIndexIsVisible(35);
   getContentPane().add(sp);
   pack();
 }
 public static void main(String[] args){new Testing().setVisible(true);}
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.