• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending JPanel and Focus

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I've created a new class and called it gamePanel. It extends JPanel. I've added this panel to a frame, along with some buttons, in my main method. Essentially, by pressing buttons, it takes the focus off the panel which doesn't allow my key listeners (which were added to the panel) to work. Because gamePanel is a custom panel, and not a normal JPanel, using the method .requestFocusInWindow() doesn't work. so I made a method for my gamePanel object that when called upon will invoke the other method to give the focus back to the panel. However, it gives an error at the point in which my gamePanel object calls the method. So my ultimate question is, how can I get this working, or how can i return focus to my object?

If you need it, heres my code. Its long, and you should only need to look at lines 143 in "test" and line 21 in "gamePanel".


gamePanel class:

 
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
KeyBindings
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To elaborate, key bindings.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved the same probles only using
You can read more at https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic