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,65 @@
/*
* Copyright (c) 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.
*/
import java.awt.Insets;
import java.awt.Rectangle;
import com.sun.java.swing.SwingUtilities3;
/*
* @test
* @bug 8365379
* @summary Verify SwingUtilities3 insets return correct result
* independent of initial values
* @modules java.desktop/com.sun.java.swing
* @run main ApplyInsetsTest
*/
public class ApplyInsetsTest {
public static void main(String[] args) {
Rectangle rect = new Rectangle(10, 20, 60, 60);
Insets insets = new Insets(5, 10, 15, 20);
Rectangle expected =
new Rectangle(rect.x + insets.left,
rect.y + insets.top,
rect.width - (insets.left + insets.right),
rect.height - (insets.top + insets.bottom));
SwingUtilities3.applyInsets(rect, insets);
if (!rect.equals(expected)) {
throw new RuntimeException("Test failed: expected " + expected +
" but got " + rect);
}
// Right to left test
rect.setRect(10, 20, 60, 60);
expected.x = rect.x + insets.right;
SwingUtilities3.applyInsets(rect, insets, false);
if (!rect.equals(expected)) {
throw new RuntimeException("Right to left test failed: expected "
+ expected + " but got " + rect);
}
System.out.println("Test passed.");
}
}

View file

@ -0,0 +1,2 @@
modules=java.desktop

View file

@ -0,0 +1,168 @@
/*
* Copyright (c) 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 8367376
* @key headful
* @summary DesktopProperty never reset pending status to process new updates
* @modules java.desktop/sun.swing.plaf
* @run main DesktopPropertyResetPendingFlagTest
*/
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicButtonUI;
import sun.swing.plaf.DesktopProperty;
import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
public class DesktopPropertyResetPendingFlagTest extends JFrame {
static class ExpectedException extends RuntimeException {}
/**
* The original ticket required changing the system accessibility settings.
* But we can instead automate this test if we just create & update our own
* DesktopProperty.
*/
static class TestDesktopProperty extends DesktopProperty {
public TestDesktopProperty(String key, Object fallback) {
super(key, fallback);
}
@Override
public void updateUI() {
super.updateUI();
}
}
private static void assertEquals(int expectedValue, int actualValue) {
String msg = "expected " + expectedValue +
" observed " + actualValue;
if (expectedValue != actualValue) {
throw new RuntimeException(msg);
}
System.out.println(msg);
}
public static void main(String[] args) throws Exception {
// we only override this to intercept ExpectedExceptions
EventQueue newEventQueue = new EventQueue() {
@Override
protected void dispatchEvent(AWTEvent event) {
try {
super.dispatchEvent(event);
} catch (ExpectedException e) {
// This is part of the test. But if we don't catch
// this here the test harness says our test failed.
observedExpectedExceptionCounter++;
}
}
};
Toolkit.getDefaultToolkit().getSystemEventQueue().push(newEventQueue);
for (UIManager.LookAndFeelInfo info :
UIManager.getInstalledLookAndFeels()) {
System.out.println("Setting L&F: " + info.getClassName());
UIManager.setLookAndFeel(info.getClassName());
runTest();
}
}
private static void runTest() throws Exception {
AtomicReference<JFrame> frameRef = new AtomicReference<>();
SwingUtilities.invokeLater(() -> {
DesktopPropertyResetPendingFlagTest t =
new DesktopPropertyResetPendingFlagTest();
t.pack();
t.setVisible(true);
frameRef.set(t);
});
try {
panelUpdateUICounter = 0;
observedExpectedExceptionCounter = 0;
TestDesktopProperty property =
new TestDesktopProperty("test", new Object());
SwingUtilities.invokeLater(property::updateUI);
SwingUtilities.invokeAndWait(() -> {
});
SwingUtilities.invokeLater(property::updateUI);
SwingUtilities.invokeAndWait(() -> {
});
CountDownLatch keepAliveLatch = new CountDownLatch(1);
SwingUtilities.invokeLater(() -> {
// We expect 3 updateUI invocations: during construction, the first
// property.updateUI, & the second property.updateUI
assertEquals(3, panelUpdateUICounter);
// We expect 2 attempts on buttonUI.uninstallUI
assertEquals(2, observedExpectedExceptionCounter);
// The test is finished
keepAliveLatch.countDown();
});
keepAliveLatch.await();
} finally {
SwingUtilities.invokeAndWait(() -> frameRef.get().dispose());
}
}
static int panelUpdateUICounter;
static int observedExpectedExceptionCounter;
DesktopPropertyResetPendingFlagTest() {
JButton button = new JButton("button");
button.setUI(new BasicButtonUI() {
@Override
public void uninstallUI(JComponent c) {
throw new ExpectedException();
}
});
JPanel p = new JPanel() {
@Override
public void updateUI() {
panelUpdateUICounter++;
}
};
p.add(button);
getContentPane().add(p);
}
}

View file

