The javax.swing package library provides a class called SwingUtilities, which offers various utility methods for Swing applications. One such method is replaceUIInputMap, which allows developers to replace an existing input map associated with a specific component.
Example 1:
//Replace the text component's input map for "left" and "right" arrow keys with a custom input map JTextComponent textComp = new JTextArea(); InputMap origInputMap = textComp.getInputMap(); InputMap customInputMap = new InputMap(); customInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "customLeftAction"); customInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "customRightAction"); SwingUtilities.replaceUIInputMap(textComp, JComponent.WHEN_FOCUSED, customInputMap);
In this example, we are replacing the input map for a JTextArea component with a custom input map that maps the "left" and "right" arrow keys to custom actions. We first get the original input map with the getInputMap method and create a new InputMap object for our custom bindings. We then call SwingUtilities.replaceUIInputMap to replace the original input map with our custom input map.
Example 2:
//Replace the menu bar's input map for "escape" key with a null input map JMenuBar menuBar = new JMenuBar(); InputMap origInputMap = menuBar.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); InputMap nullInputMap = new InputMap(); SwingUtilities.replaceUIInputMap(menuBar, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, nullInputMap);
In this example, we are replacing the input map for a menu bar component with a null input map for the "escape" key. We first get the original input map with the getInputMap method and create a new InputMap object that does not have any bindings. We then call SwingUtilities.replaceUIInputMap to replace the original input map with our null input map. This will effectively disable any actions associated with the "escape" key for the menu bar.
Overall, the javax.swing package library provides many utility methods for working with Swing components, including the SwingUtilities class and its replaceUIInputMap method. By using this method, developers can easily replace an existing input map associated with a component and create custom bindings for various keys.
Java SwingUtilities.replaceUIInputMap - 18 examples found. These are the top rated real world Java examples of javax.swing.SwingUtilities.replaceUIInputMap extracted from open source projects. You can rate examples to help us improve the quality of examples.