Keyboard focus?

Sharky Forums


Results 1 to 3 of 3

Thread: Keyboard focus?

  1. #1
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Erie, PA, USA
    Posts
    693

    Keyboard focus?

    OK, Java gurus. I have a swing application, one JFrame with two JPanels. One JPanel is a sort of drawing area, the other has some JButtons and TextFields and stuff.

    Now my problem is, I want to use the spacebar to do some things in the drawing area.

    In the constructor of my JFrame I set up the JPanels and do this...

    this.requestFocus(); // get keyboard focus
    drawingArea.requestFocusInWindow(); // drawingArea is a JPanel

    But when I hit the space bar, the only thing that happens is an ActionEvent is fired on the default button in the OTHER JPanel!

    Obviously I am setting up the keyboard focus wrong somehow, but I can't find any decent tutorials on how to set the focus for a Panel (plenty of tut's for textboxes and such but none for panels). Any ideas?
    AMD AthlonXP 2600+ Thoroughbred B @ 200x10.5
    Shuttle AN35N nForce2 Ultra 400
    2x512MB Kingston PC3200 (3-3-3)
    ATI Radeon 9600 Pro
    40GB WD ATA-100 8MB cache
    Creative 12X DVD Drive
    Memorex 52X CD-RW
    Running Windows XP Pro

  2. #2
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Erie, PA, USA
    Posts
    693
    Nevermind, for those who are interested, one way to do it is to override the isFocusTraversable() method in your custom JPanel class to make it return true. Compiler tells me this is deprecated though, so I guess I still haven't found the "correct" way to do it.
    AMD AthlonXP 2600+ Thoroughbred B @ 200x10.5
    Shuttle AN35N nForce2 Ultra 400
    2x512MB Kingston PC3200 (3-3-3)
    ATI Radeon 9600 Pro
    40GB WD ATA-100 8MB cache
    Creative 12X DVD Drive
    Memorex 52X CD-RW
    Running Windows XP Pro

  3. #3
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    FYI, we're a pretty good resource here at Sharky's. But if you run into tough Java issues or need another place to search for answers, check out Sun's Java forums.

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

Posting Permissions

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