RFR: 6741930: JOptionPane doesn't honour Focus Traversal Policy
Prasanta Sadhukhan
psadhukhan at openjdk.org
Tue Feb 17 03:07:14 UTC 2026
On Mon, 16 Feb 2026 10:43:50 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:
> The FocusTraversalPolicy of a JOptionPane (JDialog) "reports" via `FocusTraversalPolicy.getInitialComponent`/`FocusTraversalPolicy.getFirstComponnent` that the focusable component passed to a JOptionPane, should get the initial focus. This however doesn't always happen, as the text field's FocusListener methods focusGained and focusLost are not invoked.
> Fix is made to honor the first focusable component of custom component (if present) and set the focus accordingly..
> This will cause the component's focusGained/focusLost method to get called.
> CI testing is ok..
I believe it is honoring that too. It will check and requestFocus to `selectInitialValue(op)` initialValue and then fallback to `initialFocusComponent` if it is not set..
Object paneInitVal = op.getInitialValue();
// must be from pane because BasicOptionPane
// sets intialFocusComponent to the default button
if (paneInitVal != null) {
if (paneInitVal instanceof JComponent) {
// if JOptionPane initialValue is a JComponent
((JComponent) paneInitVal).requestFocus();
} else if (initialFocusComponent != null) {
// else custom option button gets
// the focus if available
initialFocusComponent.requestFocus();
-------------
PR Comment: https://git.openjdk.org/jdk/pull/29738#issuecomment-3911539414
More information about the client-libs-dev
mailing list