@ -0,0 +1,245 @@
/*
* Copyright (c) 2013, 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 4928019
* @key headful
* @summary Makes sure all the basic classes can be created with GTK.
* @requires (os.family != "windows" & os.family != "mac")
* @library /test/lib
* @build jtreg.SkippedException
* @run main bug4928019
*/
import javax.swing.*;
import javax.swing.plaf.basic.*;
import jtreg.SkippedException;
public class bug4928019 {
public static void main(String[] args) throws Throwable {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception e) {
throw new SkippedException("GTKLookAndFeel isn't supported", e);
}
new JButton() {
public void updateUI() {
setUI(new BasicButtonUI());
}
};
new JCheckBox() {
public void updateUI() {
setUI(new BasicCheckBoxUI());
}
};
new JCheckBoxMenuItem() {
public void updateUI() {
setUI(new BasicCheckBoxMenuItemUI());
}
};
new JColorChooser() {
public void updateUI() {
setUI(new BasicColorChooserUI());
}
};
new JComboBox() {
public void updateUI() {
setUI(new BasicComboBoxUI());
}
};
new JDesktopPane() {
public void updateUI() {
setUI(new BasicDesktopPaneUI());
}
};
new JEditorPane() {
public void updateUI() {
setUI(new BasicEditorPaneUI());
}
};
new JFileChooser() {
public void updateUI() {
setUI(new BasicFileChooserUI(null));
}
};
new JFormattedTextField() {
public void updateUI() {
setUI(new BasicFormattedTextFieldUI());
}
};
new JInternalFrame() {
public void updateUI() {
setUI(new BasicInternalFrameUI(null));
}
};
new JLabel() {
public void updateUI() {
setUI(new BasicLabelUI());
}
};
new JList() {
public void updateUI() {
setUI(new BasicListUI());
}
};
new JMenuBar() {
public void updateUI() {
setUI(new BasicMenuBarUI());
}
};
new JMenuItem() {
public void updateUI() {
setUI(new BasicMenuItemUI());
}
};
new JMenu() {
public void updateUI() {
setUI(new BasicMenuUI());
}
};
new JOptionPane() {
public void updateUI() {
setUI(new BasicOptionPaneUI());
}
};
new JPanel() {
public void updateUI() {
setUI(new BasicPanelUI());
}
};
new JPasswordField() {
public void updateUI() {
setUI(new BasicPasswordFieldUI());
}
};
new JPopupMenu() {
public void updateUI() {
setUI(new BasicPopupMenuUI());
}
};
new JProgressBar() {
public void updateUI() {
setUI(new BasicProgressBarUI());
}
};
new JRadioButton() {
public void updateUI() {
setUI(new BasicRadioButtonUI());
}
};
new JRadioButtonMenuItem() {
public void updateUI() {
setUI(new BasicRadioButtonMenuItemUI());
}
};
new JRootPane() {
public void updateUI() {
setUI(new BasicRootPaneUI());
}
};
new JScrollBar() {
public void updateUI() {
setUI(new BasicScrollBarUI());
}
};
new JScrollPane() {
public void updateUI() {
setUI(new BasicScrollPaneUI());
}
};
new JSeparator() {
public void updateUI() {
setUI(new BasicSeparatorUI());
}
};
new JSlider() {
public void updateUI() {
setUI(new BasicSliderUI(null));
}
};
new JSpinner() {
public void updateUI() {
setUI(new BasicSpinnerUI());
}
};
new JSplitPane() {
public void updateUI() {
setUI(new BasicSplitPaneUI());
}
};
new JTabbedPane() {
public void updateUI() {
setUI(new BasicTabbedPaneUI());
}
};
new JTable() {
public void updateUI() {
setUI(new BasicTableUI());
}
};
new JTextArea() {
public void updateUI() {
setUI(new BasicTextAreaUI());
}
};
new JTextField() {
public void updateUI() {
setUI(new BasicTextFieldUI());
}
};
new JTextPane() {
public void updateUI() {
setUI(new BasicTextPaneUI());
}
};
new JToggleButton() {
public void updateUI() {
setUI(new BasicToggleButtonUI());
}
};
new JToolBar() {
public void updateUI() {
setUI(new BasicToolBarUI());
}
};
new JToolTip() {
public void updateUI() {
setUI(new BasicToolTipUI());
}
};
new JTree() {
public void updateUI() {
setUI(new BasicTreeUI());
}
};
new JViewport() {
public void updateUI() {
setUI(new BasicViewportUI());
}
};
System.out.println("DONE");
}
}

View file

@ -0,0 +1,80 @@
/*
* Copyright (c) 2008, 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 6635110
* @key headful
* @summary GTK icons should not throw NPE when called by non-GTK UI
* @requires (os.family != "windows" & os.family != "mac")
* @library /test/lib
* @build jtreg.SkippedException
* @run main Test6635110
*/
import java.awt.Component;
import java.awt.image.BufferedImage;
import javax.swing.JMenu;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicMenuUI;
import javax.swing.plaf.basic.BasicToolBarUI;
import jtreg.SkippedException;
public class Test6635110 implements Runnable {
static final int WIDTH = 160;
static final int HEIGHT = 80;
final BufferedImage IMAGE =
new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
@Override public void run() {
JMenu menu = new JMenu("menu");
menu.setUI(new BasicMenuUI());
paint(menu);
JToolBar tb = new JToolBar();
tb.setFloatable(true);
tb.setUI(new BasicToolBarUI());
paint(tb);
}
private void paint(Component c) {
c.setSize(WIDTH, HEIGHT);
c.paint(IMAGE.getGraphics());
}
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception e) {
throw new SkippedException("GTKLookAndFeel isn't supported", e);
}
SwingUtilities.invokeAndWait(new Test6635110());
}
}

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2010, 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 6963870
* @key headful
* @summary Tests that GTKPainter.ListTableFocusBorder.getBorderInsets()
* doesn't return null
* @requires (os.family != "windows" & os.family != "mac")
* @library /test/lib
* @build jtreg.SkippedException
* @run main Test6963870
*/
import java.awt.Insets;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import jtreg.SkippedException;
public class Test6963870 implements Runnable {
static final String[] UI_NAMES = {
"List.focusCellHighlightBorder",
"List.focusSelectedCellHighlightBorder",
"List.noFocusBorder",
"Table.focusCellHighlightBorder",
"Table.focusSelectedCellHighlightBorder",
};
@Override
public void run() {
for (String uiName: UI_NAMES) {
test(uiName);
}
}
void test(String uiName) {
Border b = UIManager.getBorder(uiName);
Insets i = b.getBorderInsets(null);
if (i == null) {
throw new RuntimeException("getBorderInsets() returns null for " + uiName);
}
}
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception e) {
throw new SkippedException("GTKLookAndFeel isn't supported", e);
}
SwingUtilities.invokeAndWait(new Test6963870());
}
}

View file

