Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/java.desktop/share/classes/java/awt/TextArea.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -606,6 +606,15 @@ public Dimension minimumSize() {
}
}

@Override
public synchronized void setEditable(boolean b) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setEditable method is overridden in both TextArea and TextField class and method implementation is same. Since both of them are inherited from TextComponent class, could we move the code to TextComponent's setEditable method and remove them from respective classes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good suggestion.

super.setEditable(b);
Color defaultBackground = this.getBackground();
if (!backgroundSetByClientCode) {
setBackground(defaultBackground, false);
}
}

/**
* Returns a string representing the state of this {@code TextArea}.
* This method is intended to be used only for debugging purposes, and the
Expand Down
7 changes: 6 additions & 1 deletion src/java.desktop/share/classes/java/awt/TextComponent.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -359,6 +359,11 @@ public void setBackground(Color c) {
super.setBackground(c);
}

void setBackground(Color c, boolean setByClient) {
backgroundSetByClientCode = setByClient;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assigning the passed value to backgroundSetByClientCode does not make sense to me: the code in TextArea.setEditable already depends on the value of backgroundSetByClientCode, and the field shouldn't change as the result.

super.setBackground(c);
}

Comment on lines +362 to +366
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need a method need a javadoc comment block like the setBackground method before it or like any of the methods in the class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this method isn't a public or protected method (it's package-private, so it can't be accessed by users) there's no need for a javadoc comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. OK I'll wait for you to move the test to the AWT directory then because I do agree, the components involved seem to be AWT rather than Swing ones.

/**
* Gets the start position of the selected text in
* this text component.
Expand Down
11 changes: 10 additions & 1 deletion src/java.desktop/share/classes/java/awt/TextField.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -488,6 +488,15 @@ public Dimension minimumSize() {
}
}

@Override
public synchronized void setEditable(boolean b) {
super.setEditable(b);
Color defaultBackground = this.getBackground();
if (!backgroundSetByClientCode) {
setBackground(defaultBackground, false);
}
}

/**
* Adds the specified action listener to receive
* action events from this text field.
Expand Down
136 changes: 136 additions & 0 deletions test/jdk/java/awt/TextComponent/BackgroundTest.java
Copy link
Contributor

@kumarabhi006 kumarabhi006 Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ Ran the test on mac machine and I observed no changes with or without your fix. And the text on first textfield seems selected and due to that it is of different color. Is it possible to make them unselected at test start up, so that the background color can be verified for enabled and disabled case easily?

Attached the screenshot for test ui.

Test_UI_On_StartUp
Test_UI_On_StartUp

Test_UI_After_Click_DisableText_Button
Test_UI_After_Clicking_Disable

Test_UI_After_Click_EnableText_Button
Test_UI_After_Click_Enable

Original file line number Diff line number Diff line change
@@ -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;
}
}