1 /* 2 * Copyright (c) 2015, 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 import java.awt.FlowLayout; 25 import java.awt.Font; 26 27 import javax.swing.JFrame; 28 import javax.swing.JSpinner; 29 import javax.swing.SwingUtilities; 30 import javax.swing.UIManager; 31 import javax.swing.UnsupportedLookAndFeelException; 32 import javax.swing.plaf.UIResource; 33 34 import static javax.swing.JSpinner.*; 35 import static javax.swing.UIManager.getInstalledLookAndFeels; 36 37 /** 38 * @test 39 * @key headful 40 * @bug 5036022 41 */ 42 public class FontSetByUser implements Runnable { 43 44 private static final Font USERS_FONT = new Font("dialog", Font.BOLD, 41); 45 46 public static void main(final String[] args) throws Exception { 47 for (final UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) { 48 SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf)); 49 SwingUtilities.invokeAndWait(new FontSetByUser()); 50 } 51 } 52 53 @Override 54 public void run() { 55 final JFrame frame1 = new JFrame(); 56 try { 57 testDefaultFont(frame1); 58 } finally { 59 frame1.dispose(); 60 } 61 } 62 63 private static void testDefaultFont(final JFrame frame) { 64 final JSpinner spinner = new JSpinner(); 65 final JSpinner spinner_u = new JSpinner(); 66 frame.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50)); 67 frame.getContentPane().add(spinner); 68 frame.getContentPane().add(spinner_u); 69 frame.pack(); 70 frame.setLocationRelativeTo(null); 71 frame.setVisible(true); 72 final DefaultEditor ed = (DefaultEditor) spinner.getEditor(); 73 final DefaultEditor ed_u = (DefaultEditor) spinner_u.getEditor(); 74 ed_u.getTextField().setFont(USERS_FONT); 75 76 for (int i = 5; i < 40; i += 5) { 77 /* 78 * Validate that the font of the text field is changed to the 79 * font of JSpinner if the font of text field was not set by the 80 * user. 81 */ 82 final Font tff = ed.getTextField().getFont(); 83 if (!(tff instanceof UIResource)) { 84 throw new RuntimeException("Font must be UIResource"); 85 } 86 if (spinner.getFont().getSize() != tff.getSize()) { 87 throw new RuntimeException("Rrong size"); 88 } 89 spinner.setFont(new Font("dialog", Font.BOLD, i)); 90 /* 91 * Validate that the font of the text field is NOT changed to the 92 * font of JSpinner if the font of text field was set by the user. 93 */ 94 final Font tff_u = ed_u.getTextField().getFont(); 95 if (tff_u instanceof UIResource || !tff_u.equals(USERS_FONT)) { 96 throw new RuntimeException("Font must NOT be UIResource"); 97 } 98 if (spinner_u.getFont().getSize() == tff_u.getSize()) { 99 throw new RuntimeException("Wrong size"); 100 } 101 spinner_u.setFont(new Font("dialog", Font.BOLD, i)); 102 } 103 } 104 105 private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) { 106 try { 107 UIManager.setLookAndFeel(laf.getClassName()); 108 System.out.println("LookAndFeel: " + laf.getClassName()); 109 } catch (ClassNotFoundException | InstantiationException | 110 UnsupportedLookAndFeelException | IllegalAccessException e) { 111 throw new RuntimeException(e); 112 } 113 } 114 }