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);}
}