Path: blob/master/test/jdk/javax/swing/JList/SetSelectedValueTest.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @key headful25* @bug 507676126* @summary Verifies that the selection is cleared when setSelectedValue is27* called with null28* @run main SetSelectedValueTest29*/3031import javax.swing.SwingUtilities;32import javax.swing.DefaultListModel;33import javax.swing.JList;34import javax.swing.ListSelectionModel;35import java.util.Collections;36import java.util.List;3738public class SetSelectedValueTest {39public static void main(String[] args) throws Exception {4041SwingUtilities.invokeAndWait(new Runnable() {42public void run() {43// Create a JList with 2 elements44DefaultListModel dlm = new DefaultListModel();45JList list = new JList<String>(dlm);46list.setSelectionMode(47ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);48dlm.addElement("1");49dlm.addElement("2");5051// Select both the elements added in list52list.setSelectionInterval(0, 1);53checkSelectionByList(list, List.of("1", "2"));5455// Set the selected value as null. This should clear the56// selection57list.setSelectedValue(null, true);58checkSelectionByList(list, Collections.emptyList());5960// Select both the elements added in list61list.setSelectionInterval(0, 1);62checkSelectionByList(list, List.of("1", "2"));63}64});65}6667static void checkSelectionByList(JList list, List<String> selectionList)68throws RuntimeException {69List<String> listSelection = list.getSelectedValuesList();70if (!listSelection.equals(selectionList)) {71System.out.println("Expected: " + selectionList);72System.out.println("Actual: " + listSelection);73throw new RuntimeException("Wrong selection");74}75}76}777879