@ -0,0 +1,180 @@
/*
* Copyright (c) 2022, 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 8078471
* @key headful
* @requires (os.family == "linux")
* @summary Verifies if filechooser current directory changed to parent
* directory on BACKSPACE key press except root directory.
* @run main TestBackSpaceAction
*/
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.KeyEvent;
import java.awt.Robot;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestBackSpaceAction {
private static JFrame frame;
private static JFileChooser fileChooser;
private static Robot robot;
private static File testDir;
private static File subDir;
private static File prevDir;
private static File crntDir;
public static void main(String[] args) throws Exception {
robot = new Robot();
robot.setAutoDelay(100);
try {
// create test directory
String tmpDir = System.getProperty("java.io.tmpdir");
//'java.io.tmpdir' isn't guaranteed to be defined
if (tmpDir.length() == 0) {
tmpDir = System.getProperty("user.home");
}
testDir = new File(tmpDir, "testDir");
testDir.mkdir();
testDir.deleteOnExit();
// create sub directory inside test directory
subDir = new File(testDir, "subDir");
subDir.mkdir();
subDir.deleteOnExit();
} catch (Exception e) {
e.printStackTrace();
}
for (UIManager.LookAndFeelInfo laf :
UIManager.getInstalledLookAndFeels()) {
if (!laf.getClassName().contains("MotifLookAndFeel")) {
System.out.println("Testing LAF: " + laf.getClassName());
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
doTesting(laf);
}
}
}
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
try {
UIManager.setLookAndFeel(laf.getClassName());
} catch (UnsupportedLookAndFeelException ignored) {
System.out.println("Unsupported LAF: " + laf.getClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static void doTesting(UIManager.LookAndFeelInfo laf)
throws Exception {
try {
SwingUtilities.invokeAndWait(() -> {
createAndShowUI();
});
boolean passed_1 = false;
boolean passed_2 = false;
robot.waitForIdle();
robot.delay(100);
// check backspace key at subDir level
clickBackSpace();
if (prevDir != crntDir) {
passed_1 = true;
}
// check if backspace key changes directory at root level
while (!fileChooser.getFileSystemView().isFileSystemRoot(prevDir)) {
clickBackSpace();
if (prevDir == crntDir) {
passed_2 = true;
break;
}
}
if (passed_1 && passed_2) {
System.out.println("Passed");
} else {
throw new RuntimeException("BackSpace does not lead to " +
"parent directory for LAF: " + laf.getClassName());
}
} finally {
SwingUtilities.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
private static void createAndShowUI() {
frame = new JFrame("Test File Chooser Backspace Action");
frame.getContentPane().setLayout(new BorderLayout());
fileChooser = new JFileChooser(subDir);
fileChooser.setControlButtonsAreShown(false);
fileChooser.addHierarchyListener(new HierarchyListener(){
public void hierarchyChanged(HierarchyEvent he) {
grabFocusForComboBox(fileChooser.getComponents());
}
});
frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static void grabFocusForComboBox(Component[] comp)
{
for (Component c:comp) {
if (c instanceof JComboBox) {
JComboBox cb = (JComboBox)c;
cb.requestFocusInWindow();
break;
} else if (c instanceof JPanel) {
JPanel jp = (JPanel)c;
grabFocusForComboBox(jp.getComponents());
}
}
}
private static void clickBackSpace() {
prevDir = fileChooser.getCurrentDirectory();
robot.keyPress(KeyEvent.VK_BACK_SPACE);
robot.keyRelease(KeyEvent.VK_BACK_SPACE);
crntDir = fileChooser.getCurrentDirectory();
}
}

View file

@ -0,0 +1,216 @@
/*
* Copyright (c) 2022, 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 4912623
* @key headful
* @requires (os.family == "linux")
* @summary Verifies if all files or folders are selected on CTRL+A press for
* JFileChooser.
* @run main TestFileChooserCtrlASelection
*/
import java.awt.BorderLayout;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.Point;
import java.awt.Robot;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class TestFileChooserCtrlASelection {
private static JFrame frame;
private static JFileChooser fileChooser;
private static Robot robot;
private static File testDir;
private static File testFile;
private static File[] SubDirs;
private static File[] subFiles;
private static boolean passed_1 = false;
private static boolean passed_2 = false;
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
robot = new Robot();
robot.setAutoDelay(100);
try {
// create test directory
String tmpDir = System.getProperty("user.home");
// Create a test directory that contains only folders
testDir = new File(tmpDir, "testDir");
if (!testDir.exists()) {
testDir.mkdir();
}
testDir.deleteOnExit();
// create sub directories inside test directory
SubDirs = new File[5];
for (int i = 0; i < 5; ++i) {
SubDirs[i] = new File(testDir, "subDir_" + (i+1));
SubDirs[i].mkdir();
SubDirs[i].deleteOnExit();
}
// Create a test directory that contains only files
testFile = new File(tmpDir, "testFile");
if (!testFile.exists()) {
testFile.mkdir();
}
testFile.deleteOnExit();
// create temporary files inside testFile
subFiles = new File[5];
for (int i = 0; i < 5; ++i) {
subFiles[i] = File.createTempFile("subFiles_" + (i+1),
".txt", new File(testFile.getAbsolutePath()));
subFiles[i].deleteOnExit();
}
} catch (Exception e) {
e.printStackTrace();
}
doTesting();
}
private static void doTesting() throws Exception {
try {
SwingUtilities.invokeAndWait(() -> {
createAndShowUI();
});
robot.delay(1000);
checkMultiSelectionDefault();
checkMultiSelectionDisabled();
} finally {
SwingUtilities.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
private static void createAndShowUI() {
frame = new JFrame("Test File Chooser Ctrl+A Action");
frame.getContentPane().setLayout(new BorderLayout());
fileChooser = new JFileChooser(testDir);
fileChooser.setControlButtonsAreShown(false);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/*
* JFileChooser MultiSelection property is set true by default.
*/
private static void checkMultiSelectionDefault() {
System.out.println("Testing MultiSelection enabled by default");
Point frameLocation = fileChooser.getLocationOnScreen();
int frameHeight = frame.getHeight();
// check Ctrl+A on folders list
doMouseClick(frameLocation, frameHeight, 50);
doKeyPressAction();
File files[] = fileChooser.getSelectedFiles();
System.out.println("files length: " + files.length);
if (files.length > 1) {
passed_1 = true;
}
// check Ctrl+A on files list
fileChooser.setCurrentDirectory(testFile);
doMouseClick(frameLocation, frameHeight, 230);
doKeyPressAction();
files = fileChooser.getSelectedFiles();
System.out.println("files length: " + files.length);
if (files.length > 1) {
passed_2 = true;
}
if (passed_1 && passed_2) {
System.out.println("Passed");
} else {
throw new RuntimeException("Unable to select all files " +
"or folder");
}
}
private static void checkMultiSelectionDisabled() {
System.out.println("Testing MultiSelection disabled");
passed_1 = false;
passed_2 = false;
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setCurrentDirectory(testDir);
Point frameLocation = fileChooser.getLocationOnScreen();
int frameHeight = frame.getHeight();
// check Ctrl+A on folders list
doMouseClick(frameLocation, frameHeight, 50);
doKeyPressAction();
File files[] = fileChooser.getSelectedFiles();
System.out.println("files length: " + files.length);
if (files.length == 0) {
passed_1 = true;
}
// check Ctrl+A on files list
fileChooser.setCurrentDirectory(testFile);
doMouseClick(frameLocation, frameHeight, 230);
doKeyPressAction();
files = fileChooser.getSelectedFiles();
System.out.println("files length: " + files.length);
if (files.length == 0) {
passed_2 = true;
}
if (passed_1 && passed_2) {
System.out.println("Passed");
} else {
throw new RuntimeException("All files or folder selected for" +
"MultiSelection disabled");
}
}
private static void doMouseClick(Point frameLocation, int frameHeight,
int offset) {
robot.mouseMove(frameLocation.x + offset, frameLocation.y +
frameHeight / 3);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(100);
}
private static void doKeyPressAction() {
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.delay(100);
}
}

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2022, 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 6777156
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @requires (os.family == "linux")
* @summary Verifies if user is not able to select "../" beyond
* root file system.
* @run main/manual TestFileChooserDirectorySelection
*/
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class TestFileChooserDirectorySelection {
private static JFrame frame;
private static final String INSTRUCTIONS =
"Double click on the \"../\" entry from directory list.\n\n" +
"Repeat the same process till the current directory is root " +
"i.e \" / \" .\n\n" +
"If \" ../ \" option is not available at root directory" +
", press Pass else Fail.";
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
PassFailJFrame passFailJFrame = new PassFailJFrame(
"JFileChooser Test Instructions", INSTRUCTIONS, 5, 8, 35);
try {
SwingUtilities.invokeAndWait(
TestFileChooserDirectorySelection::createAndShowUI);
passFailJFrame.awaitAndCheck();
} finally {
SwingUtilities.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
private static void createAndShowUI() {
frame = new JFrame("Test File Chooser Directory Selection");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setControlButtonsAreShown(false);
PassFailJFrame.addTestWindow(frame);
PassFailJFrame.positionTestWindow(
frame, PassFailJFrame.Position.HORIZONTAL);
frame.add(fileChooser);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 2022, 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 8081507
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @requires (os.family == "linux")
* @summary Verifies if Open/Save button shows text as Open/Save.
* @run main/manual TestFileChooserOpenSaveButtonText
*/
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class TestFileChooserOpenSaveButtonText {
private static JFrame frame;
private static final String INSTRUCTIONS =
"Instructions: \n\n" +
"1. Check the text on button in bottom panel is Open.\n" +
"2. Press Cancel button.\n" +
"3. Check the text on button in bottom panel is Save.\n" +
"4. Press Cancel button.\n" +
"5. If above instructions are confirmed, " +
"Press Pass else Fail.";
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
PassFailJFrame passFailJFrame = new PassFailJFrame(
"JFileChooser Test Instructions", INSTRUCTIONS, 5, 8, 35);
try {
SwingUtilities.invokeAndWait(
TestFileChooserOpenSaveButtonText::createAndShowUI);
passFailJFrame.awaitAndCheck();
} finally {
SwingUtilities.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
private static void createAndShowUI() {
frame = new JFrame("Test File Chooser Open/Save button text");
PassFailJFrame.addTestWindow(frame);
PassFailJFrame.positionTestWindow(
frame, PassFailJFrame.Position.HORIZONTAL);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(null);
fileChooser.showSaveDialog(null);
}
}

View file

@ -0,0 +1,223 @@
/*
* Copyright (c) 2022, 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 6972078
* @key headful
* @requires (os.family == "linux")
* @summary Verifies if user is able to select single directory if multi selection
* is enabled for JFileChooser.
* @run main TestFileChooserSingleDirectorySelection
*/
import java.io.File;
import java.awt.BorderLayout;
import java.awt.event.InputEvent;
import java.awt.Point;
import java.awt.Robot;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestFileChooserSingleDirectorySelection {
private static JFrame frame;
private static JFileChooser fileChooser;
private static Robot robot;
private static volatile Point clickLocation;
public static void main(String[] args) throws Exception {
System.setProperty("sun.java2d.uiScale", "1.0");
robot = new Robot();
robot.setAutoDelay(100);
File testDirDirs = createFoldersOnlyDir();
File testDirFiles = createFilesOnlyDir();
populateDirs(testDirDirs);
populateFiles(testDirFiles);
for (UIManager.LookAndFeelInfo laf :
UIManager.getInstalledLookAndFeels()) {
System.out.println("Testing LAF: " + laf.getClassName());
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
checkFileOnlyTest(laf, testDirFiles);
checkDirectoriesOnlyTest(laf, testDirDirs);
checkFilesAndDirectoriesTest(laf, testDirDirs);
System.out.println("Passed");
}
}
private static File createFoldersOnlyDir() {
String tmpDir = System.getProperty("java.io.tmpdir");
File dirsDir = new File(tmpDir, "dirsDir");
if (!dirsDir.exists()) {
dirsDir.mkdir();
}
dirsDir.deleteOnExit();
return dirsDir;
}
private static void populateDirs(File parent) {
for (int i = 0; i < 10; ++i) {
File subDir = new File(parent, "subDir_" + (i+1));
subDir.mkdir();
subDir.deleteOnExit();
}
}
private static File createFilesOnlyDir() {
String tmpDir = System.getProperty("java.io.tmpdir");
File filesDir = new File(tmpDir, "filesDir");
if (!filesDir.exists()) {
filesDir.mkdir();
}
filesDir.deleteOnExit();
return filesDir;
}
private static void populateFiles(File parent) throws Exception {
for (int i = 0; i < 10; ++i) {
File subFile = new File(parent, "subFiles_" + (i+1));
subFile.createNewFile();
subFile.deleteOnExit();
}
}
private static void checkFileOnlyTest(UIManager.LookAndFeelInfo laf,
File dir) throws Exception {
System.out.println("Testing File Only mode");
try {
SwingUtilities.invokeAndWait(() -> {
createAndShowUI();
fileChooser.setCurrentDirectory(dir);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
});
robot.waitForIdle();
robot.delay(1000);
if (laf.getClassName().contains("Motif")
|| laf.getClassName().contains("GTK")) {
doTesting(laf, 230);
} else {
doTesting(laf, 50);
}
} finally {
disposeFrame();
}
}
private static void checkDirectoriesOnlyTest(UIManager.LookAndFeelInfo laf,
File dir) throws Exception {
System.out.println("Testing Directories Only mode");
try {
SwingUtilities.invokeAndWait(() -> {
createAndShowUI();
fileChooser.setCurrentDirectory(dir);
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
});
robot.waitForIdle();
robot.delay(1000);
doTesting(laf, 50);
} finally {
disposeFrame();
}
}
private static void checkFilesAndDirectoriesTest(UIManager.LookAndFeelInfo laf,
File dir) throws Exception {
System.out.println("Testing Files and Directories mode");
try {
SwingUtilities.invokeAndWait(() -> {
createAndShowUI();
fileChooser.setCurrentDirectory(dir);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
});
robot.waitForIdle();
robot.delay(1000);
doTesting(laf, 50);
} finally {
disposeFrame();
}
}
private static void createAndShowUI() {
frame = new JFrame("Test File Chooser Single Directory Selection");
frame.getContentPane().setLayout(new BorderLayout());
fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
fileChooser.setControlButtonsAreShown(false);
frame.getContentPane().add(fileChooser, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
try {
UIManager.setLookAndFeel(laf.getClassName());
} catch (UnsupportedLookAndFeelException ignored) {
System.out.println("Unsupported LAF: " + laf.getClassName());
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private static void doTesting(UIManager.LookAndFeelInfo laf, int xOffset)
throws Exception {
SwingUtilities.invokeAndWait(() -> {
Point fileChooserLocation = fileChooser.getLocationOnScreen();
fileChooserLocation.y += frame.getHeight() / 3;
clickLocation = new Point(fileChooserLocation);
});
clickMouse(clickLocation, xOffset);
checkResult(laf);
}
private static void clickMouse(Point point, int xOffset) {
robot.mouseMove(point.x + xOffset , point.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(100);
robot.waitForIdle();
}
private static void checkResult(UIManager.LookAndFeelInfo laf) throws Exception {
SwingUtilities.invokeAndWait(() -> {
File[] files = fileChooser.getSelectedFiles();
if (files.length == 0) {
throw new RuntimeException("getSelectedFiles returned " +
"empty array for LAF: " + laf.getClassName());
}
});
}
private static void disposeFrame() throws Exception {
SwingUtilities.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}

View file

@ -0,0 +1,146 @@
/*
* Copyright (c) 2011, 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.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.text.JTextComponent;
import jtreg.SkippedException;
/*
* @test
* @bug 6492108 8160755
* @key headful
* @requires (os.family == "linux")
* @summary Verifies that the background is painted the same for
* JTextArea, JTextPane, and JEditorPane.
* @library /javax/swing/regtesthelpers /test/lib
* @build SwingTestHelper Util
* @run main/othervm bug6492108
*/
public class bug6492108 extends SwingTestHelper {
private JPanel panel;
public static void main(String[] args) throws Throwable {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
} catch (Exception e) {
throw new SkippedException("GTK LAF is not supported on this system");
}
new bug6492108().run(args);
}
private static void addTextComps(Container parent,
Class<? extends JTextComponent> type)
throws Throwable
{
JTextComponent text = type.newInstance();
addTextComp(parent, text);
text = type.newInstance();
text.setEditable(false);
addTextComp(parent, text);
text = type.newInstance();
text.setEnabled(false);
addTextComp(parent, text);
text = type.newInstance();
text.setEnabled(false);
text.setEditable(false);
addTextComp(parent, text);
}
private static void addTextComp(Container parent, JTextComponent text) {
JScrollPane sp = new JScrollPane(text);
text.setFocusable(false); // to avoid showing the blinking caret
sp.setPreferredSize(new Dimension(150, 150));
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
parent.add(sp);
}
protected Component createContentPane() {
panel = new JPanel();
panel.setLayout(new GridLayout(3, 4));
try {
addTextComps(panel, JTextArea.class);
addTextComps(panel, JEditorPane.class);
addTextComps(panel, JTextPane.class);
} catch (Throwable t) {
fail("Problem creating text components");
}
setDelay(50);
return panel;
}
private void onEDT10() {
requestAndWaitForFocus(panel);
}
private void onEDT20() {
// For each component on the top row, compare against the two
// components below in the same column. All three components in
// that column should be the same pixel-for-pixel.
for (int count = 0; count < 4; count++) {
Component ref = panel.getComponent(count);
Rectangle refRect = new Rectangle(ref.getLocationOnScreen(), ref.getSize());
BufferedImage refImg = robot.createScreenCapture(refRect);
for (int k = 1; k < 3; k++) {
int index = count + (k*4);
Component test = panel.getComponent(index);
Rectangle testRect = new Rectangle(test.getLocationOnScreen(), test.getSize());
BufferedImage testImg = robot.createScreenCapture(testRect);
if (!Util.compareBufferedImages(refImg, testImg)) {
try {
ImageIO.write(refImg, "png", new File("refImg.png"));
ImageIO.write(testImg, "png", new File("testImg.png"));
} catch (IOException ignored) {}
fail("Image comparison failed for images at index " + count + " and " + index);
}
}
}
}
}

View file

@ -0,0 +1,95 @@
/*
* 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 4210461
* @summary Tests that Motif Look & Feel's MenuItem Accelerator Delimiter is
* shown properly
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual MotifLAFMenuAcceleratorDelimiter
*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
public class MotifLAFMenuAcceleratorDelimiter {
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch (Exception e) {
throw new RuntimeException("The Motif LAF failed to instantiate");
}
String INSTRUCTIONS = """
The visual design specification for the Motif LAF asks for
a "+" to delimit the other two entities in a menu item's
accelerator.
As a point of reference, the visual design specifications for the
L&Fs are as follows: JLF/Metal = "-", Mac = "-", Motif = "+",
Windows = "+".
Click on "Menu" of "MotifLAFMenuAcceleratorDelimiter" window,
make sure it shows MenuItem with label "Hi There! ^+H" or
"Hi There! Ctrl+H".
If it shows same label test passed otherwise failed.
""";
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(50)
.testUI(MotifLAFMenuAcceleratorDelimiter::initialize)
.build()
.awaitAndCheck();
}
private static JFrame initialize() {
JFrame fr = new JFrame("MotifLAFMenuAcceleratorDelimiter");
JPanel menuPanel = new JPanel();
JMenuBar menuBar = new JMenuBar();
menuBar.setOpaque(true);
JMenu exampleMenu = new JMenu("Menu");
JMenuItem hiMenuItem = new JMenuItem("Hi There!");
hiMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
ActionEvent.CTRL_MASK));
exampleMenu.add(hiMenuItem);
menuBar.add(exampleMenu);
menuPanel.add(menuBar);
fr.setLayout(new BorderLayout());
fr.add(menuPanel, BorderLayout.CENTER);
fr.setSize(250,100);
return fr;
}
}

View file

@ -0,0 +1,71 @@
/*
* 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 4141400
* @summary Tests that the divider of JSplitPane can be moved only by
* dragging its thumb under Motif LAF
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual bug4141400
*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
public class bug4141400 {
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
} catch (Exception e) {
throw new RuntimeException("Failed to set Motif LAF");
}
String INSTRUCTIONS = """
Place mouse cursor somewhere on the split pane divider, but outside
its thumb. Then try to move the divider. It should not move. If it
does not move, the test passes, otherwise it fails.
""";
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(70)
.testUI(bug4141400::initialize)
.build()
.awaitAndCheck();
}
private static JFrame initialize() {
JFrame fr = new JFrame("bug4141400");
JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
true,
new JButton("Button 1"),
new JButton("Button 2"));
fr.add(pane);
fr.setSize(250, 300);
return fr;
}
}

View file

@ -0,0 +1,91 @@
/*
* Copyright (c) 2013, 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 8016551
* @summary JMenuItem in WindowsLookAndFeel can't paint default icons
* @author Leonid Romanov
* @run main/othervm bug8016551
*/
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Robot;
public class bug8016551 {
private static volatile RuntimeException exception = null;
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
// We intentionally allow the test to run with other l&f
}
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
Icon icon = UIManager.getIcon("InternalFrame.closeIcon");
if (icon == null) {
return;
}
JMenuItem item = new TestMenuItem(icon);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(item);
f.pack();
f.setVisible(true);
} catch (ClassCastException e) {
throw new RuntimeException(e);
}
}
});
Robot robot = new Robot();
robot.waitForIdle();
if (exception != null) {
throw exception;
}
}
static class TestMenuItem extends JMenuItem {
TestMenuItem(Icon icon) {
super(icon);
}
@Override
public void paint(Graphics g) {
try {
super.paint(g);
} catch (RuntimeException e) {
exception = e;
}
}
}
}

View file

@ -0,0 +1,155 @@
/*
* Copyright (c) 2018, 2019, 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 8211987
* @key headful
* @requires (os.family == "windows")
* @summary Verify if Menu bar gets input focus even if Alt-released event is consumed.
* @modules java.desktop/com.sun.java.swing.plaf.windows
* @run main AltFocusIssueTest
*/
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
import java.awt.Robot;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
* Try to demonstrate the wrong behavior
*/
public class AltFocusIssueTest {
/**
* Menu inside menu bar of the frame
*/
private static JMenu menu;
/**
* Text area to test on.
*/
private static JTextArea ta;
private static JFrame frame;
/**
* Test that the text area loses input focus although Alt-released event is consumed.
*
* @throws InterruptedException
* @throws InvocationTargetException
*/
public static void testAltEvents() throws Exception {
Robot robot = new Robot();
SwingUtilities.invokeAndWait(() -> {
try {
createUI();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> ta.requestFocusInWindow());
robot.waitForIdle();
if (!ta.isFocusOwner()) {
throw new RuntimeException("textarea should have input focus");
}
if (menu.isSelected()) {
throw new RuntimeException("menu is selected...");
}
// Simulate an Alt-typed event
robot.keyPress(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_ALT);
robot.waitForIdle();
// Since the event is consumed, I expect the input focus to be in the text area
if (!ta.isFocusOwner()) {
throw new RuntimeException("textarea should still have input focus");
}
// OR
if (SwingUtilities.getRootPane(ta).isFocusOwner()) {
throw new RuntimeException("Focus should not be changed from the text area");
}
// OR
if (menu.isSelected()) {
throw new RuntimeException("Menu must not be selected");
}
}
/**
* Builds UI to test.
*
*/
private static void createUI() throws Exception {
// Install Windows L&F
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
menu = new JMenu("Menu");
menu.add(new JMenuItem("Menu item"));
menuBar.add(menu);
ta = new JTextArea();
frame.getContentPane().add(ta);
ta.addKeyListener( new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ALT) {
/*
* This is where I need to do special handling of the Alt-released event.
* After, nobody else must react to this event, thus I consume it.
*/
e.consume();
}
}
});
frame.setSize(400, 300);
frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
try {
testAltEvents();
} finally {
if (frame != null) {
SwingUtilities.invokeAndWait(() -> frame.dispose());
}
}
}
}

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2003, 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 4685843
* @requires (os.family == "windows")
* @summary Tests that disabled JCheckBoxMenuItem's are drawn properly in
* Windows LAF
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual bug4685843
*/
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.UIManager;
public class bug4685843 {
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel (
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
throw new RuntimeException("Failed to set Windows LAF");
}
String INSTRUCTIONS = """
In the window named "bug4685843" open File menu.
If all three disabled items are drawn properly press "Pass".
Otherwise press "Fail".
""";
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(35)
.testUI(bug4685843::initialize)
.build()
.awaitAndCheck();
}
private static JFrame initialize() {
JMenuBar jMenuBar = new JMenuBar();
JMenu jMenu = new JMenu("File");
JMenuItem jMenuItem = new JMenuItem("JMenuItem");
JCheckBoxMenuItem jCheckBoxMenuItem =
new JCheckBoxMenuItem("JCheckBoxMenuItem");
JRadioButtonMenuItem jRadioButtonMenuItem =
new JRadioButtonMenuItem("JRadioButtonMenuItem");
jMenuItem.setEnabled(false);
jMenu.add(jMenuItem);
jCheckBoxMenuItem.setEnabled(false);
jMenu.add(jCheckBoxMenuItem);
jRadioButtonMenuItem.setEnabled(false);
jMenu.add(jRadioButtonMenuItem);
jMenuBar.add(jMenu);
JFrame mainFrame = new JFrame("bug4685843");
mainFrame.setJMenuBar(jMenuBar);
mainFrame.setSize(200, 200);
return mainFrame;
}
}

View file

@ -0,0 +1,95 @@
/*
* 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 4210461
* @requires (os.family == "windows")
* @summary Tests that Windows Look & Feel's MenuItem Accelerator Delimiter is
* shown properly
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual WindowsLAFMenuAcceleratorDelimiter
*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
public class WindowsLAFMenuAcceleratorDelimiter {
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
throw new RuntimeException("The Windows LAF failed to instantiate");
}
String INSTRUCTIONS = """
The visual design specification for the Windows LAF asks for
a "+" to delimit the other two entities in a menuitem's
accelerator.
As a point of reference, the visual design specifications for the
L&Fs are as follows: JLF/Metal = "-", Mac = "-", Motif = "+",
Windows = "+".
Click on "Menu" of "WindowsLAFMenuAcceleratorDelimiter" window,
make sure it shows MenuItem with label "Hi There! Ctrl+H".
If it shows same label test passed otherwise failed.
""";
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(50)
.testUI(WindowsLAFMenuAcceleratorDelimiter::initialize)
.build()
.awaitAndCheck();
}
private static JFrame initialize() {
JFrame fr = new JFrame("WindowsLAFMenuAcceleratorDelimiter");
JPanel menuPanel = new JPanel();
JMenuBar menuBar = new JMenuBar();
menuBar.setOpaque(true);
JMenu exampleMenu = new JMenu("Menu");
JMenuItem hiMenuItem = new JMenuItem("Hi There!");
hiMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
ActionEvent.CTRL_MASK));
exampleMenu.add(hiMenuItem);
menuBar.add(exampleMenu);
menuPanel.add(menuBar);
fr.setLayout(new BorderLayout());
fr.add(menuPanel, BorderLayout.CENTER);
fr.setSize(250, 100);
return fr;
}
}

View file

@ -0,0 +1,193 @@
/*
* Copyright (c) 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.
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import static javax.swing.BorderFactory.createEmptyBorder;
/*
* @test id=windows
* @bug 8348760 8365375 8365389 8365625
* @requires (os.family == "windows")
* @summary Verify that Windows Look & Feel allows changing
* accelerator colors
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual MenuItemAcceleratorColor
*/
/*
* @test id=classic
* @bug 8348760 8365375 8365389 8365625
* @requires (os.family == "windows")
* @summary Verify that Windows Classic Look & Feel allows changing
* accelerator colors
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual MenuItemAcceleratorColor classic
*/
public final class MenuItemAcceleratorColor {
private static final String INSTRUCTIONS =
"Click the Menu to open it.\n" +
"\n" +
"Verify that the first and the last menu items render " +
"their accelerators using the default colors, the color " +
"should match that of the menu item itself in regular and " +
"selected states.\n" +
"\n" +
"Verify that the second menu item renders its accelerator " +
"with green and that the color changes to red when selected.\n" +
"\n" +
"Verify that the third menu item renders its accelerator " +
"with magenta and yellow correspondingly.\n" +
"\n" +
"Verify that only the fifth menu item renders its accelerator " +
"with blue; both the fourth and sixth should render their " +
"accelerator with a shade of gray.\n" +
"\n" +
"If the above conditions are satisfied, press the Pass button; " +
"otherwise, press the Fail button.";
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel((args.length > 0 && "classic".equals(args[0]))
? "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"
: "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.rows(20)
.columns(60)
.testUI(MenuItemAcceleratorColor::createUI)
.build()
.awaitAndCheck();
}
private static Box createInfoPanel() {
Box box = Box.createVerticalBox();
box.add(new JLabel("Look and Feel: "
+ UIManager.getLookAndFeel()
.getName()));
box.add(new JLabel("Java version: "
+ System.getProperty("java.runtime.version")));
return box;
}
private static JFrame createUI() {
JPanel content = new JPanel(new BorderLayout());
content.setBorder(createEmptyBorder(8, 8, 8, 8));
content.add(createInfoPanel(),
BorderLayout.SOUTH);
JFrame frame = new JFrame("Accelerator colors in Windows L&F");
frame.setJMenuBar(createMenuBar());
frame.add(content, BorderLayout.CENTER);
frame.setSize(350, 370);
return frame;
}
private static JMenuBar createMenuBar() {
JMenuItem first = new JMenuItem("First menu item");
first.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
InputEvent.CTRL_DOWN_MASK));
// Modify colors for accelerator rendering
Color acceleratorForeground = UIManager.getColor("MenuItem.acceleratorForeground");
Color acceleratorSelectionForeground = UIManager.getColor("MenuItem.acceleratorSelectionForeground");
UIManager.put("MenuItem.acceleratorForeground", Color.GREEN);
UIManager.put("MenuItem.acceleratorSelectionForeground", Color.RED);
JMenuItem second = new JMenuItem("Second menu item");
second.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.SHIFT_DOWN_MASK
| InputEvent.CTRL_DOWN_MASK));
UIManager.put("MenuItem.acceleratorForeground", Color.MAGENTA);
UIManager.put("MenuItem.acceleratorSelectionForeground", Color.YELLOW);
JMenuItem third = new JMenuItem("Third menu item");
third.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T,
InputEvent.ALT_DOWN_MASK));
// Restore colors
UIManager.put("MenuItem.acceleratorForeground", acceleratorForeground);
UIManager.put("MenuItem.acceleratorSelectionForeground", acceleratorSelectionForeground);
// Disabled foreground
JMenuItem fourth = new JMenuItem("Fourth menu item");
fourth.setEnabled(false);
fourth.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
InputEvent.CTRL_DOWN_MASK));
Color disabledForeground = UIManager.getColor("MenuItem.disabledForeground");
UIManager.put("MenuItem.disabledForeground", Color.BLUE);
JMenuItem fifth = new JMenuItem("Fifth menu item");
fifth.setEnabled(false);
fifth.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
InputEvent.CTRL_DOWN_MASK
| InputEvent.SHIFT_DOWN_MASK));
// Restore disabled foreground
UIManager.put("MenuItem.disabledForeground", disabledForeground);
JMenuItem sixth = new JMenuItem("Sixth menu item");
sixth.setEnabled(false);
sixth.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_DOWN_MASK
| InputEvent.ALT_DOWN_MASK));
JMenuItem quit = new JMenuItem("Quit");
quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
InputEvent.CTRL_DOWN_MASK));
JMenu menu = new JMenu("Menu");
menu.add(first);
menu.add(second);
menu.add(third);
menu.addSeparator();
menu.add(fourth);
menu.add(fifth);
menu.add(sixth);
menu.addSeparator();
menu.add(quit);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
return menuBar;
}
}

