7022041: TitleBorder Null Pointer Exception
Summary: Added check if getTitleFont() and getTitleColor() don't return null
Reviewed-by: alexsch
--- a/jdk/src/share/classes/javax/swing/border/TitledBorder.java Fri Jul 06 14:20:27 2012 +0400
+++ b/jdk/src/share/classes/javax/swing/border/TitledBorder.java Thu Jul 12 16:54:47 2012 +0200
@@ -440,7 +440,7 @@
* @return the title-font of the titled border
*/
public Font getTitleFont() {
- return titleFont;
+ return titleFont == null ? UIManager.getFont("TitledBorder.font") : titleFont;
}
/**
@@ -449,7 +449,7 @@
* @return the title-color of the titled border
*/
public Color getTitleColor() {
- return titleColor;
+ return titleColor == null ? UIManager.getColor("TitledBorder.titleColor") : titleColor;
}
@@ -681,10 +681,6 @@
if (font != null) {
return font;
}
- font = UIManager.getFont("TitledBorder.font");
- if (font != null) {
- return font;
- }
if (c != null) {
font = c.getFont();
if (font != null) {
@@ -699,10 +695,6 @@
if (color != null) {
return color;
}
- color = UIManager.getColor("TitledBorder.titleColor");
- if (color != null) {
- return color;
- }
return (c != null)
? c.getForeground()
: null;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/border/Test7022041.java Thu Jul 12 16:54:47 2012 +0200
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2012 Red Hat, Inc. All Rights Reserved.
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+import java.awt.Color;
+import java.awt.Font;
+
+import javax.swing.border.EmptyBorder;
+import javax.swing.border.TitledBorder;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+
+/* @test
+ * @bug 7022041
+ * @summary This test check the behaviour of getTitleFont() and getTitleColor()
+ * methods of the TitledBorder class.
+ * @author Pavel Tisnovsky
+ */
+public class Test7022041 {
+
+ public static void main(String[] args) throws Exception {
+ UIManager.LookAndFeelInfo[] installedLookAndFeels = UIManager.getInstalledLookAndFeels();
+ // try to test all installed Look and Feels
+ for (UIManager.LookAndFeelInfo lookAndFeel : installedLookAndFeels) {
+ String name = lookAndFeel.getName();
+ System.out.println("Testing " + name);
+ // Some Look and Feels work only when test is run in a GUI environment
+ // (GTK+ LAF is an example)
+ try {
+ UIManager.setLookAndFeel(lookAndFeel.getClassName());
+ checkTitleColor();
+ System.out.println(" titleColor test ok");
+ checkTitleFont();
+ System.out.println(" titleFont test ok");
+ }
+ catch (UnsupportedLookAndFeelException e) {
+ System.out.println(" Note: LookAndFeel " + name
+ + " is not supported on this configuration");
+ }
+ }
+ }
+
+ /**
+ * Check behaviour of method TitledBorder.getTitleColor()
+ */
+ private static void checkTitleColor() {
+ TitledBorder titledBorder = new TitledBorder(new EmptyBorder(1, 1, 1, 1));
+ Color defaultColor = UIManager.getLookAndFeelDefaults().getColor("TitledBorder.titleColor");
+ Color titledBorderColor = titledBorder.getTitleColor();
+
+ // check default configuration
+ if (defaultColor == null) {
+ if (titledBorderColor == null) {
+ return;
+ }
+ else {
+ throw new RuntimeException("TitledBorder default color should be null");
+ }
+ }
+ if (!defaultColor.equals(titledBorderColor)) {
+ throw new RuntimeException("L&F default color " + defaultColor.toString()
+ + " differs from TitledBorder color " + titledBorderColor.toString());
+ }
+
+ // title color is explicitly specified
+ Color color = Color.green;
+ titledBorder.setTitleColor(color);
+ if (!color.equals(titledBorder.getTitleColor())) {
+ throw new RuntimeException("TitledBorder color should be " + color.toString());
+ }
+
+ // title color is unspecified
+ titledBorder.setTitleColor(null);
+ if (!defaultColor.equals(titledBorder.getTitleColor())) {
+ throw new RuntimeException("L&F default color " + defaultColor.toString()
+ + " differs from TitledBorder color " + titledBorderColor.toString());
+ }
+ }
+
+ /**
+ * Check behaviour of method TitledBorder.getTitleFont()
+ */
+ private static void checkTitleFont() {
+ TitledBorder titledBorder = new TitledBorder(new EmptyBorder(1, 1, 1, 1));
+ Font defaultFont = UIManager.getLookAndFeelDefaults().getFont("TitledBorder.font");
+ Font titledBorderFont = titledBorder.getTitleFont();
+
+ // check default configuration
+ if (defaultFont == null) {
+ if (titledBorderFont == null) {
+ return;
+ }
+ else {
+ throw new RuntimeException("TitledBorder default font should be null");
+ }
+ }
+ if (!defaultFont.equals(titledBorderFont)) {
+ throw new RuntimeException("L&F default font " + defaultFont.toString()
+ + " differs from TitledBorder font " + titledBorderFont.toString());
+ }
+
+ // title font is explicitly specified
+ Font font = new Font("Dialog", Font.PLAIN, 10);
+ titledBorder.setTitleFont(font);
+ if (!font.equals(titledBorder.getTitleFont())) {
+ throw new RuntimeException("TitledBorder font should be " + font.toString());
+ }
+
+ // title Font is unspecified
+ titledBorder.setTitleFont(null);
+ if (!defaultFont.equals(titledBorder.getTitleFont())) {
+ throw new RuntimeException("L&F default font " + defaultFont.toString()
+ + " differs from TitledBorder font " + titledBorderFont.toString());
+ }
+ }
+}
+