undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,74 @@
/*
* Copyright (c) 2002, 2025, 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.
*/
/*
* @test
* @bug 4737679 4623376 4501485 4740906 4708221
* @requires (os.family == "windows")
* @summary Alt+Left/right/up/down generate characters in JTextArea
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual AltPlusNumberKeyCombinationsTest
*/
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
public class AltPlusNumberKeyCombinationsTest {
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
[WINDOWS PLATFORM ONLY]
Please do the following steps for both TextField and TextArea:
1. Hold down ALT and press a NON-NUMPAD right arrow, then release
ALT key. If any symbol is typed the test failed.
2. Hold down ALT and press one after another the following
NUMPAD keys: 0, 1, 2, 8. Release ALT key. If the Euro symbol
is not typed the test failed
3. Hold down ALT and press one after another the following
NUMPAD keys: 0, 2, 2, 7. Release ALT key. If nothing or
the blank symbol is typed the test failed
If all the steps are done successfully the test PASSed,
else test FAILS.
""";
PassFailJFrame.builder()
.title("Test Instructions")
.instructions(INSTRUCTIONS)
.columns(35)
.testUI(initialize())
.build()
.awaitAndCheck();
}
public static Frame initialize() {
Frame f = new Frame("key combination test");
f.setLayout(new FlowLayout());
TextField tf = new TextField("TextField");
f.add(tf);
TextArea ta = new TextArea("TextArea");
f.add(ta);
f.setSize(200, 200);
return f;
}
}

View file

@ -0,0 +1,127 @@
/*
* Copyright (c) 1999, 2025, 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.
*/
/*
* @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.
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual BackgroundTest
*/
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
public class BackgroundTest {
private static final String enableString = "EnableText";
private static final String disableString = "DisableText";
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
1. When the frame appears, it should have a blue background.
2. The first TextField and TextArea will be the default color.
The second TextField and TextArea will be green.
3. Press the "DisableText" button.
The first TextField and TextArea should change colors to the
default disabled color. On Windows, this is usually gray.
On linux and macos it will match the environment settings.
If the TextField or the TextArea do not change colors as described,
the test FAILS.
4. 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.
5. The second TextField and TextArea should still be green.
If either of them are not green, the test FAILS.
Otherwise, the test PASSES.
""";
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(35)
.testUI(BackgroundTest::initialize)
.build()
.awaitAndCheck();
}
public static Frame initialize() {
Frame frame = new Frame("Background Test");
frame.setLayout(new FlowLayout());
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(disableString);
enableButton.setBackground(Color.red);
frame.setSize(500, 250);
frame.setBackground(Color.blue);
tf.setText("Background not set - should be default");
tf.setEditable(true);
frame.add(tf);
ta.setText("Background not set - should be default");
ta.setEditable(true);
frame.add(ta);
setTf.setText("Background is set - should be Green");
setTf.setBackground(Color.green);
setTf.setEditable(true);
frame.add(setTf);
setTa.setText("Background is set - should be Green");
setTa.setBackground(Color.green);
setTa.setEditable(true);
frame.add(setTa);
enableButton.addActionListener(e -> {
boolean currentlyEditable = tf.isEditable();
if (currentlyEditable) {
tf.setEditable(false);
ta.setEditable(false);
setTf.setEditable(false);
setTa.setEditable(false);
enableButton.setLabel(enableString);
} else {
tf.setEditable(true);
ta.setEditable(true);
setTf.setEditable(true);
setTa.setEditable(true);
enableButton.setLabel(disableString);
}
});
frame.add(enableButton);
return frame;
}
}

View file

