Mega Code Archive

 
Categories / Java Tutorial / Swing
 

Handling JFrame Events

The JFrame class supports the registration of eleven different listeners: ComponentListener: To find out when the frame moves or is resized. ContainerListener: Normally not added to a JFrame because you add components to the content pane of its JRootPane. FocusListener: To find out when the frame gets or loses input focus. HierarchyBoundsListener: To find out when the frame moves or is resized. HierarchyListener: To find out when the frame is shown or hidden. InputMethodListener: To work with input methods for internationalization. MouseListener and MouseMotionListener: To listen for mouse and mouse motion events. PropertyChangeListener: To listen for changes to bound properties. WindowListener: To find out when a window is iconified, deiconified, closing, closed and so on. KeyListener: Normally not added to a JFrame. Instead, you register a keyboard action for its content pane, like this: JPanel content = (JPanel)frame.getContentPane();       KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);       content.registerKeyboardAction(actionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);