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

On macOS List may loses selection when added to Frame

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3 P3
    • tbd
    • 11, 17, 21, 25
    • client-libs
    • None
    • generic
    • os_x

      On macOS, when a List component is added to a Frame, it may lose its previous selections. This happens even if the list was set to allow multiple selections.

      In the example below, selecting two items works at first. But after adding the list to the frame, the first selection is lost when selecting another item.

      This does not happen if the list is not added to the frame.

      import java.awt.Frame;
      import java.awt.List;
      import java.util.Arrays;

      public class ListSelectionBug {
          public static void main(String[] args) {
              Frame frame = new Frame();
              List list = new List(4, true); // multi-selection list
              list.add("Item1");
              list.add("Item2");

              System.out.println("selected " + Arrays.toString(list.getSelectedIndexes()));
              list.select(0); // select first item
              System.out.println("selected " + Arrays.toString(list.getSelectedIndexes()));
              list.select(1); // select second item
              System.out.println("selected " + Arrays.toString(list.getSelectedIndexes()));

              frame.add(list); // comment out this line to avoid the bug
              frame.pack();

              System.out.println("selected " + Arrays.toString(list.getSelectedIndexes()));
              list.select(1); // BUG: selection of first item is lost
              System.out.println("selected " + Arrays.toString(list.getSelectedIndexes()));

              frame.dispose();
          }
      }

            serb Sergey Bylokhov
            serb Sergey Bylokhov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: