title: Simple Focus Listener in Java
body: <p>I have created a simple application with a Panel(extended by JPanel) which contains a small box(myMessagePanel - see the code below).</p>
  
  <p>Basically, I want to generate focusevents from both the Panel and the box when the box is pressed. However, only focusGained and focusLost in the Panel is called. Here is the code:</p>
  
  <p>myMessageBox is the box, Panel is the panel and FocusListenerTest contains the main</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  class Panel extends JPanel implements MouseListener, FocusListener {
  myMessageBox msgBox;
  
  public Panel() {
      addMouseListener(this);
      addFocusListener(this);
  }
  
  public void paintComponent(Graphics g) {
      super.paintComponent(g);
      setBackground(Color.GREEN);
      msgBox = new myMessageBox(g);
      g.dispose();
  }
  
  public void focusGained(FocusEvent fe) {
      System.out.println("Focus gained in JPanel");
  }
  
  public void focusLost(FocusEvent fe){
      System.out.println("Focus lost in JPanel");
  }       
  
  public void mousePressed(MouseEvent me) {
      requestFocus();
      System.out.println("Mouse Pressed in JPanel");
  }
  public void mouseReleased(MouseEvent me) {}
  public void mouseClicked(MouseEvent me) {}
  public void mouseEntered(MouseEvent me) {}
  public void mouseExited(MouseEvent me) {}
  }
  
  class myMessageBox extends Component implements FocusListener, MouseListener{
  myMessageBox(Graphics g) {
      addMouseListener(this);
      addFocusListener(this);
      paintComponent(g);
      System.out.println("Done");
  }
  
  public void paintComponent(Graphics g) {
      g.fillRect(150,100,100,100);
  }
  
  public void mousePressed(MouseEvent me) {
      requestFocus();
      System.out.println("Mouse pressed on box");
  }
  
  public void mouseReleased(MouseEvent me) {}
  public void mouseClicked(MouseEvent me) {}
  public void mouseEntered(MouseEvent me) {}
  public void mouseExited(MouseEvent me) {}
  
  public void focusGained(FocusEvent fe){
      System.out.println("Focus gained by box");
  }
  
  public void focusLost(FocusEvent fe) {
      System.out.println("Focus lost by box");
  }
  }
  
  public class FocusListenertest {
  public static void main(String args[]) {
      JFrame window = new JFrame();
      Panel content = new Panel();
      window.setContentPane(content);
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setLocation(100,75);
      window.setSize(400, 400);
      window.setVisible(true);
  }
  }
  </code></pre>
  
  <p>When I run this code, "Focus gained by box" is not printed when the myMessageBox is pressed. </p>
  
  <p>Thanks</p>
  
  <p>(PS: Didn't write comments as it is a simple application. Please let me know if comments are required)</p>