Corinna Neri
About
-
Posted Questions
No Question(s) posted yet!
Posted Answers
Answer
Baseball
Basketball
Cross-Country
Fencing
Football
Golf
Rowing: Heavyweight
Rowing: Lightweight
Soccer
Squash
Swimming & Diving
Tennis
Track and Field
Answer is posted for the following question.
Answer
Anchorage , AK, USA Latitude and longitude coordinates are: 61217381, -149863129 A wonderful planned city of Anchorage is located in Alaska, the US,
Answer is posted for the following question.
Where is anchorage located?
Answer
DBCC SHRINKFILE tries to shrink each physical log file to its target size immediately However, if part of the logical log resides in the
Answer is posted for the following question.
How to dbcc shrinkfile?
Answer
Place of publication or publisher Name of database Full URL Date of access Example: Centers for Disease Control and Prevention CDC Wonder http://wonder
Answer is posted for the following question.
How to cite mmwr in apa format?
Answer
Focus is the mechanism that determines which of the components in a window will receive keyboard input events. A focus manager looks for special keystrokes that change the focus (usually the Tab and Shift-Tab keys), and then decides which component will next get the focus. Often, you'll want to control how the focus moves between components, especially if you're designing a complex form or dialog for users to enter information.
Java has had a focus manager for AWT components since version 1.0 of the Java Development Kit (JDK), and developers have howled about its inadequacies ever since. Well, it's time to stop howling. The focus management services provided with the Java Foundation Classes (JFC) should satisfy all of your needs for controlling focus changes.
The only way to manage focus changes with JDK 1.0 is through a few methods in the Component class. JDK 1.0 allows components to set the focus to themselves with requestFocus and to change the focus to the next component with nextFocus. Other than getting notification of focus changes with gotFocus and lostFocus, that's pretty much all you can do.
JDK 1.1 deprecates nextFocus and replaces it with transferFocus, which provides exactly the same functionality as nextFocus. Likewise, 1.1 deprecates gotFocus and lostFocus and replaces them with the addFocusListener method to support focus change notification using the 1.1 event model.
Neither JDK 1.0 nor 1.1 provide any means for applications to override the behavior of the focus manager, or to change the algorithm used to determine focus traversal.
Swing provides some powerful new ways to manage focus changes at the component level. Here's what you can do with focus management at that level:
And if this isn't enough to meet your focus control needs, Swing allows you to create and use your own custom focus manager.
Let's take a quick look at the relationship between the AWT and Swing focus managers. The following figure shows how JFC incorporates both the AWT and Swing focus managers.
Note that the AWT focus manager will manage focus for Swing components because they are extended AWT components. However, the flip side is not true: The Swing focus manager will not manage focus for AWT components. The section "Mixing AWT and Swing components" explains how to handle focus management if you're mixing these two component types in an application.
There are two JComponent methods that allow you to explicitly set the focus to a given component: requestFocus and grabFocus. If you've worked with AWT components, you're probably familiar with the requestFocus method. Swing overrides this Component method with an implementation that allows you to call the new setRequestFocusEnabled method. This method can enable or disable the component from getting the focus via requestFocus.
However, you can't use setRequestFocusEnabled to prevent a component from getting the focus via the focus manager. That's because the focus manager uses a new Swing method, grabFocus, to give components the focus. grabFocus works just like requestFocus, except that you can't disable it. The Swing docs say that only focus managers should use grabFocus.
Let's take a look at a bit of sample code written for components. This code will cause a component to get the focus whenever the component is under the mouse:
This fragment adds a mouse listener, which is implemented in an anonymous inner class based on the MouseAdapter adapter class. The mouse adapter's mouseEntered method is called whenever the mouse passes over the component. The implementation for mouseEntered calls hasFocus to see if the component already has the focus, and if it doesn't, calls requestFocus to give the component the focus. A slightly more fail-safe way to set the focus would be:
And if you're writing a focus manager, you can use
grabFocus:
Like AWT 1.1, Swing provides the means for a component to transfer the focus to the next component with the transferFocus method.
This method can be useful for implementing components that automatically advance the focus when the user has completed whatever task or purpose is served by the component (like choosing from a group of radio buttons or items in a list). transferFocus is not really useful for explicitly setting the focus -- you would have to know which component has the focus just before the component that you actually want to set the focus to.
Swing doesn't bring any changes to the operation of AWT 1.1's focus listener implementation. I'll give a brief description of focus listeners for those readers that aren't familiar with them; if you know this stuff you may want to skip on to the next section.
Focus listeners provide a means of notifying applications when components gain or lose the focus. The adapter class for focus events, FocusAdapter, has two methods: focusGained and focusLost. The addFocusListener and removeFocusListener methods add and remove focus listeners for components. Standard event-listener stuff -- nothing fancy here. Let's glance at a piece of code. The following code fragment adds a focus listener that changes the background color when a component gets the focus.
You can't always count on getting focusGained and focusLost events in matched pairs, hence the need for the bFocusIndicated variable to keep up with whether we've indicated focus on a component.
Up until this point, most of what I've covered is probably familiar to you if you have much experience with AWT 1.1 components. Now we'll start getting into Swing's new and improved features.
Swing provides a mechanism for component-managed focus traversal. This can be a powerful alternative to writing your own focus manager and is a better solution for developing components that have their own particular behavior when it comes to gaining and losing the focus. For example, say you want to develop a text field component that won't surrender the focus until the user enters valid data. If you've tried to implement such a component in AWT 1.1 by using the focusLost notification to validate the data and then set the focus back to the text field, you've probably experienced some frustration. One obvious solution to this problem -- calling requestFocus while processing the focusLost event -- doesn't always have a predictable outcome.
With Swing components, you can override the isManagingFocus method to return true and get Tab and Shift-Tab keystrokes sent to the component's key listener. This allows the component to manage focus traversal when the component has the focus. The following code implements a text field that doesn't allow the focus to traverse to the next component until valid data is entered:
In this example, any keystrokes other than Tab keystrokes are considered to be valid data. If no valid data has been entered, the text field's key listener consumes Tab keystrokes, preventing them from reaching the focus manager. Of course, if you were implementing this technique in a real application, you would want to present a message to the user and inform them as to why they are being prevented from tabbing away from the text field.
One limitation to managing focus traversal at the component level is that you can't control focus changes initiated by mouse actions. In fact, you can't even do this by writing your own custom focus manager. To control focus changes initiated by mouse actions, you'll have to modify the low-level mouse handling for all of your components.
The Swing focus manager effectively sits on top of the AWT focus manager. It can only manage focus for Swing components, so if you're mixing AWT and Swing components, you should disable the Swing focus manager with the following line of code.
Answer is posted for the following question.
How to set focus on jtextfield in java?
Answer
What causes brain freeze? When your body senses sudden, extreme cold in the mouth or throat, it tries to react and warm up. Blood vessels throughout the head expand to let extra blood into the area for warmth. That quick change in blood vessel size causes sudden pain.
Answer is posted for the following question.
Why do we get brain freeze from cold drinks?