From 611bc7f7350805da8624b8b4e883f05412f43dd9 Mon Sep 17 00:00:00 2001 From: Alisen Chung Date: Mon, 24 Jun 2024 20:41:56 -0700 Subject: [PATCH 1/7] init commit --- .../share/classes/java/awt/TextArea.java | 12 +++++++++++- .../share/classes/java/awt/TextComponent.java | 7 ++++++- .../share/classes/java/awt/TextField.java | 12 +++++++++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/java.desktop/share/classes/java/awt/TextArea.java b/src/java.desktop/share/classes/java/awt/TextArea.java index fcf37543e5b52..2ee29425510aa 100644 --- a/src/java.desktop/share/classes/java/awt/TextArea.java +++ b/src/java.desktop/share/classes/java/awt/TextArea.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 2024, 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 @@ -35,6 +35,7 @@ import javax.accessibility.AccessibleContext; import javax.accessibility.AccessibleState; import javax.accessibility.AccessibleStateSet; +import javax.swing.UIManager; /** * A {@code TextArea} object is a multi-line region @@ -606,6 +607,15 @@ public Dimension minimumSize() { } } + @Override + public synchronized void setEditable(boolean b) { + super.setEditable(b); + Color defaultBackground = UIManager.getColor("TextArea.background"); + if (!backgroundSetByClientCode) { + setBackground(b ? defaultBackground : SystemColor.control, false); + } + } + /** * Returns a string representing the state of this {@code TextArea}. * This method is intended to be used only for debugging purposes, and the diff --git a/src/java.desktop/share/classes/java/awt/TextComponent.java b/src/java.desktop/share/classes/java/awt/TextComponent.java index 9c33172d9f595..3c603f0dc3a05 100644 --- a/src/java.desktop/share/classes/java/awt/TextComponent.java +++ b/src/java.desktop/share/classes/java/awt/TextComponent.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 2024, 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 @@ -359,6 +359,11 @@ public void setBackground(Color c) { super.setBackground(c); } + void setBackground(Color c, boolean setByClient) { + backgroundSetByClientCode = setByClient; + super.setBackground(c); + } + /** * Gets the start position of the selected text in * this text component. diff --git a/src/java.desktop/share/classes/java/awt/TextField.java b/src/java.desktop/share/classes/java/awt/TextField.java index 82296f62706ac..b7d8cb3bf3400 100644 --- a/src/java.desktop/share/classes/java/awt/TextField.java +++ b/src/java.desktop/share/classes/java/awt/TextField.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1995, 2024, 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 @@ -37,6 +37,7 @@ import javax.accessibility.AccessibleContext; import javax.accessibility.AccessibleState; import javax.accessibility.AccessibleStateSet; +import javax.swing.UIManager; /** * A {@code TextField} object is a text component @@ -488,6 +489,15 @@ public Dimension minimumSize() { } } + @Override + public synchronized void setEditable(boolean b) { + super.setEditable(b); + Color defaultBackground = UIManager.getColor("TextField.background"); + if (!backgroundSetByClientCode) { + setBackground(b ? defaultBackground : SystemColor.control, false); + } + } + /** * Adds the specified action listener to receive * action events from this text field. From 2a0cdbdb05f7f74e91b09301409b2da123f990be Mon Sep 17 00:00:00 2001 From: Alisen Chung Date: Mon, 1 Jul 2024 09:37:34 -0700 Subject: [PATCH 2/7] add test --- .../swing/JTextField/BackgroundTest.java | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 test/jdk/javax/swing/JTextField/BackgroundTest.java diff --git a/test/jdk/javax/swing/JTextField/BackgroundTest.java b/test/jdk/javax/swing/JTextField/BackgroundTest.java new file mode 100644 index 0000000000000..4f79ea56b57ce --- /dev/null +++ b/test/jdk/javax/swing/JTextField/BackgroundTest.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 1999, 2024, 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. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.awt.Button; +import java.awt.Color; +import java.awt.FlowLayout; +import java.awt.Frame; +import java.awt.TextArea; +import java.awt.TextField; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/* + * @test + * @bug 4258667 4405602 + * @summary Make sure TextComponents are grayed out when non-editable + * if the background color has not been set by client code. + * Make sure TextComponents are not grayed out when non-editable + * if the background color has been set by client code. + * @key headful + * @library /java/awt/regtesthelpers + * @build PassFailJFrame + * @run main/manual BackgroundTest + */ + +public class BackgroundTest { + private static final String instructions = + """ + The test frame should have a blue background. + The first TextField and TextArea will be the default color. + On Windows and macOS, this is usually white. On Solaris, it will match + your environment settings. + The second TextField and TextArea will be green. + + Press the DisableText button. + + The first TextField and TextArea should change colors to the + default disabled color. On Windows, this is usually gray. + On Solaris, it will match your environment settings. If either + the TextField or the TextArea do not change colors as described, + the test FAILS. + + The second TextField and TextArea should still be green. + If either of them are not green, the test FAILS. + + Press the EnableText button (same button as before). + + The first TextField and TextArea should return to their + original colors as described in the first paragraph. If they + do not, the test FAILS. + + The second TextField and TextArea should still be green. + If either of them are not green, the test FAILS. + + Otherwise, the test PASSES. + """; + + public static void main(String[] args) throws Exception { + PassFailJFrame.builder() + .instructions(instructions) + .rows((int) instructions.lines().count() + 1) + .columns(45) + .testUI(BackgroundTest::createUI) + .build() + .awaitAndCheck(); + } + + public static Frame createUI() { + Frame f = new Frame("BackgroundTest"); + + f.setBackground(Color.blue); + f.setLayout(new FlowLayout(FlowLayout.CENTER)); + + TextField tf = new TextField(30); + TextArea ta = new TextArea(4, 30); + TextField setTf = new TextField(30); + TextArea setTa = new TextArea(4, 30); + + Button enableButton = new Button("DisableText"); + enableButton.setBackground(Color.red); + + tf.setText("Background not set - should be default"); + tf.setEditable(true); + f.add(tf); + ta.setText("Background not set - should be default"); + ta.setEditable(true); + f.add(ta); + + setTf.setText("Background is set - should be Green"); + setTf.setBackground(Color.green); + setTf.setEditable(true); + f.add(setTf); + setTa.setText("Background is set - should be Green"); + setTa.setBackground(Color.green); + setTa.setEditable(true); + f.add(setTa); + + enableButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + boolean currentlyEditable = tf.isEditable(); + + tf.setEditable(!currentlyEditable); + ta.setEditable(!currentlyEditable); + setTf.setEditable(!currentlyEditable); + setTa.setEditable(!currentlyEditable); + enableButton.setLabel(currentlyEditable ? "EnableText" : "DisableText"); + } + }); + + f.add(enableButton); + + f.setSize(300, 300); + return f; + } +} From 0b0f049c4af66f9f22b0b7c11ab94bc20cdaeba1 Mon Sep 17 00:00:00 2001 From: Alisen Chung Date: Wed, 24 Jul 2024 16:58:46 -0700 Subject: [PATCH 3/7] moved test to awt --- .../JTextField => java/awt/TextComponent}/BackgroundTest.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/jdk/{javax/swing/JTextField => java/awt/TextComponent}/BackgroundTest.java (100%) diff --git a/test/jdk/javax/swing/JTextField/BackgroundTest.java b/test/jdk/java/awt/TextComponent/BackgroundTest.java similarity index 100% rename from test/jdk/javax/swing/JTextField/BackgroundTest.java rename to test/jdk/java/awt/TextComponent/BackgroundTest.java From e83c29fed157b0777dc5b0f1925694146386cb92 Mon Sep 17 00:00:00 2001 From: Alisen Chung Date: Tue, 6 Aug 2024 14:31:50 -0700 Subject: [PATCH 4/7] use getbackground --- src/java.desktop/share/classes/java/awt/TextArea.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java.desktop/share/classes/java/awt/TextArea.java b/src/java.desktop/share/classes/java/awt/TextArea.java index 2ee29425510aa..e637f7331d6b7 100644 --- a/src/java.desktop/share/classes/java/awt/TextArea.java +++ b/src/java.desktop/share/classes/java/awt/TextArea.java @@ -610,9 +610,9 @@ public Dimension minimumSize() { @Override public synchronized void setEditable(boolean b) { super.setEditable(b); - Color defaultBackground = UIManager.getColor("TextArea.background"); + Color defaultBackground = this.getBackground(); if (!backgroundSetByClientCode) { - setBackground(b ? defaultBackground : SystemColor.control, false); + setBackground(defaultBackground, false); } } From 18f896bcdc5036c88648a78c5a88b8b6f3c6bb9a Mon Sep 17 00:00:00 2001 From: Alisen Chung Date: Mon, 12 Aug 2024 09:26:42 -0700 Subject: [PATCH 5/7] remove uimanager from textfield --- src/java.desktop/share/classes/java/awt/TextField.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java.desktop/share/classes/java/awt/TextField.java b/src/java.desktop/share/classes/java/awt/TextField.java index b7d8cb3bf3400..cdfd720ebae6c 100644 --- a/src/java.desktop/share/classes/java/awt/TextField.java +++ b/src/java.desktop/share/classes/java/awt/TextField.java @@ -492,9 +492,9 @@ public Dimension minimumSize() { @Override public synchronized void setEditable(boolean b) { super.setEditable(b); - Color defaultBackground = UIManager.getColor("TextField.background"); + Color defaultBackground = this.getBackground(); if (!backgroundSetByClientCode) { - setBackground(b ? defaultBackground : SystemColor.control, false); + setBackground(defaultBackground, false); } } From ccf63c8a86740dbcd84979147e87ed30d983d75a Mon Sep 17 00:00:00 2001 From: Alisen Chung Date: Mon, 12 Aug 2024 09:27:47 -0700 Subject: [PATCH 6/7] remove import --- src/java.desktop/share/classes/java/awt/TextField.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/java.desktop/share/classes/java/awt/TextField.java b/src/java.desktop/share/classes/java/awt/TextField.java index cdfd720ebae6c..a0572b43534bd 100644 --- a/src/java.desktop/share/classes/java/awt/TextField.java +++ b/src/java.desktop/share/classes/java/awt/TextField.java @@ -37,7 +37,6 @@ import javax.accessibility.AccessibleContext; import javax.accessibility.AccessibleState; import javax.accessibility.AccessibleStateSet; -import javax.swing.UIManager; /** * A {@code TextField} object is a text component From f8b946873f6b8ed8ca2d60e79769451ce0662a01 Mon Sep 17 00:00:00 2001 From: Alisen Chung Date: Tue, 20 Aug 2024 16:49:35 -0700 Subject: [PATCH 7/7] fix import --- src/java.desktop/share/classes/java/awt/TextArea.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/java.desktop/share/classes/java/awt/TextArea.java b/src/java.desktop/share/classes/java/awt/TextArea.java index e637f7331d6b7..cea9d0dedd83d 100644 --- a/src/java.desktop/share/classes/java/awt/TextArea.java +++ b/src/java.desktop/share/classes/java/awt/TextArea.java @@ -35,7 +35,6 @@ import javax.accessibility.AccessibleContext; import javax.accessibility.AccessibleState; import javax.accessibility.AccessibleStateSet; -import javax.swing.UIManager; /** * A {@code TextArea} object is a multi-line region