@ -0,0 +1,60 @@
/*
* Copyright (c) 2005, 2023, 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.
*/
/*
@test
@bug 5100200
@summary IAE in X11 text field peer code
@key headful
*/
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.TextField;
public class CorrectSetCaretPositionDuringInitPeerTest
{
static TextField tf;
static Frame frame;
public static void main(String[] args) throws Exception {
try{
EventQueue.invokeAndWait(() -> {
frame = new Frame("Caret Position test");
tf = new TextField("Very very very long string");
tf.setSelectionStart(10);
tf.setText("Short"); // now TextField.length() less than 10
frame.add(tf);
frame.pack();
frame.setVisible(true);
});
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
}

View file

@ -0,0 +1,122 @@
/*
* Copyright (c) 2005, 2026, 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.
*/
/*
* @test
* @key headful
* @bug 5100806
* @summary TextArea.select(0,0) does not de-select the selected text properly
* @run main CorrectTextComponentSelectionTest
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Robot;
import java.awt.TextArea;
import java.awt.TextComponent;
import java.awt.TextField;
public class CorrectTextComponentSelectionTest {
static TextField tf = new TextField("TextField");
static TextArea ta = new TextArea("TextArea");
static Robot r;
static Frame frame;
static volatile Color color_center;
public static void main(String[] args) throws Exception {
try {
r = new Robot();
EventQueue.invokeAndWait(() -> initialize());
r.waitForIdle();
r.delay(1000);
test(tf);
test(ta);
System.out.println("Test Passed!");
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
public static void initialize() {
frame = new Frame("TextComponent Selection Test");
frame.setLayout(new BorderLayout());
// We should place to the text components the long strings in order to
// cover the components by the selection completely
tf.setText(" ".repeat(50));
// We check the color of the text component in order to find out the
// bug reproducible situation
tf.setForeground(Color.WHITE);
tf.setBackground(Color.WHITE);
ta.setText((" ".repeat(50) + "\n").repeat(50));
ta.setForeground(Color.WHITE);
ta.setBackground(Color.WHITE);
frame.add(tf, "North");
frame.add(ta, "South");
frame.setSize(200, 200);
frame.setVisible(true);
}
private static void test(TextComponent tc) throws Exception {
if (tc instanceof TextField) {
System.out.println("TextField testing ...");
} else if (tc instanceof TextArea) {
System.out.println("TextArea testing ...");
}
r.waitForIdle();
r.delay(100);
EventQueue.invokeAndWait(() -> {
tc.requestFocus();
tc.selectAll();
tc.select(0, 0);
});
r.waitForIdle();
r.delay(100);
EventQueue.invokeAndWait(() -> {
Point p = tc.getLocationOnScreen();
p.translate(tc.getWidth() / 2, tc.getHeight() / 2);
color_center = r.getPixelColor(p.x, p.y);
});
System.out.println("Color of the text component (CENTER) = " + color_center);
System.out.println("White color = " + Color.WHITE);
if (color_center.getRGB() != Color.WHITE.getRGB()) {
throw new RuntimeException("Test Failed");
}
}
}

View file

@ -0,0 +1,105 @@
/*
* Copyright (c) 2005, 2023, 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.
*/
/*
@test
@bug 5100950 8199723
@summary textarea.getSelectedText() returns the de-selected text, on XToolkit
@key headful
@requires (os.family == "mac") | (os.family == "windows")
*/
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.KeyboardFocusManager;
import java.awt.TextField;
public class DeselectionDuringDoSelectionNonVisibleTest {
static TextField tf1;
static TextField tf2;
static Frame frame;
public static void main(String[] args) throws Exception {
try {
EventQueue.invokeAndWait(() -> {
frame = new Frame("Deselection test");
tf1 = new TextField("Text Field 1");
tf2 = new TextField("Text Field 2");
frame.add(tf1);
frame.add(tf2);
frame.setLayout(new FlowLayout());
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
});
boolean isWin = System.getProperty("os.name").startsWith("Win");
System.out.println("is Windows OS? " + isWin);
Thread.sleep(500);
tf1.requestFocus();
Thread.sleep(500);
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != tf1) {
throw new RuntimeException("Test failed (TextField1 isn't focus owner).");
}
tf1.selectAll();
Thread.sleep(500);
tf2.requestFocus();
Thread.sleep(500);
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != tf2) {
throw new RuntimeException("Test failed (TextField2 isn't focus owner).");
}
tf2.selectAll();
Thread.sleep(500);
String selectedText = tf1.getSelectedText();
String text = tf1.getText();
System.out.println("tf1.getText()=" + text);
System.out.println("tf1.getSelectedText()=" + selectedText);
// Motif behaviour: After the selection of the second text, the first selected
// text is unselected
if (!selectedText.equals("") && !isWin) {
throw new RuntimeException("Test failed (TextField1 isn't deselected).");
}
// Windows behaviour: After the selection of the second text, the first selected
// text is only not highlighted
if (!selectedText.equals(text) && isWin) {
throw new RuntimeException("Test failed (TextField1 is deselected).");
}
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
}

View file

@ -0,0 +1,98 @@
/*
* Copyright (c) 2005, 2025, 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.
*/
/*
* @test
* @bug 5042122
* @summary Verifies the TextComponent is grayed out when disabled
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual DisableTest
*/
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.Iterator;
public class DisableTest {
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
1. Click "Enable" and "Disable" buttons and verify the text
components are disabled and enabled correctly.
2. Verify that the disabled text components are grayed
out and are uneditable.
3. Click PASS or FAIL accordingly.
""";
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(35)
.testUI(DisableTest::initialize)
.build()
.awaitAndCheck();
}
public static Frame initialize() {
Frame frame = new Frame("TextComponent Disabled test");
frame.setLayout(new BorderLayout());
frame.setSize(200, 200);
final Vector comps = new Vector();
comps.add(new TextField("TextField"));
TextArea ta = new TextArea("TextArea", 2, 100, TextArea.SCROLLBARS_NONE);
comps.add(ta);
Panel pc = new Panel();
pc.setLayout(new BoxLayout(pc, BoxLayout.Y_AXIS));
Iterator iter = comps.iterator();
while (iter.hasNext()) {
Component c = (Component) iter.next();
c.setEnabled(false);
pc.add(c);
}
frame.add(pc, BorderLayout.CENTER);
Panel p = new Panel();
final Button be = new Button("Enable");
final Button bd = new Button("Disable");
p.add(be);
p.add(bd);
ActionListener al = ev -> {
boolean enable = (ev.getSource() == be);
Iterator iterator = comps.iterator();
while (iterator.hasNext()) {
Component c = (Component) iterator.next();
c.setEnabled(enable);
}
};
be.addActionListener(al);
bd.addActionListener(al);
frame.add(p, BorderLayout.SOUTH);
return frame;
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2006, 2023, 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.
*/
/*
@test
@bug 4995931
@summary java.awt.TextComponent caret position should be within the text bounds
@key headful
*/
import java.awt.EventQueue;
import java.awt.TextField;
public class GetCaretPosOutOfBoundsTest {
static TextField tf;
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(() -> {
tf = new TextField("1234567890");
tf.setCaretPosition(100);
int pos = tf.getCaretPosition();
if (pos > 10) {
throw new RuntimeException("Wrong caret position:" + pos + " instead of 10");
}
tf.setText("12345");
if (tf.getCaretPosition() > 5) {
throw new RuntimeException("Wrong caret position:" + pos + " instead of 5");
}
});
}
}

View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 1999, 2023, 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.
*/
/*
@test
@bug 4185654
@summary tests that the text insertion caret is positioned before the first character
@key headful
*/
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.TextArea;
public class InitialInsertionCaretPositionTest {
static TextField textField;
static TextArea textArea;
static String failureMessage = "";
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(() -> {
textArea = new TextArea("abcdefghij\nabcdefghij");
textField = new TextField("abcdefghij");
boolean textFieldPassed = (textField.getCaretPosition() == 0);
boolean textAreaPassed = (textArea.getCaretPosition() == 0);
if (!textFieldPassed) {
failureMessage += " The text insertion caret for the text field is not\n";
failureMessage += " initially set before the first character.\n";
}
if (!textAreaPassed) {
failureMessage += " The text insertion caret for the text area is not\n";
failureMessage += " initially set before the first character.\n";
}
if (textAreaPassed && textFieldPassed) {
System.out.println("The test passed.");
} else {
System.out.println("The test failed:");
System.out.println(failureMessage);
}
if (!textAreaPassed || !textFieldPassed) {
throw new RuntimeException(failureMessage);
}
});
}
}

View file

@ -0,0 +1,112 @@
/*
* Copyright (c) 2001, 2023, 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.
*/
/*
@test
@bug 4426750
@requires (os.family == "linux")
@key headful
@summary tests that middle mouse button click pastes primary selection
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Robot;
import java.awt.TextArea;
import java.awt.TextComponent;
import java.awt.TextField;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.InputEvent;
import java.awt.Toolkit;
public class MiddleMouseClickPasteTest {
static final int FRAME_ACTIVATION_TIMEOUT = 1000;
static final int SELECTION_PASTE_TIMEOUT = 1000;
static final int CLICK_INTERVAL = 50;
static final String TEST_TEXT = "TEST TEXT";
static Frame frame;
static TextField tf;
static TextArea ta;
static final Clipboard systemSelection = Toolkit.getDefaultToolkit().getSystemSelection();
public static void main(String[] args) throws Exception {
if (systemSelection != null) {
try {
EventQueue.invokeAndWait(MiddleMouseClickPasteTest::createAndShowGui);
Thread.sleep(FRAME_ACTIVATION_TIMEOUT);
checkPaste(tf);
checkPaste(ta);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
EventQueue.invokeAndWait(()-> {
if (frame != null) {
frame.dispose();
}
});
}
}
}
public static void createAndShowGui() {
frame = new Frame();
tf = new TextField();
ta = new TextArea();
frame.setLayout(new BorderLayout());
frame.add(tf, BorderLayout.NORTH);
frame.add(ta, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void checkPaste(TextComponent textComponent) throws Exception {
final Point sourcePoint = textComponent.getLocationOnScreen();
final Dimension d = textComponent.getSize();
sourcePoint.translate(d.width / 2, d.height / 2);
final Robot robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(CLICK_INTERVAL);
textComponent.setText("");
systemSelection.setContents(new StringSelection(TEST_TEXT), null);
robot.mouseMove(sourcePoint.x, sourcePoint.y);
robot.mousePress(InputEvent.BUTTON2_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
robot.delay(SELECTION_PASTE_TIMEOUT);
if (!TEST_TEXT.equals(textComponent.getText())) {
throw new RuntimeException("Primary selection not pasted" +
" into: " + textComponent);
}
}
}

View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 1999, 2025, 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.
*/
/*
* @test
* @bug 4035364
* @summary Checks that Caps Lock key works
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual ModifiersTest
*/
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class ModifiersTest {
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
1. Type some text in the TextArea in upper and lowercase,
using the Caps Lock ON/OFF.
2. If Caps Lock toggles correctly and you are able to type in
both cases, the test PASS. Else Test FAILS.
""";
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(35)
.testUI(ModifiersTest::initialize)
.build()
.awaitAndCheck();
}
public static Frame initialize() {
Frame frame = new Frame("Modifiers Test");
frame.setLayout(new GridLayout(1, 1));
frame.addKeyListener(new KeyChecker());
frame.setLayout(new GridLayout(2, 1));
Label label = new Label("See if you can type in upper and lowercase using Caps Lock:");
frame.add(label);
TextArea ta = new TextArea();
frame.add(ta);
ta.addKeyListener(new KeyChecker());
ta.requestFocus();
frame.setSize(400, 300);
return frame;
}
}
// a KeyListener for debugging purposes
class KeyChecker extends KeyAdapter {
public void keyPressed(KeyEvent ev) {
System.out.println(ev);
}
public void keyReleased(KeyEvent ev) {
System.out.println(ev);
}
public void keyTyped(KeyEvent ev) {
System.out.println(ev);
}
}

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2003, 2023, 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.
*/
/*
@test
@bug 4257143
@summary RFE: Cannot set some AWT properties until peer has been created
@key headful
*/
import java.awt.EventQueue;
import java.awt.TextArea;
import java.awt.TextField;
public class PeerlessSetCaret {
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(() -> {
TextField tf = new TextField("Hello, World!");
TextArea ta = new TextArea("Hello, World!");
// without the fix these will throw IllegalComponentStateException
tf.setCaretPosition(1);
ta.setCaretPosition(1);
});
}
}

