Results 1 to 6 of 6
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default text components and key and input maps

    I am into text components and printing out the content of maps like keymaps/ input maps etc.
    Seeing where they are and what they are and in order to see clearly what happens I am asking for array length etc.
    I do have some questions concerning this material, but first I got a insane error
    which I do not understand:
    when I include the line "x = keys.length;" (see line 4, second code), I get a "Exception in thread "main" java.lang.NullPointerException"
    and when I exclude that line everything goes fine: that just doe not make since to me!


    Java Code:
    System.out.println("Action map's length is: " + keys.length);
            if (keys != null) {
                for (int i = 0; i < keys.length; i++) {
                    Object key = keys[i];
                    Action targetAction = actionMap.get(key);
                    System.out.println(i + " tName: <" + key + ">, action: "
                    + targetAction.getClass().getName());
                }
            }
        }
    Java Code:
     public static void printInputMap(InputMap inputMap, String heading) {
            KeyStroke[] keys = inputMap.allKeys();
            int x = 0;
        //    x = keys.length;
            System.out.println("\n \n" + heading + " (keys length "
            + x + "):");
            if (keys != null) {
                for (int i = 0; i < keys.length; i++) {
                    KeyStroke key = keys[i];
                    Object actionName = inputMap.get(key);
                    System.out.println(i + " tKey: <" + key + ">, action name: "
                    + actionName);
                }
            }
        }


    Action map's length is: 64
    0 tName: <caret-end-paragraph>, action: javax.swing.text.DefaultEditorKit$EndParagraphActi on
    1 tName: <delete-previous-word>, action: com.apple.laf.AquaKeyBindings$DeletePrevWordAction
    Exception in thread "main" java.lang.NullPointerException
    2 tName: <beep>, action: javax.swing.text.DefaultEditorKit$BeepAction
    3 tName: <aqua-page-up>, action: com.apple.laf.AquaKeyBindings$AquaMultilineAction
    4 tName: <aqua-move-up>, action: com.apple.laf.AquaKeyBindings$AquaMultilineAction
    5 tName: <selection-end>, action: javax.swing.text.DefaultEditorKit$EndAction
    6 tName: <selection-page-left>, action: javax.swing.text.DefaultEditorKit$PageAction
    7 tName: <cut-to-clipboard>, action: javax.swing.text.DefaultEditorKit$CutAction
    8 tName: <caret-end-word>, action: javax.swing.text.DefaultEditorKit$EndWordAction
    9 tName: <insert-tab>, action: javax.swing.text.DefaultEditorKit$InsertTabAction
    10 tName: <selection-end-paragraph>, action: javax.swing.text.DefaultEditorKit$EndParagraphActi on
    11 tName: <selection-previous-word>, action: javax.swing.text.DefaultEditorKit$PreviousWordActi on
    12 tName: <insert-break>, action: javax.swing.plaf.basic.BasicTextUI$TextActionWrapp er
    13 tName: <caret-begin-line>, action: javax.swing.text.DefaultEditorKit$BeginLineAction
    14 tName: <caret-begin-paragraph>, action: javax.swing.text.DefaultEditorKit$BeginParagraphAc tion
    15 tName: <selection-backward>, action: javax.swing.text.DefaultEditorKit$NextVisualPositi onAction
    16 tName: <delete-next-word>, action: com.apple.laf.AquaKeyBindings$DeleteNextWordAction
    17 tName: <caret-begin-word>, action: javax.swing.text.DefaultEditorKit$BeginWordAction
    18 tName: <insert-content>, action: javax.swing.text.DefaultEditorKit$InsertContentAct ion
    19 tName: <copy>, action: javax.swing.TransferHandler$TransferAction
    20 tName: <caret-end-line>, action: javax.swing.text.DefaultEditorKit$EndLineAction
    21 tName: <selection-begin>, action: javax.swing.text.DefaultEditorKit$BeginAction
    22 tName: <selection-page-down>, action: javax.swing.text.DefaultEditorKit$VerticalPageActi on
    at swing.DisplayKeymaps.printInputMap(DisplayKeymaps. java:26)
    at swing.DisplayKeymaps.main(DisplayKeymaps.java:86)
    23 tName: <default-typed>, action: javax.swing.text.DefaultEditorKit$DefaultKeyTypedA ction
    24 tName: <unselect>, action: javax.swing.text.DefaultEditorKit$UnselectAction
    25 tName: <select-paragraph>, action: javax.swing.text.DefaultEditorKit$SelectParagraphA ction
    26 tName: <caret-up>, action: javax.swing.text.DefaultEditorKit$NextVisualPositi onAction
    27 tName: <caret-down>, action: javax.swing.text.DefaultEditorKit$NextVisualPositi onAction
    28 tName: <caret-previous-word>, action: javax.swing.text.DefaultEditorKit$PreviousWordActi on
    29 tName: <selection-down>, action: javax.swing.text.DefaultEditorKit$NextVisualPositi onAction
    30 tName: <selection-begin-line>, action: javax.swing.text.DefaultEditorKit$BeginLineAction
    31 tName: <page-up>, action: javax.swing.text.DefaultEditorKit$VerticalPageActi on
    32 tName: <requestFocus>, action: javax.swing.plaf.basic.BasicTextUI$FocusAction
    33 tName: <selection-end-line>, action: javax.swing.text.DefaultEditorKit$EndLineAction
    34 tName: <selection-page-right>, action: javax.swing.text.DefaultEditorKit$PageAction
    35 tName: <paste>, action: javax.swing.TransferHandler$TransferAction
    36 tName: <copy-to-clipboard>, action: javax.swing.text.DefaultEditorKit$CopyAction
    37 tName: <page-down>, action: javax.swing.text.DefaultEditorKit$VerticalPageActi on
    38 tName: <select-word>, action: javax.swing.text.DefaultEditorKit$SelectWordAction
    39 tName: <set-read-only>, action: javax.swing.text.DefaultEditorKit$ReadOnlyAction
    40 tName: <paste-from-clipboard>, action: javax.swing.text.DefaultEditorKit$PasteAction
    41 tName: <selection-next-word>, action: javax.swing.text.DefaultEditorKit$NextWordAction
    42 tName: <delete-next>, action: javax.swing.text.DefaultEditorKit$DeleteNextCharAc tion
    43 tName: <aqua-move-down>, action: com.apple.laf.AquaKeyBindings$AquaMultilineAction
    44 tName: <selection-begin-word>, action: javax.swing.text.DefaultEditorKit$BeginWordAction
    45 tName: <dump-model>, action: javax.swing.text.DefaultEditorKit$DumpModelAction
    46 tName: <selection-begin-paragraph>, action: javax.swing.text.DefaultEditorKit$BeginParagraphAc tion
    47 tName: <caret-backward>, action: javax.swing.text.DefaultEditorKit$NextVisualPositi onAction
    48 tName: <set-writable>, action: javax.swing.text.DefaultEditorKit$WritableAction
    49 tName: <java.lang.Object@811978b>, action: javax.swing.text.DefaultEditorKit$DefaultKeyTypedA ction
    50 tName: <caret-end>, action: javax.swing.text.DefaultEditorKit$EndAction
    51 tName: <caret-begin>, action: javax.swing.text.DefaultEditorKit$BeginAction
    52 tName: <toggle-componentOrientation>, action: javax.swing.text.DefaultEditorKit$ToggleComponentO rientationAction
    53 tName: <delete-previous>, action: javax.swing.text.DefaultEditorKit$DeletePrevCharAc tion
    54 tName: <select-all>, action: javax.swing.text.DefaultEditorKit$SelectAllAction
    55 tName: <selection-end-word>, action: javax.swing.text.DefaultEditorKit$EndWordAction
    56 tName: <caret-next-word>, action: javax.swing.text.DefaultEditorKit$NextWordAction
    57 tName: <selection-forward>, action: javax.swing.text.DefaultEditorKit$NextVisualPositi onAction
    58 tName: <selection-page-up>, action: javax.swing.text.DefaultEditorKit$VerticalPageActi on
    59 tName: <caret-forward>, action: javax.swing.text.DefaultEditorKit$NextVisualPositi onAction
    60 tName: <selection-up>, action: javax.swing.text.DefaultEditorKit$NextVisualPositi onAction
    61 tName: <aqua-page-down>, action: com.apple.laf.AquaKeyBindings$AquaMultilineAction
    62 tName: <cut>, action: javax.swing.TransferHandler$TransferAction
    63 tName: <select-line>, action: javax.swing.text.DefaultEditorKit$SelectLineAction


    Input map used when focused (keys length 82)

    etc.

  2. #2
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: text components and key and input maps

    Might that have to do with the main thread and event thread.....
    I read about that some where....

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: text components and key and input maps

    BTW why the site was down, anybody knows?
    Last edited by willemjav; 10-25-2013 at 08:38 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: text components and key and input maps

    On your printInputMap method, you first check that keys is null before you attempt to use it. However,
    when you assign keys.length to x, you are not checking for null and trying to use it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: text components and key and input maps

    Thanks I will add the check

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: text components and key and input maps

    When running this code I get most key maps empty (only the first two work), why is that so?


    Java Code:
    import java.awt.event.KeyEvent;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.*;
    import javax.swing.text.*;
    
    
    /**
     *
     * @author willem
     */
    public class KeymapStuff {
    
    
        public static void main(String arg[])  {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JTextComponent t = null;
                    Class cl = null;
                        try {
                            cl = Class.forName("javax.swing.JTextArea");
                            Object o = null;
                            try {
                                 o = cl.newInstance();
                            }
                            catch (InstantiationException ex) {
                                Logger.getLogger(KeymapStuff.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            catch (IllegalAccessException ex) {
                                Logger.getLogger(KeymapStuff.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            if (o instanceof JTextComponent) {
                                t = (JTextComponent)o;
                            }
                            }
                            catch (ClassNotFoundException ex) {
                                Logger.getLogger(KeymapStuff.class.getName()).log(Level.SEVERE, null, ex);
                            }
        
                            if (t != null && cl != null) {
                                // the action map
                                ActionMap actionMap = t.getActionMap();
                                Object[] keys = actionMap.allKeys();
                                System.out.println(" the actionmap: " + actionMap);
    
                                if (keys != null ) {
                                    System.out.println(" the actionmap all keys: " + keys
                                            + " //all keys length: " + keys.length);
                                    System.out.println(); System.out.println();
                                }
                                else {
                                    System.out.println("// keys = null");
                                    System.out.println(); System.out.println();
                                }
    
                                // the input map WHEN_FOCUSED
                                InputMap inputMap =  t.getInputMap(JComponent.WHEN_FOCUSED);
                                KeyStroke[] keystroke = inputMap.allKeys();
                                System.out.println(" the inputmap(WHEN_FOCUSED): " + inputMap);
    
                                if (keystroke != null) {
                                    System.out.println(" the inputmap all keys: " + keystroke
                                        + " //all keys length: " + keystroke.length);
                                    System.out.println(); System.out.println();
                                }
                                else {
                                    System.out.println("// keys = null");
                                    System.out.println(); System.out.println();
                                }
    
                                //the imput map WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
                                inputMap =  t.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
                                keystroke = inputMap.allKeys();
                                System.out.println(" the inputmap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT): "
                                        + inputMap);
    
                                if (keystroke != null) {
                                    System.out.println(" the inputmap all keys: " + keystroke 
                                            + " //all keys length: " + keystroke.length);
                                    System.out.println(); System.out.println();
                                }
                                else {
                                    System.out.println("// // inputmap all keys = null");
                                    System.out.println(); System.out.println();
                                }
    
                                //the imput map WHEN_IN_FOCUSED_WINDOW
                                inputMap =  t.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                                keystroke = inputMap.allKeys();
                                System.out.println(" the inputmap(WHEN_IN_FOCUSED_WINDOW): " + inputMap);
    
                                if (keystroke != null ) {
                                    System.out.println(" the inputmap all keys: " + keystroke
                                           + " //all keys length: "+ keystroke.length);
                                    System.out.println(); System.out.println();
                                }
                                else {
                                    System.out.println("// inputmap all keys = null");
                                    System.out.println(); System.out.println();
                                }
                                // the keymap and keyevents
                                Keymap map = t.getKeymap();
                                keystroke = map.getBoundKeyStrokes();
                                System.out.println(" the keymap: " + map);
    
                                if (keystroke != null) {
                                    System.out.println(" // the keymap length is: " + keystroke.length);
                                    for (int i = 0; i < keystroke.length; i++) {
                                        System.out.println(" getmodifiers: " +
                                            KeyEvent.getKeyModifiersText(keystroke[i].getModifiers()));
                                    }
                                }
                                
                                System.out.println(); System.out.println();
                                }
                                else {
                                    System.out.println("error t is: " + t);
                                    System.out.println("error cl is: " + cl);
                                }
                     }
    
                 });
          }
    }
    the output on my computer:


    the actionmap: javax.swing.ActionMap@1c5dd582
    the actionmap all keys: [Ljava.lang.Object;@546c585a //all keys length: 64


    the inputmap(WHEN_FOCUSED): javax.swing.InputMap@151ca803
    the inputmap all keys: [Ljavax.swing.KeyStroke;@c5a67c9 //all keys length: 82


    the inputmap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT): javax.swing.InputMap@353c375
    // // inputmap all keys = null


    the inputmap(WHEN_IN_FOCUSED_WINDOW): javax.swing.ComponentInputMap@50618d26
    // inputmap all keys = null


    the keymap: Keymap[AquaTextAreaUI]{}
    // the keymap length is: 0


    BUILD SUCCESSFUL (total time: 4 seconds)
    Last edited by willemjav; 10-27-2013 at 02:07 PM.

Similar Threads

  1. checking text input
    By willemjav in forum AWT / Swing
    Replies: 14
    Last Post: 03-26-2013, 09:59 PM
  2. text components textbooks
    By willemjav in forum New To Java
    Replies: 8
    Last Post: 02-02-2013, 01:55 PM
  3. Replies: 1
    Last Post: 11-14-2011, 01:03 PM
  4. End Scanner int input with text value
    By BillyB in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 05:13 PM
  5. change text size in all GUI components
    By itaipee in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2009, 04:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •