Home » Desktop Java » awt » event » GetFocusTraversalKeys example

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

GetFocusTraversalKeys example

In this example we are going to see how to get the focus traversal keys of an input device in a Java Desktop Application.

This is quite simple and to do it you have to follows these steps

  • Create an input device like an JButton.
  • Create a new HashSet to hold AWTKeyStroke objects.
  • Use the JButton.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS) call to get the FocusTraversalKeys of that input device.
  • Use JButton.add(KeyStroke.getKeyStroke("P")) to add a keystroke to the button.
  • Use the JButton.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, s) to set FocusTraversalKeys of these device.

Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.AWTKeyStroke;
import java.awt.KeyboardFocusManager;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.KeyStroke;

public class Main {

    public static void main(String[] argv) throws Exception {

  JButton jButton = new JButton("a");

  Set<AWTKeyStroke> s = new HashSet<AWTKeyStroke>(jButton.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));


  jButton.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, s);


This was an example on how to get FocusTraversalKeys in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area


Leave a Reply


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of