Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {
    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
            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);
    }
}