Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Can't Focus On JPanel

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JFrame that contains three panels: two panels for buttons and text boxes and one custom JPanel. The panels for the buttons and text boxes are north and south and the custom one is in the center. I need to get focus to my center panel for registering keystroke events, but the focus won't leave my other components, even if I click the center panel. I have MouseListeners set up for that panel which work, but even when I'm using them the text focus stays in the text boxes. The constructor for the frame is below. How do I get focus to the panel in the center? I've tried Key Bindings and those don't seem to work either because they still seem to need focus (as in the passed constant JComponent.WHEN_FOCUSED), or else I don't know how to make them where they don't need it. Even if you know a way, I would like to know how to get focus to my panel.

public TableClientFrame(ClientModel m) {
super(m);
// initial frame settings
frame = new JFrame("Client Frame");
frame.setSize(400, 400);
frame.setBackground(Color.WHITE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// the model
this.model = m;


// add the panel
panel = new TableClientPanel(model);
frame.add(panel, BorderLayout.CENTER);
m.addListener(panel);

frame.add(controlPanel(), BorderLayout.SOUTH);
frame.add(connectPanel(), BorderLayout.NORTH);
}
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried a two things here:

* Class FocusPanel maps keys when it has the focus.
* It has a MouseListener that requests focus when the panel is clicked

that's pretty much it. Please let me know if this does or doesn't help:

FocusPanel.java


FocusPanelTest.java
 
Jay Crosley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This worked perfectly. I'm still not sure why the JPanel wouldn't take the focus when I clicked it automatically like all the other components before I added a mouse listener, but I can use this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic