Path: blob/master/test/jdk/javax/swing/JSpinner/WrongEditorTextFieldFont/FontSetByUser.java
41153 views
/*1* Copyright (c) 2015, 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.FlowLayout;24import java.awt.Font;2526import javax.swing.JFrame;27import javax.swing.JSpinner;28import javax.swing.SwingUtilities;29import javax.swing.UIManager;30import javax.swing.UnsupportedLookAndFeelException;31import javax.swing.plaf.UIResource;3233import static javax.swing.JSpinner.*;34import static javax.swing.UIManager.getInstalledLookAndFeels;3536/**37* @test38* @key headful39* @bug 503602240*/41public class FontSetByUser implements Runnable {4243private static final Font USERS_FONT = new Font("dialog", Font.BOLD, 41);4445public static void main(final String[] args) throws Exception {46for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {47SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));48SwingUtilities.invokeAndWait(new FontSetByUser());49}50}5152@Override53public void run() {54final JFrame frame1 = new JFrame();55try {56testDefaultFont(frame1);57} finally {58frame1.dispose();59}60}6162private static void testDefaultFont(final JFrame frame) {63final JSpinner spinner = new JSpinner();64final JSpinner spinner_u = new JSpinner();65frame.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50));66frame.getContentPane().add(spinner);67frame.getContentPane().add(spinner_u);68frame.pack();69frame.setLocationRelativeTo(null);70frame.setVisible(true);71final DefaultEditor ed = (DefaultEditor) spinner.getEditor();72final DefaultEditor ed_u = (DefaultEditor) spinner_u.getEditor();73ed_u.getTextField().setFont(USERS_FONT);7475for (int i = 5; i < 40; i += 5) {76/*77* Validate that the font of the text field is changed to the78* font of JSpinner if the font of text field was not set by the79* user.80*/81final Font tff = ed.getTextField().getFont();82if (!(tff instanceof UIResource)) {83throw new RuntimeException("Font must be UIResource");84}85if (spinner.getFont().getSize() != tff.getSize()) {86throw new RuntimeException("Rrong size");87}88spinner.setFont(new Font("dialog", Font.BOLD, i));89/*90* Validate that the font of the text field is NOT changed to the91* font of JSpinner if the font of text field was set by the user.92*/93final Font tff_u = ed_u.getTextField().getFont();94if (tff_u instanceof UIResource || !tff_u.equals(USERS_FONT)) {95throw new RuntimeException("Font must NOT be UIResource");96}97if (spinner_u.getFont().getSize() == tff_u.getSize()) {98throw new RuntimeException("Wrong size");99}100spinner_u.setFont(new Font("dialog", Font.BOLD, i));101}102}103104private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {105try {106UIManager.setLookAndFeel(laf.getClassName());107System.out.println("LookAndFeel: " + laf.getClassName());108} catch (ClassNotFoundException | InstantiationException |109UnsupportedLookAndFeelException | IllegalAccessException e) {110throw new RuntimeException(e);111}112}113}114115116