Problem with JList, DefaultListModel and JScrollPane

T

Tobi Krausl

Hi!

I've the following problem with JList, DefaultListModel and
JScrollPane:
How can I get a JList (JScrollPane) which automatically scrolls down
and shows the last entry added to the list?
In my program there are several classes manipulating a
DefaultListModel connected to a certain JList. To handle that, the
constructor of each class gets a reference to that DefaultListModel.
Ok, up to now it all works pretty fine. But if the JList gets bigger
and Scrollbars are added, the user has to scroll down "manually" to
see the bottom of the list. How can I handle the JList/JScrollPane to
select and show always(!) the bottom of the list everytime myListModel
changes??

I tried the following:

..
..
myList.addListSelectionListener(
new ListSelectionListener() {

public void valueChanged(ListSelectionEvent e) {
int i=0;

i=myListModel.getSize();

myList.setSelectedIndex(i-1);
myList.ensureIndexIsVisible(i-1);

}
}
);
..
..

This works, but only when the users clicks on the list.
Could you please help me?

Tnx so much,
Tobi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top