Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JList/SetSelectedValueTest.java
41149 views
1
/*
2
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
/*
24
* @test
25
* @key headful
26
* @bug 5076761
27
* @summary Verifies that the selection is cleared when setSelectedValue is
28
* called with null
29
* @run main SetSelectedValueTest
30
*/
31
32
import javax.swing.SwingUtilities;
33
import javax.swing.DefaultListModel;
34
import javax.swing.JList;
35
import javax.swing.ListSelectionModel;
36
import java.util.Collections;
37
import java.util.List;
38
39
public class SetSelectedValueTest {
40
public static void main(String[] args) throws Exception {
41
42
SwingUtilities.invokeAndWait(new Runnable() {
43
public void run() {
44
// Create a JList with 2 elements
45
DefaultListModel dlm = new DefaultListModel();
46
JList list = new JList<String>(dlm);
47
list.setSelectionMode(
48
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
49
dlm.addElement("1");
50
dlm.addElement("2");
51
52
// Select both the elements added in list
53
list.setSelectionInterval(0, 1);
54
checkSelectionByList(list, List.of("1", "2"));
55
56
// Set the selected value as null. This should clear the
57
// selection
58
list.setSelectedValue(null, true);
59
checkSelectionByList(list, Collections.emptyList());
60
61
// Select both the elements added in list
62
list.setSelectionInterval(0, 1);
63
checkSelectionByList(list, List.of("1", "2"));
64
}
65
});
66
}
67
68
static void checkSelectionByList(JList list, List<String> selectionList)
69
throws RuntimeException {
70
List<String> listSelection = list.getSelectedValuesList();
71
if (!listSelection.equals(selectionList)) {
72
System.out.println("Expected: " + selectionList);
73
System.out.println("Actual: " + listSelection);
74
throw new RuntimeException("Wrong selection");
75
}
76
}
77
}
78
79