selection in JList

Hi all,

I had some problem in the selection of Jlist while navigating through JList using charecters.

Problem :-

JList is added to a scroll pane.

There are some 100 records in the JList, Now after selecting the first record i keep on pressing some charecter say some 'k'.Now the focus has to move to the record which starts with 'k' by default this can be achived but my scroll bar also should move to the location where the selection is made.

Can any one please help me to sort out problem.

Thanks in Advance,

vamsy.

[577 byte] By [vamsyva] at [2007-10-3 3:21:30]
# 1
post your code displaying the problem(10 hard-coded items in the list is enough)
Michael_Dunna at 2007-7-14 21:13:47 > top of Java-index,Desktop,Core GUI APIs...
# 2

HI Michael,

I just created a JList and added to the scroll pane as follows-

kParameterIDsp = new JScrollPane();

kParameterID = new JList();

kParameterIDsp.getViewport().add(kParameterID, null);

The scrollpane size is adjusted such that only 5 rows are visble in the Jlist.

Then i had populated the data into JList as follows-

Vector add = new Vector();

kParameterID.setListData(add);

As you asked 10 elements are as follows which were populated into the Vector add in above code-

Apple

Basket

cat

dog

elephant

Hen

Holland

Hawk

whale

zebra

After populating the above elements into the JList i could view till 5 element elephant above as i had a scroll bar i can drag and view the others.

But the my problem is when i press letter H the focus has to move to Hen so that the scrollBar must also movedown to 6 the element.

vamsyva at 2007-7-14 21:13:47 > top of Java-index,Desktop,Core GUI APIs...
# 3

works OK in this

import javax.swing.*;

import java.awt.*;

import java.util.*;

class Testing extends JFrame

{

JList list = new JList();

JScrollPane sp = new JScrollPane(list);

public Testing()

{

setLocation(400,300);

setDefaultCloseOperation(EXIT_ON_CLOSE);

list.setVisibleRowCount(5);

list.setListData(new Vector<String>(Arrays.asList(new String[]{

"Apple","Basket","cat","dog","elephant","Hen","Holland","Hawk","whale","zebra"})));

getContentPane().add(sp);

pack();

}

public static void main(String[] args){new Testing().setVisible(true);}

}

Michael_Dunna at 2007-7-14 21:13:47 > top of Java-index,Desktop,Core GUI APIs...
# 4
Maybe JList#ensureIndexIsVisible ?
JayDSa at 2007-7-14 21:13:47 > top of Java-index,Desktop,Core GUI APIs...
# 5
Hi Michael,Thanks for your reply.I am using JDK 1.4 i had tested your sample code but still the scroll bar is not moving with the slection.Is it solved in JDK 1.5?Thanks vamsy.
vamsyva at 2007-7-14 21:13:47 > top of Java-index,Desktop,Core GUI APIs...
# 6
Hi Jay,Thanks it worked after overRiding the below method of JList- public void setSelectedIndex( int index ){super.setSelectedIndex( index );ensureIndexIsVisible( index );}
vamsyva at 2007-7-14 21:13:47 > top of Java-index,Desktop,Core GUI APIs...