public class InputContext extends Object
By default, one InputContext instance is created per Window instance, and this input context is shared by all components within the window's container hierarchy. However, this means that only one text input operation is possible at any one time within a window, and that the text needs to be committed when moving the focus from one text component to another. If this is not desired, text components can create their own input context instances.
The Java Platform supports input methods that have been developed in the Java
programming language, using the interfaces in the java.awt.im.spi
package,
and installed into a Java SE Runtime Environment as extensions. Implementations
may also support using the native input methods of the platforms they run on;
however, not all platforms and locales provide input methods. Keyboard layouts
are provided by the host platform.
Input methods are unavailable if (a) no input method written in the Java programming language has been installed and (b) the Java Platform implementation or the underlying platform does not support native input methods. In this case, input contexts can still be created and used; their behavior is specified with the individual methods below.
Component.getInputContext()
,
Component.enableInputMethods(boolean)
Modifier | Constructor and Description |
---|---|
protected |
InputContext()
Constructs an InputContext.
|
Modifier and Type | Method and Description |
---|---|
void |
dispatchEvent(AWTEvent event)
Dispatches an event to the active input method.
|
void |
dispose()
Releases the resources used by this input context.
|
void |
endComposition()
Ends any input composition that may currently be going on in this
context.
|
Object |
getInputMethodControlObject()
Returns a control object from the current input method, or null.
|
static InputContext |
getInstance()
Returns a new InputContext instance.
|
Locale |
getLocale()
Returns the current locale of the current input method or keyboard
layout.
|
boolean |
isCompositionEnabled()
Determines whether the current input method is enabled for composition.
|
void |
reconvert()
Asks the current input method to reconvert text from the
current client component.
|
void |
removeNotify(Component client)
Notifies the input context that a client component has been
removed from its containment hierarchy, or that input method
support has been disabled for the component.
|
boolean |
selectInputMethod(Locale locale)
Attempts to select an input method or keyboard layout that
supports the given locale, and returns a value indicating whether such
an input method or keyboard layout has been successfully selected.
|
void |
setCharacterSubsets(Character.Subset[] subsets)
Sets the subsets of the Unicode character set that input methods of this input
context should be allowed to input.
|
void |
setCompositionEnabled(boolean enable)
Enables or disables the current input method for composition,
depending on the value of the parameter
enable . |
protected InputContext()
getInstance()
.public static InputContext getInstance()
public boolean selectInputMethod(Locale locale)
Not all host operating systems provide API to determine the locale of
the currently selected native input method or keyboard layout, and to
select a native input method or keyboard layout by locale.
For host operating systems that don't provide such API,
selectInputMethod
assumes that native input methods or
keyboard layouts provided by the host operating system support only the
system's default locale.
A text editing component may call this method, for example, when the user changes the insertion point, so that the user can immediately continue typing in the language of the surrounding text.
locale
- The desired new locale.NullPointerException
- if locale
is nullpublic Locale getLocale()
InputMethod.getLocale()
method returns null.
Not all host operating systems provide API to determine the locale of
the currently selected native input method or keyboard layout.
For host operating systems that don't provide such API,
getLocale
assumes that the current locale of all native
input methods or keyboard layouts provided by the host operating system
is the system's default locale.
public void setCharacterSubsets(Character.Subset[] subsets)
subsets
- The subsets of the Unicode character set from which characters may be inputpublic void setCompositionEnabled(boolean enable)
enable
.
An input method that is enabled for composition interprets incoming events for both composition and control purposes, while a disabled input method does not interpret events for composition. Note however that events are passed on to the input method regardless whether it is enabled or not, and that an input method that is disabled for composition may still interpret events for control purposes, including to enable or disable itself for composition.
For input methods provided by host operating systems, it is not always possible to
determine whether this operation is supported. For example, an input method may enable
composition only for some locales, and do nothing for other locales. For such input
methods, it is possible that this method does not throw
UnsupportedOperationException
,
but also does not affect whether composition is enabled.
enable
- whether to enable the current input method for compositionUnsupportedOperationException
- if there is no current input
method available or the current input method does not support
the enabling/disabling operationisCompositionEnabled()
public boolean isCompositionEnabled()
true
if the current input method is enabled for
composition; false
otherwiseUnsupportedOperationException
- if there is no current input
method available or the current input method does not support
checking whether it is enabled for compositionsetCompositionEnabled(boolean)
public void reconvert()
InputMethodRequests.getSelectedText
method. The other InputMethodRequests
methods
must be prepared to deal with further information requests by
the input method. The composed and/or committed text will be
sent to the client component as a sequence of
InputMethodEvent
s. If the input method cannot
reconvert the given text, the text is returned as committed
text in an InputMethodEvent
.UnsupportedOperationException
- if there is no current input
method available or the current input method does not support
the reconversion operation.public void dispatchEvent(AWTEvent event)
event
- The eventNullPointerException
- if event
is nullpublic void removeNotify(Component client)
Component.removeNotify
method. Potentially pending input from input methods
for this component is discarded.
If no input methods are available, then this method has no effect.client
- Client componentNullPointerException
- if client
is nullpublic void endComposition()
A text editing component may call this in a variety of situations, for example, when the user moves the insertion point within the text (but outside the composed text), or when the component's text is saved to a file or copied to the clipboard.
public void dispose()
public Object getInputMethodControlObject()
If no input methods are available or the current input method does not provide an input method control object, then null is returned.
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2018, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.