Search code examples
javaswingfocus

Filter next focus components


I want to filter the next focus componenet using the tab key

for example , i dont want to focus JLabel, JScrollPane, JScrollBar button, JPanel, etc...

How can i do that dynamically without the programmer to handle the focus?

Here is what i did :

JFrame frame = new JFrame("");
frame.setFocusTraversalPolicy(new JTPCFocusTraversalPolicy());

public class JTPCFocusTraversalPolicy extends LayoutFocusTraversalPolicy {
  protected boolean accept(Component aComponent) {
    return !JTPCGUIUtils.getInstance().filterCompoenent(aComponent);
  }
}

I had a case of a JList and a JButton, but i have to press tab 4 times for moving between thous components.

In my case how can i make only 2 tabs for moving between list and button?


Solution

  • From Java 1.4 onwards you can define FocusTraversalPolicy.