Re: Am I using the InputMap and the ActionMap correctly?
Johannes Wahle <[email protected]>
| Newsgroups | gmane.comp.java.netbeans.modules.openide.devel |
|---|---|
| Message-ID | <[email protected]> |
What does often mean?
Is this Look and feel dependent?
The approach I mentioned is from the Java tutorials and the following
test passes on my Windows 7 machine.
Regards,
Johannes
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
import javax.swing.table.JTableHeader;
import org.junit.Assert;
import org.junit.Test;
public class InputActionMapTest {
List<Class<? extends JComponent>> componentClasses = Arrays.asList(
JList.class, JTable.class, JTree.class, JLabel.class,
JPanel.class, JTextField.class,
JTextPane.class, JButton.class, JColorChooser.class,
JComboBox.class, JTextArea.class,
JFileChooser.class, JInternalFrame.class, JLayer.class,
JLayeredPane.class, JMenuBar.class,
JOptionPane.class, JPopupMenu.class,
JProgressBar.class, JRootPane.class, JScrollBar.class,
JScrollPane.class, JSeparator.class, JSlider.class,
JSpinner.class, JSplitPane.class,
JTabbedPane.class, JTableHeader.class,
JEditorPane.class, JToolBar.class,
JToolTip.class, JViewport.class
);
@Test
public void testComponentsHaveDifferentActionAndInputMaps() throws
Exception {
for (Class<? extends JComponent> c : componentClasses)
performTest(c);
}
private void performTest(Class<? extends JComponent> c) throws
Exception {
JComponent c1 = c.newInstance();
JComponent c2 = c.newInstance();
assertNotNullAndNotSame(c1.getActionMap(), c2.getActionMap());
assertNotNullAndNotSame(
c1.getInputMap(JComponent.WHEN_FOCUSED),
c2.getInputMap(JComponent.WHEN_FOCUSED));
assertNotNullAndNotSame(
c1.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT),
c2.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
assertNotNullAndNotSame(
c1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW),
c2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW));
}
private void assertNotNullAndNotSame(Object o1, Object o2) {
Assert.assertNotNull(o1);
Assert.assertNotNull(o2);
Assert.assertNotSame(o1, o2);
}
}
On 30.05.2016 17:59, Eirik Bakke wrote:
> Beware that InputMap objects are often shared between different instances
> of a Swing component. So modifying an InputMap that you didn't create
> yourself can affect components that you maybe didn't intend to affect. I
> usually always use a new InputMap that has the old one as a parent.
>
> -- Eirik
>
> On 5/29/16, 4:44 PM, "Klaus Martinschitz" <[email protected]>
> wrote:
>
>> O.K., I did it. I made a mistake.
>> It should look like this...
>>
>>
>> Code:
>> InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "select");//NOI18N
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_R, 0), "rotate");//NOI18N
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD,
>> KeyEvent.SHIFT_DOWN_MASK), "rotatePhi1Positive");//NOI18N
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD,
>> KeyEvent.CTRL_DOWN_MASK), "rotatePhiPositive");//NOI18N
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD,
>> KeyEvent.ALT_DOWN_MASK), "rotatePhi2Positive");//NOI18N
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT,
>> KeyEvent.SHIFT_DOWN_MASK), "rotatePhi1Negative");//NOI18N
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT,
>> KeyEvent.CTRL_DOWN_MASK), "rotatePhiNegative");//NOI18N
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT,
>> KeyEvent.ALT_DOWN_MASK), "rotatePhi2Negative");//NOI18N
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
>> "escape");//NOI18N
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 0), "zoom");//NOI18N
>>
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0),
>> "selectFirstStereographicaReflexProvider");//NOI18N
>> inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_2, 0),
>> "selectSecondStereographicaReflexProvider");//NOI18N
>> setInputMap(WHEN_IN_FOCUSED_WINDOW, inputMap);
>>
>>
>>
>>
>> I didn't use WHEN_IN_FOCUSED_WINDOW.
>>
>> Now it works.
>>
>> Thanks again for your help.
>>
>> Regards,
>> Klaus
>>
>>
>>
>>