8365615: Improve JMenuBar/RightLeftOrientation.java

Reviewed-by: prr, psadhukhan
This commit is contained in:
Alexey Ivanov 2025-08-28 13:13:10 +00:00
parent 22ae137400
commit afa8e79ba1

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 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
@ -20,75 +20,76 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4211731 4214512
* @summary
* This test checks if menu bars lay out correctly when their
* ComponentOrientation property is set to RIGHT_TO_LEFT. This test is
* manual. The tester is asked to compare left-to-right and
* right-to-left menu bars and judge whether they are mirror images of each
* other.
* ComponentOrientation property is set to RIGHT_TO_LEFT.
* The tester is asked to compare left-to-right and
* right-to-left menu bars and decide whether they are mirror
* images of each other.
* @library /test/jdk/java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual RightLeftOrientation
*/
import java.awt.ComponentOrientation;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class RightLeftOrientation {
public final class RightLeftOrientation {
static JFrame ltrFrame;
static JFrame rtlFrame;
private static List<JFrame> frames;
private static final String INSTRUCTIONS = """
This test checks menu bars for correct Right-To-Left Component Orientation.
This test checks menu bars for correct Right-To-Left component orientation.
You should see two frames, each containing a menu bar.
One frame will be labelled "Left To Right" and will contain
You should see two frames, each contains a menu bar.
One frame is labelled "Left To Right" and contains
a menu bar with menus starting on its left side.
The other frame will be labelled "Right To Left" and will
contain a menu bar with menus starting on its right side.
The other frame is labelled "Right To Left" and
contains a menu bar with menus starting on its right side.
The test will also contain radio buttons that can be used to set
the look and feel of the menu bars.
For each look and feel, you should compare the two menu
bars and make sure they are mirror images of each other. """;
The test also displays a frame with radio buttons
to change the look and feel of the menu bars.
For each look and feel, compare the two menu bars
in LTR and RTL orientation and make sure they are mirror
images of each other.""";
public static void main(String[] args) throws Exception {
PassFailJFrame.builder()
.title("RTL test Instructions")
.title("Menu Bar RTL Instructions")
.instructions(INSTRUCTIONS)
.rows((int) INSTRUCTIONS.lines().count() + 2)
.columns(30)
.testUI(RightLeftOrientation::createTestUI)
.positionTestUIRightColumn()
.build()
.awaitAndCheck();
}
private static JFrame createTestUI() {
JFrame frame = new JFrame("RightLeftOrientation");
private static JFrame createPlafChangerFrame() {
JFrame frame = new JFrame("Change Look and Feel");
JPanel panel = new JPanel();
ButtonGroup group = new ButtonGroup();
JRadioButton rb;
ActionListener plafChanger = new PlafChanger();
UIManager.LookAndFeelInfo[] lafInfos = UIManager.getInstalledLookAndFeels();
for (int i = 0; i < lafInfos.length; i++) {
rb = new JRadioButton(lafInfos[i].getName());
JRadioButton rb = new JRadioButton(lafInfos[i].getName());
rb.setActionCommand(lafInfos[i].getClassName());
rb.addActionListener(plafChanger);
group.add(rb);
@ -99,33 +100,39 @@ public class RightLeftOrientation {
}
frame.add(panel);
ltrFrame = new JFrame("Left To Right");
ltrFrame.setJMenuBar(createMenuBar(ComponentOrientation.LEFT_TO_RIGHT));
ltrFrame.setSize(400, 100);
ltrFrame.setLocation(new Point(10, 10));
ltrFrame.setVisible(true);
rtlFrame = new JFrame("Right To Left");
rtlFrame.setJMenuBar(createMenuBar(ComponentOrientation.RIGHT_TO_LEFT));
rtlFrame.setSize(400, 100);
rtlFrame.setLocation(new Point(10, 120));
rtlFrame.setVisible(true);
frame.pack();
return frame;
}
static class PlafChanger implements ActionListener {
private static List<JFrame> createTestUI() {
JFrame plafFrame = createPlafChangerFrame();
JFrame ltrFrame = new JFrame("Left To Right");
ltrFrame.setJMenuBar(createMenuBar(ComponentOrientation.LEFT_TO_RIGHT));
ltrFrame.setSize(400, 100);
JFrame rtlFrame = new JFrame("Right To Left");
rtlFrame.setJMenuBar(createMenuBar(ComponentOrientation.RIGHT_TO_LEFT));
rtlFrame.setSize(400, 100);
return (frames = List.of(plafFrame, ltrFrame, rtlFrame));
}
private static final class PlafChanger implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String lnfName = e.getActionCommand();
try {
UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(ltrFrame);
SwingUtilities.updateComponentTreeUI(rtlFrame);
}
catch (Exception exc) {
System.err.println("Could not load LookAndFeel: " + lnfName);
frames.forEach(SwingUtilities::updateComponentTreeUI);
} catch (Exception exc) {
String message = "Could not set Look and Feel to " + lnfName;
System.err.println(message);
JOptionPane.showMessageDialog(frames.get(0),
message,
"Look and Feel Error",
JOptionPane.ERROR_MESSAGE);
}
}