View file

@ -0,0 +1,113 @@
/*
* Copyright (c) 2017, 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.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Robot;
import java.awt.Toolkit;
import java.lang.reflect.Method;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
/**
* @test
* @key headful
* @bug 8075255
* @summary tests if the desktop property changes this will force the UIs to
* update all known Frames
* @requires (os.family == "windows")
* @modules java.desktop/java.awt:open java.desktop/sun.awt
*/
public final class RevalidateOnPropertyChange {
private static JFrame frame;
private static JButton button;
private static volatile Dimension sizeAfter;
private static volatile Dimension sizeBefore;
private static volatile boolean flag;
public static void main(String[] args) throws Exception {
System.setProperty("swing.useSystemFontSettings", "true");
UIManager.put("Application.useSystemFontSettings", true);
// this functionality is supported on windows in "Windows and Metal L&F"
test("com.sun.java.swing.plaf.windows.WindowsLookAndFeel",
"win.defaultGUI.font",
new Font(Font.DIALOG, FontUIResource.BOLD, 40));
test("javax.swing.plaf.metal.MetalLookAndFeel",
"win.ansiVar.font.height", 70);
}
/**
* Emulates the property change via reflection.
*/
static void test(String laf, String prop, Object value) throws Exception {
Class cls = Toolkit.class;
Method setDesktopProperty =
cls.getDeclaredMethod("setDesktopProperty", String.class,
Object.class);
setDesktopProperty.setAccessible(true);
UIManager.setLookAndFeel(laf);
EventQueue.invokeAndWait(RevalidateOnPropertyChange::createGUI);
Robot r = new Robot();
r.waitForIdle();
EventQueue.invokeAndWait(() -> {
sizeBefore = button.getSize();
});
Toolkit toolkit = Toolkit.getDefaultToolkit();
toolkit.addPropertyChangeListener(prop, evt -> flag = true);
setDesktopProperty.invoke(toolkit, prop, value);
r.waitForIdle();
EventQueue.invokeAndWait(() -> {
sizeAfter = button.getSize();
frame.dispose();
});
if (!flag) {
throw new RuntimeException("The listener was not notified");
}
if (sizeAfter.equals(sizeBefore)) {
throw new RuntimeException("Size was not changed :" + sizeAfter);
}
}
private static void createGUI() {
frame = new JFrame();
button = new JButton(UIManager.getLookAndFeel().getName());
frame.setLayout(new FlowLayout());
frame.getContentPane().add(button);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2009, 2017, 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 6824600 8075255
@summary OOM occurs when setLookAndFeel() is executed in Windows L&F(XP style)
@author Pavel Porvatov
@modules java.desktop/sun.swing.plaf
@run main Test6824600
*/
import sun.swing.plaf.DesktopProperty;
import java.awt.*;
public class Test6824600 {
public static void main(String[] args) throws Exception {
Toolkit toolkit = Toolkit.getDefaultToolkit();
HackedDesktopProperty desktopProperty = new HackedDesktopProperty("Button.background", null);
// Register listener in toolkit
desktopProperty.getValueFromDesktop();
int length = toolkit.getPropertyChangeListeners().length;
// Make several invocations
desktopProperty.getValueFromDesktop();
desktopProperty.getValueFromDesktop();
desktopProperty.invalidate();
desktopProperty.getValueFromDesktop();
desktopProperty.getValueFromDesktop();
if (length != toolkit.getPropertyChangeListeners().length) {
throw new RuntimeException("New listeners were added into Toolkit");
}
}
public static class HackedDesktopProperty extends DesktopProperty {
public HackedDesktopProperty(String key, Object fallback) {
super(key, fallback);
}
// Publish the method
public Object getValueFromDesktop() {
return super.getValueFromDesktop();
}
}
}

View file

@ -0,0 +1,142 @@
/*
* Copyright (c) 2017, 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.
*/
/* @test
@key headful
@bug 8173145
@requires (os.family == "windows")
@summary Menu is activated after using mnemonic Alt/Key combination
@run main Test8173145
*/
import java.awt.AWTException;
import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class Test8173145 {
private volatile static JButton btn;
private volatile static JFrame f;
private volatile static boolean uiCreated;
public static void main(String[] args) throws InvocationTargetException, InterruptedException, AWTException {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
uiCreated = createGUI();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
if (uiCreated) {
test();
} else {
//no windows l&f, skip the test
}
}finally {
SwingUtilities.invokeAndWait(() -> f.dispose());
}
}
private static void test() {
final Robot robot;
try {
robot = new Robot();
} catch (AWTException e) {
throw new RuntimeException(e);
}
robot.setAutoDelay(150);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_M);
robot.keyRelease(KeyEvent.VK_M);
robot.keyRelease(KeyEvent.VK_ALT);
robot.waitForIdle();
Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
if (focusOwner != btn) {
throw new RuntimeException("Wrong focus owner");
}
}
private static boolean createGUI() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
return false;
}
f = new JFrame();
JPanel panel = new JPanel();
btn = new JButton("Mmmmm");
btn.setMnemonic(KeyEvent.VK_M);
btn.setDisplayedMnemonicIndex(0);
panel.add(btn);
JTextField tf = new JTextField();
tf.setColumns(10);
panel.add(tf);
f.setJMenuBar(getMenuBar());
f.add(panel);
f.pack();
f.setVisible(true);
f.setLocationRelativeTo(null);
tf.requestFocus();
return true;
}
static JMenuBar getMenuBar() {
JMenuBar menuBar;
JMenu menu;
menuBar = new JMenuBar();
menu = new JMenu("Menu");
menuBar.add(menu);
JMenuItem mi = new JMenuItem("test");
menu.add(mi);
return menuBar;
}
}

