CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2001
    Location
    Wuerzburg, Germany
    Posts
    90

    KeyListener and JDK 1.4

    Hello,

    I use a JDialog and added a KeyListener like that:

    Code:
        this.addKeyListener(new java.awt.event.KeyAdapter()
        {
          public void keyPressed(KeyEvent e)
          {
            this_keyPressed(e);
          }
        });
    Under JDK1.3 I got all events from child components there. But under JDK1.4 I get no longer any event.
    I tried with an KeyEventDispatcher, but I doesn't work. Must I use InputMap?
    Any example code?

    Thanks

    Michael

  2. #2
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    afaicr, someone else tried this recently..

    hangon while i find the thread...
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    http://www.codeguru.com/forum/showth...ht=Timer+focus is what i was thinking of.. it's not quite the same problem.. let me know if it provides any inspiration
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Jun 2001
    Location
    Wuerzburg, Germany
    Posts
    90
    Hi,

    thanks a lot. I think it could work that way - but its not the best way.

    Michael

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    why not? why would you want to send every event to one component? what then.. a massive if else if else if else if to decide what to do?

    I know that microsoft windows works that way (basically just a massive switch loop that processes the messages in non-blocking style) but it doesnt mean it's the best way.. infact.. if you think that before IRQ came along, the CPU had to check every card and port hundreds of times a second, to see if it was needing attention, it becomes clear that "one thing that does everything" is a trend we are moving away from..

    but, if you want to do it that way, you can simply implement a simple keylistener that defers all events to a single component, then use a for loop to iterate the contents of your jframe (getComponents() method of ancestor Component) and add the listener to every one of them..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    probably something like this:

    Code:
    Component[] c = jframe.getContentPane().getComponents();
    MyDeferringKeyListener mdkl = new MyDeferringKeyListener();
    for(int i=0; i<c.length; i++){
      if c.isFocusable() //non focusable probably dont emit keyevents
        c.addKeyListener(mkdl);
    }
    it's lame though.. UI design should have a lot of effort put into it, because it is what the user interacts with.. do whatever you like that is hidden, but be consistent and sensible when working with the user
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    Join Date
    Jun 2001
    Location
    Wuerzburg, Germany
    Posts
    90
    Hi,

    I nead it e.g. for CTRL-S -> Save or ESC -> Exit the dialog.

    I did it that way:

    and it works fine.

    Code:
    public class LoginDialog extends JDialog implements KeyEventDispatcher
    …
      private void jbInit() throws Exception
      {
        KeyboardFocusManger.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
    …
    }
    public boolean dispatchKeyEvent(KeyEvent e)
      {
        if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
        {
          //do something
        }
      return true; // true if let other eventdispatcher work with that event, false if not
      }
    Michael

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

Featured