Ali Bassam
Ali Bassam

Reputation: 9959

JButton getting focused before a JTextField

enter image description here

As you can see, the passwordField is positioned before jButton1 in the navigator, however, when the usernameField is focused, and I press the TAB key, the second element being focused is the jButton1 and not the passwordField.

enter image description here

What really controls the order of focus?

Upvotes: 0

Views: 170

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347184

The default focus transferral is based more on the visual layout the the order of the components. In this case it would want to go left to right, top to bottom. This is the expected behaviour – MadProgrammer just now edit

Take a look at How to use the Focus Subsystem, especially, Customizing Focus Traversal

Updated

Take a look at switching JTextFields by pressing Enter key, which demonstrates the use of a custom focus policy

Upvotes: 1

Arsen Alexanyan
Arsen Alexanyan

Reputation: 3141

Try to search tab index or tab order to set for the components

Upvotes: -1

devrobf
devrobf

Reputation: 7213

The phrase you need search for is "tab order": there is an answer to this here. Briefly, you will need to implement a FocusTraversalPolicy subclass, and make your container use it by calling setFocusTraversalPolicy.

Upvotes: 2

Related Questions