View file

@ -0,0 +1,162 @@
/*
* Copyright (c) 2006, 2025, 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.
*/
/*
* @test
* @bug 6287895
* @requires (os.family == "linux")
* @summary Test cursor and selected text incorrectly colored in TextField
* @key headful
* @run main SelectionAndCaretColor
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.TextArea;
import java.awt.TextComponent;
import java.awt.TextField;
import java.awt.image.BufferedImage;
public class SelectionAndCaretColor {
static TextField tf = new TextField(20);
static TextArea ta = new TextArea("", 1, 20, TextArea.SCROLLBARS_NONE);
static Robot r;
static Frame frame;
static volatile int flips;
public static void main(String[] args) throws Exception {
try {
frame = new Frame("Selection and Caret color test");
r = new Robot();
EventQueue.invokeAndWait(() -> {
frame.setLayout(new BorderLayout());
tf.setFont(new Font("Monospaced", Font.PLAIN, 15));
ta.setFont(new Font("Monospaced", Font.PLAIN, 15));
frame.add(tf, BorderLayout.NORTH);
frame.add(ta, BorderLayout.SOUTH);
frame.setSize(200, 200);
frame.setVisible(true);
});
r.waitForIdle();
r.delay(1000);
test(tf);
test(ta);
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
private static int countFlips(TextComponent tc) {
int y = tc.getLocationOnScreen().y + tc.getHeight() / 2;
int x1 = tc.getLocationOnScreen().x + 5;
int x2 = tc.getLocationOnScreen().x + tc.getWidth() - 5;
int[] fb = {tc.getBackground().getRGB(), tc.getForeground().getRGB()};
int i = 0;
int flips = 0;
BufferedImage img = r.createScreenCapture(new Rectangle(x1, y, x2 - x1, 1));
for (int x = 0; x < x2 - x1; x++) {
int c = img.getRGB(x, 0);
if (c == fb[i]) {
;
} else if (c == fb[1 - i]) {
flips++;
i = 1 - i;
} else {
throw new RuntimeException("Invalid color detected: " +
Integer.toString(c, 16) + " instead of " +
Integer.toString(fb[i], 16));
}
}
return flips;
}
private static void test(TextComponent tc) throws Exception {
if (tc instanceof TextField) {
System.out.println("TextField testing ...");
} else if (tc instanceof TextArea) {
System.out.println("TextArea testing ...");
}
// now passing along the component's vertical center,
// skipping 5px from both sides,
// we should see bg - textcolor - bg - selcolor -
// seltextcolor - selcolor - bg
// that is bg-fg-bg-fg-bg-fg-bg, 6 flips
EventQueue.invokeAndWait(() -> {
tc.setForeground(Color.green);
tc.setBackground(Color.magenta);
tc.setText(" I I ");
tc.select(5, 10);
tc.requestFocus();
});
r.waitForIdle();
r.delay(200);
EventQueue.invokeAndWait(() -> {
flips = countFlips(tc);
});
if (flips != 6) {
throw new RuntimeException("Invalid number of flips: "
+ flips + " instead of 6");
}
EventQueue.invokeAndWait(() -> {
// same for caret: spaces in the tc, caret in the middle
// bg-fg-bg - 2 flips
tc.select(0, 0);
tc.setText(" ");
tc.setCaretPosition(5);
});
r.waitForIdle();
r.delay(200);
for (int i = 0; i < 10; i++) {
EventQueue.invokeAndWait(() -> {
flips = countFlips(tc);
});
if (flips == 2) {
break;
}
if (flips == 0) {
continue;
}
throw new RuntimeException("Invalid number of flips: "
+ flips + " instead of 2");
}
}
}

View file

@ -0,0 +1,107 @@
/*
* Copyright (c) 1999, 2023, 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.
*/
/*
@test
@bug 4118247
@summary Make sure bounds are enforced correctly on
TextComponent.Select(int, int)
@key headful
*/
import java.awt.EventQueue;
import java.awt.TextArea;
import java.awt.TextComponent;
public class SelectionBounds {
public static TextComponent tc;
public static int[][] index = {
{0, 0}, // 0 = selectionStart = selectionEnd
{5, 5}, // selectionStart = selectionEnd
{5, 7}, // 0 < selectionStart < selectionEnd < textLength
{-50, 7}, // selectionStart < 0 < selectionEnd < textLength
{-50, 50}, // selectionStart < 0 < textLength < selectionEnd
{5, 50}, // 0 < selectionStart < textLength < selectionEnd
{40, 50}, // 0 < textLength < selectionStart < selectionEnd
{-50, -40}, // selectionStart < selectionEnd < 0 < textLength
{7, 5}, // 0 < selectionEnd < selectionStart < textLength
{7, -50}, // selectionEnd < 0 < selectionStart < textLength
{50, -50}, // selectionEnd < 0 < textLength < selectionStart
{50, 5}, // 0 < selectionEnd < textLength < selectionStart
{50, 40}, // 0 < textLength < selectionEnd < selectionStart
{-40, -50} // selectionEnd < selectionStart < 0 < textLength
};
public static String[] selections = {
"",
"",
"56",
"0123456",
"0123456789",
"56789",
"",
"",
"",
"",
"",
"",
"",
""
};
public static void main(String[] args) throws Exception {
EventQueue.invokeAndWait(() -> {
tc = new TextArea("0123456789");
runTheTest();
});
}
private static void runTheTest() {
int i;
String str1;
for (i=0; i<index.length; i++) {
tc.select(index[i][0], index[i][1]);
str1 = tc.getSelectedText();
if (!str1.equals(selections[i])) {
System.out.println("Test " + i + " FAILED: " + str1 +
" != " + selections[i]);
System.out.println("Test " + i + " FAILED: " + str1 +
" != " + selections[i]);
throw new RuntimeException("Test " + i + " FAILED: " + str1 +
" != " + selections[i]);
}
else {
System.out.println("Test " + i + " PASSED: " + str1 +
" = " + selections[i]);
System.out.println("Test " + i + " PASSED: " + str1 +
" = " + selections[i]);
}
}
System.out.println("\nAll tests PASSED.");
}
}

