Results 1 to 2 of 2

Thread: Actions and KeyStrokes and JTable Help

  1. #1

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104

    Actions and KeyStrokes and JTable Help

    Code:
        Action enterSelection = new AbstractAction("ENTER"){
          public void actionPerformed(ActionEvent e){
           SearchInterface search = (SearchInterface) table.getModel();
           if (table.getSelectedRow() == -1 ){
             tfFind.requestFocus();
           } else {
             search.selectRow(table.getSelectedRow());
             closeDialog();
           }
          }
        };
    
        table.getInputMap(JComponent.WHEN_FOCUSED).put(
          KeyStroke.getKeyStroke("ENTER"), enterSelection.NAME);
        table.getActionMap().put(enterSelection.NAME, enterSelection);
        btnOK.addActionListener(enterSelection);
    
        Action cancelAction = new AbstractAction("CANCEL"){
          public void actionPerformed(ActionEvent e){
           SearchInterface search = (SearchInterface) table.getModel();
           search.setCancelled(true);
           closeDialog();
          }
        };
        contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
            KeyStroke.getKeyStroke("ESCAPE"), cancelAction.NAME);
        contentPane.getActionMap().put(cancelAction.NAME, cancelAction);
    
        table.getInputMap(JComponent.WHEN_FOCUSED).put(
            KeyStroke.getKeyStroke("ESCAPE"), cancelAction.NAME);
        table.getActionMap().put(cancelAction.NAME, cancelAction);
        btnCancel.addActionListener(cancelAction);
    
    As you can see the code illustrated above creates two Action objects one is for entering and the other is for cancelling. The part of the code which is made bold is giving me problems.

    table object has two keystrokes associated with it one is for escape the other is for entering. both of which should trigger when table is focused. But whenever I do either of the keystrokes only the latter action which is the cancel is executed.

    Question 1: Can there be only one keystroke associated with a component with the same condition (JComponent.WHEN_FOCUSED)?

  2. #2

    Thread Starter
    Banned debbie_82's Avatar
    Join Date
    Jan 2004
    Location
    inside a hollow void
    Posts
    104
    InputMap of an object serves as the one triggering the KeyStrokes right? and the corresponding value of a keystroke in the InputMap will serve as the key to the ActionMap of an object right?... thus triggering the value of the ActionMap which is the action object.
    Shouldn't it be ok for one component to have lots of keystrokes associated or registered with it?

    If anyone can explain to me the process how the action in the actionmap of an object get to be triggerred I'd be really happy..

    I'm quite new to all this and I'm having a bad headache already.

Posting Permissions

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



Click Here to Expand Forum to Full Width