Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4379540

BasicListUI.locationToIndex method returns wrong value

XMLWordPrintable

    • beta
    • sparc
    • solaris_2.6



      Name: nkR10003 Date: 10/16/2000


      JavaDoc comments for method BasicListUI.locationToIndex state:
      "Convert a point in JList coordinates to the index of the cell
      at that location. Returns -1 if there's no cell the specified
      location". Actually locationToIndex method returns positive value
      for any point with valid Y coordinate. For example (-1, 0) point
      locationToIndex method converts to 0 index instead of -1.
      The same problem appears with JList.locationToIndex method because
      it forwards the call to UI delegate locationToIndex method.

      Example below demonstrates this problem:
      ------------------example--------------------
      import javax.swing.*;
      import java.lang.*;
      import java.lang.reflect.*;
      import java.awt.*;

      public class test {
          public static void main(String[] args) {
              JFrame frame = new JFrame("Test");
              String[] data = {"one", "two", "three"};
              final JList list = new JList(data);
              frame.getContentPane().add(list);
              frame.setSize(list.getPreferredScrollableViewportSize());
              frame.setVisible(true);
              try {
                  SwingUtilities.invokeAndWait(new Runnable() {
                      public synchronized void run() {
                          System.out.println("x: -1");
                          System.out.println("y: 0");
                          System.out.println("index: "
                                  + list.locationToIndex(new Point(-1,0)));
                          System.out.println("UI: " + list.getUI());
                          System.out.println("UI.index: "
                                  + list.getUI().locationToIndex(list, new Point(-1,0)));
                      }
                  });
              } catch (InterruptedException ie) {
                  ie.printStackTrace();
              } catch (InvocationTargetException ite) {
                  ite.printStackTrace();
              } finally {
                  try {
                      Thread.sleep(10000);
                  } catch (InterruptedException ie) {}
                  frame.dispose();
                  System.exit(0);
              }
          }
      }
      ----------------output:----------------------
      x: -1
      y: 0
      index: 0
      UI: javax.swing.plaf.basic.BasicListUI@16ab4e
      UI.index: 0
      ---------------------------------------------
      ======================================================================

            svioletsunw Scott Violet (Inactive)
            knasunw Kna Kna (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: