Example usage for javax.swing JDialog requestFocus

List of usage examples for javax.swing JDialog requestFocus

Introduction

In this page you can find the example usage for javax.swing JDialog requestFocus.

Prototype

public void requestFocus() 

Source Link

Document

Requests that this Component get the input focus, and that this Component's top-level ancestor become the focused Window.

Usage

From source file:Main.java

public static void main(final String[] args) {
    JFrame frame = new JFrame("Frame");
    JDialog dialog = new JDialog(frame, "Dialog");
    frame.add(new JLabel("Content"));
    frame.addMouseListener(new MouseAdapter() {
        @Override/*from   w w  w .java 2 s.c  o m*/
        public void mousePressed(MouseEvent arg0) {
            System.out.println("frame pressed");
            System.out.println("dialog focused " + dialog.isFocused());
            System.out.println("frame focused " + frame.isFocused());
            super.mousePressed(arg0);
        }
    });
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    dialog.add(new JLabel("Content"));
    dialog.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent arg0) {
            super.focusLost(arg0);
            dialog.requestFocus();
        }
    });
    dialog.pack();
    dialog.setLocationRelativeTo(frame);
    dialog.setVisible(true);
}