8369327: On macOS List loses selection when added to Frame

Reviewed-by: aivanov, azvegint, prr
This commit is contained in:
Sergey Bylokhov 2026-04-22 20:15:45 +00:00
parent 03996ff5e0
commit 8bef687f2e
6 changed files with 685 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -45,7 +45,7 @@ import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
@ -53,6 +53,9 @@ import static java.awt.event.ItemEvent.DESELECTED;
import static java.awt.event.ItemEvent.ITEM_STATE_CHANGED;
import static java.awt.event.ItemEvent.SELECTED;
import static javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
import static javax.swing.ListSelectionModel.SINGLE_SELECTION;
/**
* Lightweight implementation of {@link ListPeer}. Delegates most of the work to
* the {@link JList}, which is placed inside {@link JScrollPane}.
@ -157,11 +160,14 @@ final class LWListPeer extends LWComponentPeer<List, LWListPeer.ScrollableJList>
@Override
public void select(final int index) {
synchronized (getDelegateLock()) {
getDelegate().setSkipStateChangedEvent(true);
try {
getDelegate().getView().setSelectedIndex(index);
} finally {
getDelegate().setSkipStateChangedEvent(false);
ListModel<String> model = getDelegate().getModel();
if (index >= 0 && index < model.getSize()) {
getDelegate().setSkipStateChangedEvent(true);
try {
getDelegate().getView().addSelectionInterval(index, index);
} finally {
getDelegate().setSkipStateChangedEvent(false);
}
}
}
}
@ -188,9 +194,23 @@ final class LWListPeer extends LWComponentPeer<List, LWListPeer.ScrollableJList>
@Override
public void setMultipleMode(final boolean m) {
synchronized (getDelegateLock()) {
getDelegate().getView().setSelectionMode(m ?
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
: ListSelectionModel.SINGLE_SELECTION);
JList<String> view = getDelegate().getView();
int newMode = m ? MULTIPLE_INTERVAL_SELECTION : SINGLE_SELECTION;
if (view.getSelectionMode() == newMode) {
return;
}
int lead = view.getLeadSelectionIndex();
boolean wasSelected = lead != -1 && view.isSelectedIndex(lead);
getDelegate().setSkipStateChangedEvent(true);
try {
view.clearSelection();
view.setSelectionMode(newMode);
if (wasSelected) {
view.setSelectedIndex(lead);
}
} finally {
getDelegate().setSkipStateChangedEvent(false);
}
}
}

View File

@ -151,6 +151,7 @@ java/awt/grab/EmbeddedFrameTest1/EmbeddedFrameTest1.java 7080150 macosx-all
java/awt/event/InputEvent/EventWhenTest/EventWhenTest.java 8168646 generic-all
java/awt/List/KeyEventsTest/KeyEventsTest.java 8201307 linux-all
java/awt/List/NoEvents/ProgrammaticChange.java 8201307 linux-all
java/awt/List/ListSelection/SelectInvalidTest.java 8369455 linux-all
java/awt/Paint/ListRepaint.java 8201307 linux-all
java/awt/Mixing/AWT_Mixing/OpaqueOverlapping.java 8370584 windows-x64
java/awt/Mixing/AWT_Mixing/OpaqueOverlappingChoice.java 8048171 generic-all

View File

@ -0,0 +1,159 @@
/*
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Frame;
import java.awt.List;
import static jdk.test.lib.Asserts.assertEquals;
import static jdk.test.lib.Asserts.assertFalse;
import static jdk.test.lib.Asserts.assertTrue;
/**
* @test
* @bug 8369327
* @summary Test awt list deselection methods
* @key headful
* @library /test/lib
* @build jdk.test.lib.Asserts
* @run main DeselectionUnitTest
*/
public final class DeselectionUnitTest {
public static void main(String[] args) {
testNonDisplayable(DeselectionUnitTest::testSingleMode);
testNonDisplayable(DeselectionUnitTest::testMultipleMode);
testNonDisplayable(DeselectionUnitTest::testInvalidDeselection);
testNonDisplayable(DeselectionUnitTest::testEmptyListDeselection);
testDisplayable(DeselectionUnitTest::testSingleMode);
testDisplayable(DeselectionUnitTest::testMultipleMode);
testDisplayable(DeselectionUnitTest::testInvalidDeselection);
testDisplayable(DeselectionUnitTest::testEmptyListDeselection);
}
interface Test {
void execute(Frame frame);
}
private static void testNonDisplayable(Test test) {
test.execute(null);
}
private static void testDisplayable(Test test) {
Frame frame = new Frame();
try {
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
test.execute(frame);
} finally {
frame.dispose();
}
}
private static List createList(Frame frame, boolean multi) {
List list = new List(4, multi);
if (frame != null) {
frame.add(list);
}
list.add("Item1");
list.add("Item2");
list.add("Item3");
return list;
}
private static void testSingleMode(Frame frame) {
List list = createList(frame, false);
// Select and deselect single item
list.select(1);
assertTrue(list.isIndexSelected(1));
list.deselect(1);
assertEquals(-1, list.getSelectedIndex());
assertEquals(0, list.getSelectedIndexes().length);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
// Deselect non-selected item (should be no-op)
list.select(0);
list.deselect(2);
assertEquals(0, list.getSelectedIndex());
assertTrue(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
}
private static void testMultipleMode(Frame frame) {
List list = createList(frame, true);
// Select multiple items and deselect one
list.select(0);
list.select(1);
list.select(2);
assertEquals(3, list.getSelectedIndexes().length);
list.deselect(1);
assertEquals(2, list.getSelectedIndexes().length);
assertTrue(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertTrue(list.isIndexSelected(2));
// Deselect all remaining
list.deselect(0);
list.deselect(2);
assertEquals(-1, list.getSelectedIndex());
assertEquals(0, list.getSelectedIndexes().length);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
}
private static void testInvalidDeselection(Frame frame) {
List list = createList(frame, false);
// Deselect invalid indices (should be no-op)
list.select(0);
list.deselect(-1);
list.deselect(5);
assertEquals(0, list.getSelectedIndex());
assertTrue(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
}
private static void testEmptyListDeselection(Frame frame) {
List list = new List();
if (frame != null) {
frame.add(list);
}
// Deselect on empty list (should be no-op)
list.deselect(0);
list.deselect(-1);
assertEquals(-1, list.getSelectedIndex());
assertEquals(0, list.getSelectedIndexes().length);
assertFalse(list.isIndexSelected(0));
}
}

View File

@ -0,0 +1,216 @@
/*
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Frame;
import java.awt.List;
import jdk.test.lib.Platform;
import static jdk.test.lib.Asserts.assertEquals;
import static jdk.test.lib.Asserts.assertFalse;
import static jdk.test.lib.Asserts.assertTrue;
/**
* @test
* @bug 8369327
* @summary Test awt list selection of invalid indexes
* @key headful
* @library /test/lib
* @build jdk.test.lib.Asserts jdk.test.lib.Platform
* @run main SelectInvalidTest
*/
public final class SelectInvalidTest {
/**
* A special index on windows, selects or deselects all elements.
*/
private static final int WINDOWS_INVALID = -1;
/**
* The list of invalid indexes, their usages should be noop.
*/
private static final int[] INVALID = {
WINDOWS_INVALID, Integer.MIN_VALUE, -100, 3, 100, Integer.MAX_VALUE
};
public static void main(String[] args) {
for (int i : INVALID) {
if (Platform.isWindows() && i == WINDOWS_INVALID) {
testDisplayable(SelectInvalidTest::testWinDeselectAllSingleMode, i);
testDisplayable(SelectInvalidTest::testWinSelectAllMultipleMode, i);
} else {
testDisplayable(SelectInvalidTest::testSingleMode, i);
testDisplayable(SelectInvalidTest::testMultipleMode, i);
testDisplayable(SelectInvalidTest::testEmptySelection, i);
}
}
}
interface Test {
void execute(Frame frame, int invalid);
}
private static void testDisplayable(Test test, int invalid) {
Frame frame = new Frame();
try {
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
test.execute(frame, invalid);
} finally {
frame.dispose();
}
}
private static void testSingleMode(Frame frame, int invalid) {
List list = new List(4, false);
frame.add(list);
list.add("Item1");
list.add("Item2");
list.add("Item3");
// Test initial state
list.select(invalid);
assertEquals(-1, list.getSelectedIndex());
assertEquals(0, list.getSelectedIndexes().length);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
// Test single selection
list.select(1);
list.select(invalid);
assertEquals(1, list.getSelectedIndex());
assertEquals(1, list.getSelectedIndexes().length);
assertEquals(1, list.getSelectedIndexes()[0]);
assertFalse(list.isIndexSelected(0));
assertTrue(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
// Test selection replacement in single mode
list.select(2);
list.select(invalid);
assertEquals(2, list.getSelectedIndex());
assertEquals(1, list.getSelectedIndexes().length);
assertEquals(2, list.getSelectedIndexes()[0]);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertTrue(list.isIndexSelected(2));
}
private static void testMultipleMode(Frame frame, int invalid) {
List list = new List(4, true);
frame.add(list);
list.add("Item1");
list.add("Item2");
list.add("Item3");
// Test multiple selections
list.select(0);
list.select(2);
list.select(invalid);
// Returns -1 for multiple selections
assertEquals(-1, list.getSelectedIndex());
assertEquals(2, list.getSelectedIndexes().length);
assertTrue(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertTrue(list.isIndexSelected(2));
// Test partial deselection
list.deselect(0);
list.select(invalid);
// Single selection remaining
assertEquals(2, list.getSelectedIndex());
assertEquals(1, list.getSelectedIndexes().length);
assertEquals(2, list.getSelectedIndexes()[0]);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertTrue(list.isIndexSelected(2));
}
private static void testEmptySelection(Frame frame, int invalid) {
List list = new List();
frame.add(list);
list.select(invalid);
assertEquals(-1, list.getSelectedIndex());
assertEquals(0, list.getSelectedIndexes().length);
assertFalse(list.isIndexSelected(0));
}
private static void testWinDeselectAllSingleMode(Frame frame, int invalid) {
List list = new List(4, false);
frame.add(list);
list.add("Item1");
list.add("Item2");
list.add("Item3");
list.select(invalid);
assertEquals(-1, list.getSelectedIndex());
assertEquals(0, list.getSelectedIndexes().length);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
list.select(1);
list.select(invalid);
assertEquals(-1, list.getSelectedIndex());
assertEquals(0, list.getSelectedIndexes().length);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
list.select(2);
list.select(invalid);
assertEquals(-1, list.getSelectedIndex());
assertEquals(0, list.getSelectedIndexes().length);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
}
private static void testWinSelectAllMultipleMode(Frame frame, int invalid) {
List list = new List(4, true);
frame.add(list);
list.add("Item1");
list.add("Item2");
list.add("Item3");
list.select(0);
list.select(2);
list.select(invalid);
assertEquals(-1, list.getSelectedIndex());
assertEquals(3, list.getSelectedIndexes().length);
assertTrue(list.isIndexSelected(0));
assertTrue(list.isIndexSelected(1));
assertTrue(list.isIndexSelected(2));
list.deselect(0);
list.select(invalid);
assertEquals(-1, list.getSelectedIndex());
assertEquals(3, list.getSelectedIndexes().length);
assertTrue(list.isIndexSelected(0));
assertTrue(list.isIndexSelected(1));
assertTrue(list.isIndexSelected(2));
}
}

View File

@ -0,0 +1,146 @@
/*
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Frame;
import java.awt.List;
import static jdk.test.lib.Asserts.assertEquals;
import static jdk.test.lib.Asserts.assertFalse;
import static jdk.test.lib.Asserts.assertTrue;
/**
* @test
* @bug 8369327
* @summary Test awt list selection methods
* @key headful
* @library /test/lib
* @build jdk.test.lib.Asserts
* @run main SelectionUnitTest
*/
public final class SelectionUnitTest {
public static void main(String[] args) {
testNonDisplayable(SelectionUnitTest::testSingleMode);
testNonDisplayable(SelectionUnitTest::testMultipleMode);
testNonDisplayable(SelectionUnitTest::testEmptySelection);
testDisplayable(SelectionUnitTest::testSingleMode);
testDisplayable(SelectionUnitTest::testMultipleMode);
testDisplayable(SelectionUnitTest::testEmptySelection);
}
interface Test {
void execute(Frame frame);
}
private static void testNonDisplayable(Test test) {
test.execute(null);
}
private static void testDisplayable(Test test) {
Frame frame = new Frame();
try {
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
test.execute(frame);
} finally {
frame.dispose();
}
}
private static List createList(Frame frame, boolean multi) {
List list = new List(4, multi);
if (frame != null) {
frame.add(list);
}
list.add("Item1");
list.add("Item2");
list.add("Item3");
return list;
}
private static void testSingleMode(Frame frame) {
List list = createList(frame, false);
// Test initial state
assertEquals(-1, list.getSelectedIndex());
assertEquals(0, list.getSelectedIndexes().length);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
// Test single selection
list.select(1);
assertEquals(1, list.getSelectedIndex());
assertEquals(1, list.getSelectedIndexes().length);
assertEquals(1, list.getSelectedIndexes()[0]);
assertFalse(list.isIndexSelected(0));
assertTrue(list.isIndexSelected(1));
assertFalse(list.isIndexSelected(2));
// Test selection replacement in single mode
list.select(2);
assertEquals(2, list.getSelectedIndex());
assertEquals(1, list.getSelectedIndexes().length);
assertEquals(2, list.getSelectedIndexes()[0]);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertTrue(list.isIndexSelected(2));
}
private static void testMultipleMode(Frame frame) {
List list = createList(frame, true);
// Test multiple selections
list.select(0);
list.select(2);
// Returns -1 for multiple selections
assertEquals(-1, list.getSelectedIndex());
assertEquals(2, list.getSelectedIndexes().length);
assertTrue(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertTrue(list.isIndexSelected(2));
// Test partial deselection
list.deselect(0);
// Single selection remaining
assertEquals(2, list.getSelectedIndex());
assertEquals(1, list.getSelectedIndexes().length);
assertEquals(2, list.getSelectedIndexes()[0]);
assertFalse(list.isIndexSelected(0));
assertFalse(list.isIndexSelected(1));
assertTrue(list.isIndexSelected(2));
}
private static void testEmptySelection(Frame frame) {
List list = new List();
if (frame != null) {
frame.add(list);
}
assertEquals(-1, list.getSelectedIndex());
assertEquals(0, list.getSelectedIndexes().length);
assertFalse(list.isIndexSelected(0));
}
}

View File

@ -0,0 +1,133 @@
/*
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Frame;
import java.awt.List;
import java.awt.Toolkit;
import java.util.Arrays;
/**
* @test
* @bug 8369327
* @summary Test awt list setMultipleMode selection
* @key headful
*/
public final class SetMultipleModeTest {
public static void main(String[] args) {
// Non-displayable list
// test(null); Does not work per the spec
// Displayable list
Frame frame = new Frame();
try {
test(frame);
} finally {
frame.dispose();
}
}
private static void test(Frame frame) {
List list = new List();
list.add("Item1");
list.add("Item2");
list.add("Item3");
if (frame != null) {
frame.add(list);
frame.pack();
}
// Empty: mode switch preserves empty
list.setMultipleMode(true);
check(list);
list.deselect(0);
check(list);
list.setMultipleMode(false);
check(list);
// Single to multi preserves selection
list.select(1);
list.setMultipleMode(true);
check(list, 1);
// Multi to multi is no-op
list.select(0);
list.select(2);
check(list, 0, 1, 2);
list.setMultipleMode(true);
check(list, 0, 1, 2);
// Multi to single keeps lead
list.setMultipleMode(false);
check(list, 2);
// Single to single is no-op
list.setMultipleMode(false);
check(list, 2);
// Round-trip
list.setMultipleMode(true);
check(list, 2);
list.setMultipleMode(false);
check(list, 2);
// XAWT does not move the focus cursor on deselect(),
// so multi->single keeps the focused item, skip on XAWT
boolean isXToolkit = "sun.awt.X11.XToolkit".equals(
Toolkit.getDefaultToolkit().getClass().getName());
if (!isXToolkit) {
// Deselect lead in multi, switch to single
list.setMultipleMode(true);
list.select(0);
list.select(1);
check(list, 0, 1, 2);
list.deselect(2);
check(list, 0, 1);
list.setMultipleMode(false);
check(list);
// Deselect non-selected in multi, no-op
list.setMultipleMode(true);
list.select(0);
check(list, 0);
list.deselect(2);
check(list, 0);
list.setMultipleMode(false);
check(list);
}
if (frame != null) {
frame.remove(list);
}
}
private static void check(List list, int... expected) {
int[] actual = list.getSelectedIndexes();
Arrays.sort(actual);
if (!Arrays.equals(expected, actual)) {
throw new RuntimeException(
"Expected %s, got %s".formatted(Arrays.toString(expected),
Arrays.toString(actual)));
}
}
}