View file

@ -0,0 +1,99 @@
/*
* Copyright (c) 2000, 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 4227768
* @requires (os.family == "windows")
* @summary Tests Z-ordering of Windows Look-and-Feel JInternalFrames
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual bug4227768
*/
import java.awt.Dimension;
import java.awt.Toolkit;
import java.beans.PropertyVetoException;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.UIManager;
public class bug4227768 {
private static JDesktopPane desktop ;
private static JFrame frame;
private static int openFrameCount = 0;
private static final int xOffset = 30;
private static final int yOffset = 30;
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel
("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
throw new RuntimeException("Failed to set Windows LAF");
}
String INSTRUCTIONS = """
Close the internal frame titled "Document #4". The internal frame
titled "Document #3" should get active. Now close the internal
frame titled "Document #2". The internal frame titled "Document #3"
should remain active. If something is not like this, then test
failed. Otherwise test succeeded.
""";
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(50)
.testUI(bug4227768::initialize)
.build()
.awaitAndCheck();
}
private static JFrame initialize() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("bug4227768");
frame.setSize(screenSize.width / 3, screenSize.height / 3);
frame.add(desktop = new JDesktopPane());
createFrame();
createFrame();
createFrame();
createFrame();
desktop.putClientProperty("JDesktopPane.dragMode", "outline");
return frame;
}
protected static void createFrame() {
JInternalFrame internalFrame = new JInternalFrame
("Document #" + (++openFrameCount), true, true, true, true);
internalFrame.setSize(frame.getWidth() / 2, frame.getHeight() / 2);
internalFrame.setLocation(xOffset * openFrameCount,
yOffset * openFrameCount);
desktop.add(internalFrame);
internalFrame.setVisible(true);
try {
internalFrame.setSelected(true);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -0,0 +1,97 @@
/*
* Copyright (c) 2000, 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 4305725
* @requires (os.family == "windows")
* @summary Tests if in Win LAF the JOptionPane.showInternalMessageDialog() is
* not maximized to match background maximized internal frame.
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual bug4305725
*/
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
public class bug4305725 implements ActionListener {
private static JDesktopPane desktop ;
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel
("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
throw new RuntimeException("Failed to set Windows LAF");
}
String INSTRUCTIONS = """
Maximize the internal frame, then call Exit from File menu.
You will see a message box. If message box is also the size of
internal frame, then test failed. If it is of usual size,
then test is passed.
""";
PassFailJFrame.builder()
.instructions(INSTRUCTIONS)
.columns(50)
.testUI(bug4305725::initialize)
.build()
.awaitAndCheck();
}
private static JFrame initialize() {
JFrame frame = new JFrame("bug4305725");
frame.add(desktop = new JDesktopPane());
JMenuBar mb = new JMenuBar() ;
JMenu menu = new JMenu("File");
mb.add(menu) ;
JMenuItem menuItem = menu.add(new JMenuItem("Exit"));
menuItem.addActionListener(new bug4305725()) ;
frame.setJMenuBar(mb) ;
Dimension sDim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(sDim.width / 2, sDim.height / 2) ;
JInternalFrame internalFrame = new JInternalFrame
("Internal", true, true, true, true);
internalFrame.setSize(frame.getWidth(), frame.getHeight() / 2);
internalFrame.setVisible(true);
desktop.add(internalFrame, JLayeredPane.FRAME_CONTENT_LAYER);
return frame;
}
@Override
public void actionPerformed(ActionEvent aEvent) {
JOptionPane.showInternalMessageDialog(desktop, "Exiting test app");
}
}

View file

@ -0,0 +1,79 @@
/*
* 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.
*
* 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.
*/
/*
* Portions Copyright (c) 2012 IBM Corporation
*/
/* @test
* @bug 7089914 8174720
* @requires (os.family == "windows")
* @modules java.desktop/com.sun.java.swing.plaf.windows:open
* @summary Focus on image icons are not visible in javaws cache with high contrast mode
* @author Sean Chou
*/
import javax.swing.*;
import java.lang.reflect.Field;
public class bug7089914 {
public static void main(String[] args) throws Exception {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
System.out.println("Not WindowsLookAndFeel, test skipped");
return;
}
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
JRadioButton rb = new JRadioButton();
if (!"com.sun.java.swing.plaf.windows.WindowsRadioButtonUI".equals(rb.getUI().getClass().getName())) {
throw new RuntimeException("Unexpected UI class of JRadioButton");
}
try {
Field initializedField = rb.getUI().getClass().getDeclaredField("initialized");
initializedField.setAccessible(true);
if (!initializedField.getBoolean(rb.getUI())) {
throw new RuntimeException("initialized is false");
}
rb.getUI().uninstallUI(rb);
if (initializedField.getBoolean(rb.getUI())) {
throw new RuntimeException("initialized is true");
}
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
});
}
}