View file

@ -0,0 +1,73 @@
/*
* Copyright (c) 1998, 2025, 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.
*/
/*
* @test
* @bug 4056231
* @summary Checks that TextComponents don't grab the global CDE selection
* upon construction if their own selection is null.
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual SelectionTest
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SelectionTest {
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
1. "Select some text in another window, then click the button.",
2. "If the text in the other window is de-selected, the test FAILS.",
"If the text remains selected, the test PASSES."
""";
PassFailJFrame.builder()
.title("Test Instructions")
.instructions(INSTRUCTIONS)
.columns(35)
.testUI(initialize())
.build()
.awaitAndCheck();
}
public static Frame initialize() {
Frame frame = new Frame("Selection Test");
frame.setLayout(new BorderLayout());
Button b = new Button("Select some text in another window, then" +
" click me");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.add(new TextField("text field test"));
frame.add(new TextArea("text area test"));
}
});
frame.add(b);
frame.setSize(400, 400);
return frame;
}
}

View file

@ -0,0 +1,107 @@
/*
* Copyright (c) 2020, 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.Frame;
import java.awt.TextArea;
import java.awt.TextComponent;
import java.awt.TextField;
/**
* @test
* @key headful
* @bug 6278172
* @summary Tests that TextComponent#setText() preserves the selection
*/
public final class SetTextSelection {
private static final String LONG_TEXT = "text field";
private static final String SHORT_TEXT = "text";
public static void main(String[] args) {
testNoFrame(true);
testNoFrame(false);
for (int i = 0; i < 5; i++) {
testFrame(true, i);
testFrame(false, i);
}
testDisposedFrame(true);
testDisposedFrame(false);
}
private static void testNoFrame(boolean field) {
TextComponent tf = field ? new TextField(LONG_TEXT) :
new TextArea(LONG_TEXT);
tf.selectAll();
tf.setText(SHORT_TEXT);
test(tf);
}
private static void testDisposedFrame(boolean field) {
Frame frame = new Frame();
try {
TextComponent tf = field ? new TextField(LONG_TEXT) :
new TextArea(LONG_TEXT);
frame.add(tf);
frame.pack();
tf.selectAll();
frame.dispose();
tf.setText(SHORT_TEXT);
test(tf);
} finally {
frame.dispose();
}
}
private static void testFrame(boolean field, int step) {
Frame frame = new Frame();
try {
TextComponent tf = field ? new TextField(LONG_TEXT) :
new TextArea(LONG_TEXT);
if (step == 1) {
frame.pack();
}
frame.add(tf);
if (step == 2) {
frame.pack();
}
tf.selectAll();
if (step == 3) {
frame.pack();
}
tf.setText(SHORT_TEXT);
if (step == 4) {
frame.pack();
}
test(tf);
} finally {
frame.dispose();
}
}
private static void test(TextComponent tf) {
String str = tf.getSelectedText();
if (!str.equals(SHORT_TEXT)) {
throw new RuntimeException(str);
}
}
}

View file

@ -0,0 +1,150 @@
/*
* Copyright (c) 2002, 2023, 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.
*/
/*
@test
@bug 4701398 4652358 4659958 4697796 4666876
@summary REGRESSION: TextArea.append does not work consistently with \r.
@key headful
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
public class TextAreaCRLFTest {
private static final char[] DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
public static Dialog aDialog;
public static TextArea area;
public static boolean passed = true;
public static boolean res;
public static String atext = "";
public static void main(String[] args) throws Exception {
String atextCRLF = "row1\r\nrow2\r\nrow3";
try {
EventQueue.invokeAndWait(() -> {
aDialog = new Dialog(new Frame());
aDialog.setTitle("ADialog");
aDialog.setBackground(Color.lightGray);
aDialog.setLayout(new BorderLayout());
Panel mainPanel = new Panel();
mainPanel.setLayout(new BorderLayout(6, 6));
area = new TextArea(atextCRLF, 25, 68,
TextArea.SCROLLBARS_VERTICAL_ONLY);
area.setFont(new Font("Monospaced", Font.PLAIN, 11));
mainPanel.add(area, "Center");
aDialog.add(mainPanel, "Center");
aDialog.pack();
System.out.println("before: "+hexEncode(atextCRLF));
System.out.println(" after: "+hexEncode(area.getText()));
res = area.getText().equals(atextCRLF);
System.out.println("01: " + res + "\n");
passed = passed && res;
area.setText(atextCRLF);
System.out.println("before: "+hexEncode(atextCRLF));
System.out.println(" after: "+hexEncode(area.getText()));
res = area.getText().equals(atextCRLF);
System.out.println("02: " + res + "\n");
passed = passed && res;
area.setText("");
atext = "row1";
area.append(atext+"\r");
area.append(atext+"\r");
System.out.println("before: "
+hexEncode(atext+"\r" + atext+"\r"));
System.out.println(" after: "+hexEncode(area.getText()));
res = area.getText().equals(atext + atext);
System.out.println("03: " + res + "\n");
passed = passed && res;
area.setText("");
String atext1 = "fine.";
String atext2 = "messed up.";
atext = atext1 +"\r\n"+ atext2;
for (int i = 0; i < atext.length(); i++) {
area.append(atext.substring(i, i+1));
}
System.out.println("before: "
+hexEncode(atext1 +"\r\n"+ atext2));
System.out.println(" after: "+hexEncode(area.getText()));
String s = area.getText();
String t = s.substring(s.length()-atext2.length());
res = t.equals(atext2);
System.out.println("04: " + res);
passed = passed && res;
area.setText("");
atext = "\r";
area.append(atext);
System.out.println("before: "+hexEncode(atext));
System.out.println(" after: "+hexEncode(area.getText()));
res = area.getText().equals("");
System.out.println("05: " + res + "\n");
passed = passed && res;
if (System.getProperty("os.name").toUpperCase().
startsWith("WIN")) {
if (!passed) {
throw new RuntimeException("TextAreaCRLFTest FAILED.");
} else {
System.out.println("TextAreaCRLFTest PASSED");
}
} else {
System.out.println("This is a Windows oriented testcase.");
}
});
} finally {
EventQueue.invokeAndWait(() -> {
if (aDialog != null) {
aDialog.dispose();
}
});
}
}
private static String hexEncode(String str) {
return hexEncode(str.getBytes());
}
private static String hexEncode(byte[] bytes) {
StringBuffer buffer = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++) {
byte b = bytes[i];
buffer.append(DIGITS[(b & 0xF0) >> 4]);
buffer.append(DIGITS[b & 0x0F]);
}
return buffer.toString();
}
}

View file

@ -0,0 +1,67 @@
/*
* Copyright (c) 1999, 2025, 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.
*/
/*
* @test
* @bug 4129511
* @summary Tests that TextField margins are not exceedingly wide
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual TextFieldMargin
*/
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
public class TextFieldMargin {
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
1. Examine the TextField, Label, and TextArea to see
that the text is vertically aligned along the left
2. If all are aligned along the left, then test PASS,
else test FAILS.
""";
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(35)
.testUI(TextFieldMargin::initialize)
.build()
.awaitAndCheck();
}
public static Frame initialize() {
Frame frame = new Frame("Frame with a text field & a label");
frame.setLayout(new GridLayout(5, 1));
TextField text_field = new TextField("Left Textfield");
frame.add(text_field);
Label label = new Label("Left Label");
frame.add(label);
TextArea text_area = new TextArea("Left Textfield");
frame.add(text_area);
frame.setBounds(50, 50, 300, 300);
return frame;
}
}