Path: blob/master/test/jdk/javax/swing/JSpinner/WrongEditorTextFieldFont/FontSetToNull.java
41153 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*/2223import java.awt.Font;2425import javax.swing.JSpinner;26import javax.swing.SwingUtilities;27import javax.swing.UIManager;28import javax.swing.UnsupportedLookAndFeelException;29import javax.swing.plaf.UIResource;3031import static javax.swing.JSpinner.DefaultEditor;32import static javax.swing.UIManager.getInstalledLookAndFeels;3334/**35* @test36* @key headful37* @bug 821073938*/39public final class FontSetToNull {4041private static final Font USERS_FONT = new Font("dialog", Font.BOLD, 41);4243public static void main(final String[] args) throws Exception {44for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {45SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));46SwingUtilities.invokeAndWait(() -> {47// default spinner48test(new JSpinner());49// spinner which always uses the null font50test(new JSpinner(){51@Override52public Font getFont() {53return null;54}55});56});57}58}5960/**61* A sequence of methods that test a possible NPE.62*/63private static void test(JSpinner spinner) {64final DefaultEditor de = (DefaultEditor) spinner.getEditor();6566spinner.setFont(null); // Check possible NPE67SwingUtilities.updateComponentTreeUI(de); // Check possible NPE68spinner.setFont(null); // Check possible NPE6970// should not replace the font of the TextField71de.getTextField().setFont(USERS_FONT);72spinner.setFont(null);7374final Font tff = de.getTextField().getFont();75if (tff instanceof UIResource || !tff.equals(USERS_FONT)) {76throw new RuntimeException("Wrong font: " + tff);77}7879spinner.setEditor(new JSpinner().getEditor()); // Check possible NPE80}8182private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {83try {84UIManager.setLookAndFeel(laf.getClassName());85System.out.println("LookAndFeel: " + laf.getClassName());86} catch (ClassNotFoundException | InstantiationException |87UnsupportedLookAndFeelException | IllegalAccessException e) {88throw new RuntimeException(e);89}90}91}929394