Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't Focus On JPanel  RSS feed

 
Jay Crosley
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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);
}
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!