1+ /*
2+ * Copyright (c) 1999, 2025, 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+ /*
25+ * @test
26+ * @bug 4258667 4405602
27+ * @summary Make sure TextComponents are grayed out when non-editable
28+ * if the background color has not been set by client code.
29+ * Make sure TextComponents are not grayed out when non-editable
30+ * if the background color has been set by client code.
31+ * @library /java/awt/regtesthelpers
32+ * @build PassFailJFrame
33+ * @run main/manual BackgroundTest
34+ */
35+
36+ import java .awt .Button ;
37+ import java .awt .Color ;
38+ import java .awt .FlowLayout ;
39+ import java .awt .Frame ;
40+ import java .awt .TextArea ;
41+ import java .awt .TextField ;
42+
43+ public class BackgroundTest {
44+ private static final String enableString = "EnableText" ;
45+ private static final String disableString = "DisableText" ;
46+
47+ public static void main (String [] args ) throws Exception {
48+ String INSTRUCTIONS = """
49+ 1. When the frame appears, it should have a blue background.
50+ 2. The first TextField and TextArea will be the default color.
51+ The second TextField and TextArea will be green.
52+ 3. Press the "DisableText" button.
53+ The first TextField and TextArea should change colors to the
54+ default disabled color. On Windows, this is usually gray.
55+ On linux and macos it will match the environment settings.
56+ If the TextField or the TextArea do not change colors as described,
57+ the test FAILS.
58+ 4. The second TextField and TextArea should still be green.
59+ If either of them are not green, the test FAILS.
60+ Press the "EnableText" button (same button as before).
61+ The first TextField and TextArea should return to their
62+ original colors as described in the first paragraph. If they
63+ do not, the test FAILS.
64+ 5. The second TextField and TextArea should still be green.
65+ If either of them are not green, the test FAILS.
66+ Otherwise, the test PASSES.
67+ """ ;
68+
69+ PassFailJFrame .builder ()
70+ .instructions (INSTRUCTIONS )
71+ .columns (35 )
72+ .testUI (BackgroundTest ::initialize )
73+ .build ()
74+ .awaitAndCheck ();
75+ }
76+
77+ public static Frame initialize () {
78+ Frame frame = new Frame ("Background Test" );
79+ frame .setLayout (new FlowLayout ());
80+ TextField tf = new TextField (30 );
81+ TextArea ta = new TextArea (4 , 30 );
82+ TextField setTf = new TextField (30 );
83+ TextArea setTa = new TextArea (4 , 30 );
84+ Button enableButton = new Button (disableString );
85+
86+ enableButton .setBackground (Color .red );
87+ frame .setSize (500 , 250 );
88+
89+ frame .setBackground (Color .blue );
90+
91+ tf .setText ("Background not set - should be default" );
92+ tf .setEditable (true );
93+ frame .add (tf );
94+ ta .setText ("Background not set - should be default" );
95+ ta .setEditable (true );
96+ frame .add (ta );
97+
98+ setTf .setText ("Background is set - should be Green" );
99+ setTf .setBackground (Color .green );
100+ setTf .setEditable (true );
101+ frame .add (setTf );
102+ setTa .setText ("Background is set - should be Green" );
103+ setTa .setBackground (Color .green );
104+ setTa .setEditable (true );
105+ frame .add (setTa );
106+
107+ enableButton .addActionListener (e -> {
108+ boolean currentlyEditable = tf .isEditable ();
109+
110+ if (currentlyEditable ) {
111+ tf .setEditable (false );
112+ ta .setEditable (false );
113+ setTf .setEditable (false );
114+ setTa .setEditable (false );
115+ enableButton .setLabel (enableString );
116+ } else {
117+ tf .setEditable (true );
118+ ta .setEditable (true );
119+ setTf .setEditable (true );
120+ setTa .setEditable (true );
121+ enableButton .setLabel (disableString );
122+ }
123+ });
124+ frame .add (enableButton );
125+ return frame ;
126+ }
127+ }
0 commit comments