• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ask some questions about FocusListener

 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
focusGained and focusLost hasnot been triggered.why? Some who can tell me how to do.Thanked first.

code:
class JBasePanel extends JPanel{

public JBasePanel(){
//add JMyPanel
add(new JMyPanel());

}

}

class JMyPanel extends JPanel{

................// variable

public JMyPanel(){
................ //some initialize code
//add Event
this.addFocusListener(new FocusListener(){

public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,"have")
}

public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(null,"havn't");
}

});

}

}
 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your enthusiasm is driving me.
thanks. thansks!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPanels by default are focusable, so either override isFocusable or call setFocusable:
1. I'm using System.out, not dialogs, since a dialog's display would cause
and endless triggering of focus gained and lost!
2. Why do you want a JPanel to be focusable? It's usually user-input
controls, not containers that can get focus.
 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uses setFocusable (true) the method not to achieve the anticipatedeffect.
focusGained and focusLost hasnot been triggered.JMyPanel is joined to other vessel in.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sincd my example does work, I suggest you post a minimal complete program demonstrating your problem.
 
cat cater
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First,I thank your help extremely
But JPanel only then time obtains the focal point the opportunity. I want to press the Jpanel and get focal.. JButton and other input components May duplicate obtains the focal point. Has any method to be possible to change this kind of aspect?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I *really* didn't understand that last post. Again, if you could post a
minimal example program that demonstrates your problem, that would go a
long way in helping forums members help you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic