setVisibleRowCount of JList doesn't work!
Hi all!
I am working in jdk1.3.1. The function setVisibleRowCount of class JList doesn't work(see example). Please help me!
import javax.swing.*;
import java.awt.*;
public class TempList extends JList
{
public TempList()
{
setVisibleRowCount(10);
JScrollPane sp = new JScrollPane(this);
}
public static void main(String args[])
{
JFrame frame= new JFrame("TestList");
TempList tl = new TempList();
frame.getContentPane().add(tl);
frame.pack();
frame.show();
}
}
Regards,
Nemaria.
[621 byte] By [
nemaria] at [2007-9-26 2:17:45]

I experienced this problem going back a while...
I was adding a JList as the main component to a panel. finally solved the visibleRowCount stuff when I used a GridbagLayout object to position my components and to set their relative sizes. This way you can for example, specify your JList to have a gridheight of e.g. 10, and set the extra vertical space allowance to be zero, and the space filling directions to just be horizontal, then the list should *theoretically* remain at a size of 10 rows.
Can't say any more, I've forgotten the specifics.
Hope this helps.
Richard.
After setting the visible row count try to get the preferred size of the JList and set the exact size of the component.
list.setVisibleRowCount(10);
Dimension size = list.getPreferredScrollableViewportSize();
size.width = Short.MAX_VALUE;
listScroller.setMaximumSize(size);
// setMInimumSize if you want...
Kurta
Thank you!
The problem was that I added Jlist to Jframe, and I'd have
to add the JScrollPane instead:
import javax.swing.*;
import java.awt.*;
public class TempList extends JList
{
public TempList()
{
setVisibleRowCount(5);
}
public static void main(String args[])
{
JFrame frame= new JFrame("TestList");
TempList tl = new TempList();
JScrollPane sp = new JScrollPane(tl);
frame.getContentPane().add(sp);
frame.pack();
frame.show();
}
}
Regards,
Nemaria