JPanel: FocusListener not working

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P2
    • None
    • Affects Version/s: 1.4.0
    • Component/s: client-libs

      Both the focusGained and focusLost methods do not work for JPanel.

      Code snippet:
      import javax.swing.JToolBar;
      import javax.swing.JButton;
      import javax.swing.ImageIcon;

      import javax.swing.JFrame;
      import javax.swing.JTextArea;
      import javax.swing.JScrollPane;
      import javax.swing.JPanel;
      import javax.swing.JTabbedPane;

      import java.awt.*;
      import java.awt.event.*;

      public class ToolBarDemo extends JFrame implements FocusListener {

          public ToolBarDemo() {
              //Do frame stuff.
              super("ToolBarDemo");
              addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });

              //Create the toolbar.
              //JToolBar toolBar = new JToolBar();
              //toolBar.addFocusListener(this);
              
              //Lay out the content pane.
              JPanel contentPane = new JPanel();
              contentPane.addFocusListener(this);
              contentPane.setLayout(new BorderLayout());
              contentPane.setPreferredSize(new Dimension(400, 100));
              //contentPane.add(toolBar, BorderLayout.CENTER);
              setContentPane(contentPane);
          }
          
          public void focusGained(FocusEvent e) {
            System.out.println("focusGained: " + e.getComponent());
          }
          
          public void focusLost(FocusEvent e) {
            System.out.println("focusLost: " + e.getComponent());
          }
          
          public static void main(String[] args) {
              ToolBarDemo frame = new ToolBarDemo();
              frame.pack();
              frame.setVisible(true);
          }
      }

            Assignee:
            Hania Gajewska (Inactive)
            Reporter:
            Sridhar Raman (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: