JDK-4109697 : JList.getLastVisibleIndex() can return -1 when in JScrollPane
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: solaris_2.4
  • CPU: generic
  • Submitted: 1998-02-05
  • Updated: 1998-05-28
  • Resolved: 1998-05-28
Related Reports
Relates :  
Description
When a JList instance is in a JScrollPane and the JScrollPane viewport is
taller than the contents of the list, getLastVisibleIndex will return an
incorrect value of -1.  I believe the reason for this is that the logic
for obtaining the last visible index tries to determine the list row that
maps to the location at the bottom of the visible rectangle.  Since this
is outside the bounds of the list when the JScrollPane viewport is larger
than the list, the UI returns a -1 because it assumes an invalid location 
has been sent to it.

I have attached a small application that places a JList in a JScrollPane.
At the top of the window is a "Debug" button; when you press it, it 
println's the results of calls to getFirstVisibleIndex, getLastVisibleIndex,
and getVisibleRowCount.  If you resize the application so the JScrollPane
viewport is smaller than the list, the values are valid.  When you resize
the application so the JScrollPane viewport is larger than the list, 
getLastVisibleIndex is -1.

Comments
EVALUATION This probably was wrong when the bug was first filed. The test program (slightly modified below) works fine now. /** * 4109697 - JList.getLastVisibleIndex() can return -1 when in JScrollPane * * setenv JAVA_HOME /usr/local/java/jdk1.1.6/solaris * setenv CLASSPATH .:/home/hmuller/ws/jdk12/build/solaris/1.1_classes:${JAVA_HOME}/lib/classes.zip * ${JAVA_HOME}/bin/javac LastVisibleIndexBug.java * ${JAVA_HOME}/bin/java -classpath .:${CLASSPATH} LastVisibleIndexBug * * setenv JAVA_HOME /usr/local/java/jdk1.2/solaris * unsetenv CLASSPATH * ${JAVA_HOME}/bin/javac LastVisibleIndexBug.java * ${JAVA_HOME}/bin/java -cp . LastVisibleIndexBug */ import java.awt.*; import java.awt.event.*; import com.sun.java.swing.*; import com.sun.java.swing.event.*; public class LastVisibleIndexBug { public static int WIDTH = 100; public static int HEIGHT = 200; public JFrame frame; JList months; JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar(); JMenuItem mi; // File Menu JMenu file = (JMenu) menuBar.add(new JMenu("File")); mi = (JMenuItem) file.add(new JMenuItem("Exit")); mi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); return menuBar; } void printListInfo(JList l) { System.out.println("Debug Information For " + l.getAccessibleContext().getAccessibleName() + ":"); System.out.println(" getFirstVisibleIndex: " + l.getFirstVisibleIndex()); System.out.println(" getLastVisibleIndex: " + l.getLastVisibleIndex()); System.out.println(" getVisibleRowCount: " + l.getVisibleRowCount()); } JPanel createListPanel() { JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); JButton fweep; fweep = new JButton("Debug"); fweep.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { printListInfo(months); System.out.println(""); } }); panel.add(fweep); DefaultListModel model = new DefaultListModel(); model.addElement("January"); model.addElement("February"); model.addElement("March"); model.addElement("April"); model.addElement("May"); model.addElement("June"); model.addElement("July"); model.addElement("August"); model.addElement("September"); model.addElement("October"); model.addElement("November"); model.addElement("December"); months = new JList(model); months.getAccessibleContext().setAccessibleName("Months"); months.getAccessibleContext().setAccessibleDescription("Choose a month of the year"); panel.add(new JScrollPane(months)); return panel; } public LastVisibleIndexBug() { WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; frame = new JFrame("LastVisibleIndexBug"); frame.getContentPane().setLayout(new BorderLayout()); frame.addWindowListener(l); frame.setJMenuBar(createMenuBar()); frame.getContentPane().add(createListPanel()); frame.setSize(WIDTH, HEIGHT); frame.show(); } public static void main(String s[]) { new LastVisibleIndexBug(); } }
11-06-2004