Merlin's Magic: Focus, focus, or focus

Source: Internet
Author: User
Tags abstract new features

The swing based GUI also has some legacy issues, including how to manage focus (which has the priority of receiving keyboard input), how to determine which component has the focus, and how to traverse the focus from one component to the next. Because swing is built on the Abstract Window Toolkit (AWT), management of component focus relies on the underlying focus management in AWT. The previous version of the Java platform relied on the local window manager to help focus management, so while some developers thought focus controls were within their applications, this was not the case. Many platform incompatibilities occur due to the dependence of the underlying local focus system.

As for Merlin, it provides you with a new, AWT-level focus subsystem. This subsystem has its advantages and disadvantages. The starting point of this new model is to create a system that works across platforms, with a centralized keyboardfocusmanager that manages the active and focused windows, as well as the owner of the current focus. The disadvantage is that there is some incompatibility between it and the previous version, which results in some programs not running correctly in newer versions. As a developer, when you create any new programs, you need to be aware of the new focus traversal approach.

The new focus subsystem is quite large, and in this issue we focus on one of the new features ――focustraversalpolicy―― and show you how to manage the focus traversal in a single container. For information on other features, see Resources for links to Sun's documentation and other important guidelines.

What, no interface?

Let's take a look at the Focustraversalpolicy class first. Yes, it's a class, not an interface. However, it is an abstract class and therefore needs to be sorted in detail. The Focustraversalpolicy class can control the focus traversal order in the root of a particular focus loop. The focus loop root is a container whose Focuscycleroot property is set to true. By default, Windows and frames are set to true, and other containers are set to false, but they can also be set to true. Setting the property to true means that when the focus shifts back and forth, the focus stays in a loop component within the root of the focus loop.

The Focustraversalpolicy class consists of 6 methods:

Getdefaultcomponent (Container focuscycleroot)

Getinitialcomponent (Window window)

Getcomponentbefore (Container focuscycleroot, Component acomponent)

Getcomponentafter (Container focuscycleroot, Component acomponent)

Getfirstcomponent (Container focuscycleroot)

Getlastcomponent (Container focuscycleroot)

All 6 methods return a Component object. Of these 6 methods, there are 5 methods that are abstract, and only getinitialcomponent () is the specific method.

As the name suggests, the Getdefaultcomponent () method returns the default component, which receives the focus when the relevant focus loop root gets the focus. Imagine a focus switch along a container and a scenario that switches focus to a container inside that container. This situation is called the downward Focus loop (down focusing cycle). When the focus enters that child container, getdefaultcomponent () needs to return the initial component that should have the focus.

The Getinitialcomponent () method returns the initial component that should have the focus when a window is first displayed. By default, this method returns only the default component of this window-the result that is returned when the Getdefaultcomponent () method is invoked.

The Getcomponentbefore () and Getcomponentafter () methods are in pairs. Depending on a component in a particular focus loop root (such as a container), the two methods return a component before or after that component. Typically, you move to the previous component by pressing the Shift+tab key, or move forward to the next component by pressing the TAB key, but different environments may allow different key sequences to use the keyboard to move the focus. Typically, the code in these methods has a large IF-ELSE statement block or a Map lookup.

The Getfirstcomponent () and Getlastcomponent () methods are also in pairs. While writing the Getfirstcomponent () and Getlastcomponent () methods, you should have a first/last component in mind, but the initial component may not be the first component, and these methods allow you to explicitly set which component is the first component. Which component is the final component.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.