JDK-8277369 : Strange behavior of JMenuBar with RIGHT_TO_LEFT orientation, arrow keys behaves opposite traversing through keyboard
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 8,11,17,18
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2021-11-16
  • Updated: 2022-03-03
  • Resolved: 2022-02-24
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 19
19 b12Fixed
Description
A DESCRIPTION OF THE PROBLEM :
After applying setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT) to the JMenuBar the arrow keys started to behave the opposite of what expected. Right arrow goes left and Left arrow goes right when trying to traverse through keyboard. Note that I'm using Native look and feel.

Here is the code:

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

@SuppressWarnings("serial")
public class MenuBarProblem extends JFrame {
        
    private JMenuBar menuBar;
    
    private JMenu firstMenu;
    private JMenuItem a;
    private JMenuItem b;
    
    private JMenu secondMenu;
    private JMenuItem c;
    private JMenuItem d;
    
    private JMenu thirdMenu;
    private JMenuItem e;
    private JMenuItem f;
    
    public MenuBarProblem() {
        this.setLayout(new BorderLayout());
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        this.setSize(screenSize);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        this.firstMenu = new JMenu("first");
        this.a = new JMenuItem("a");
        this.b = new JMenuItem("b");
        this.firstMenu.add(this.a);
        this.firstMenu.add(this.b);
        
        this.secondMenu = new JMenu("second");
        this.c = new JMenuItem("c");
        this.d = new JMenuItem("d");
        this.secondMenu.add(this.c);
        this.secondMenu.add(this.d);
        
        this.thirdMenu = new JMenu("third");
        this.e = new JMenuItem("e");
        this.f = new JMenuItem("f");
        this.thirdMenu.add(this.e);
        this.thirdMenu.add(this.f);
        
        this.menuBar = new JMenuBar();
        this.menuBar.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); //Here is the reason
        this.menuBar.add(this.firstMenu);
        this.menuBar.add(this.secondMenu);
        this.menuBar.add(this.thirdMenu);
        this.setJMenuBar(this.menuBar);
    }

}


FREQUENCY : always



Comments
Changeset: 379fd859 Author: Prasanta Sadhukhan <psadhukhan@openjdk.org> Date: 2022-02-24 09:52:17 +0000 URL: https://git.openjdk.java.net/jdk/commit/379fd85932e4b82e9a8e85f8ed8e63202f3cb9bc
24-02-2022

A pull request was submitted for review. URL: https://git.openjdk.java.net/jdk/pull/7396 Date: 2022-02-09 05:39:52 +0000
09-02-2022

[~naoto] Can you please advise if this is expected for RTL orientation or should it be a bug?
03-02-2022

Checked with attached test case in Windows10, issue reproducible, upon left arrow key from keyboard, JMenu on the right is selected. Test Result: ========= 8: Fail 8u311: Fail 11: Fail 11.0.13: Fail 17: Fail 18ea23: Fail
18-11-2021