Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Window;

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

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame(Main.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JButton button = new JButton("Click me to open dialog");
        button.addActionListener(e -> {
            Window parentWindow = SwingUtilities.windowForComponent(button);
            JDialog dialog = new JDialog(parentWindow);
            dialog.setLocationRelativeTo(button);
            dialog.setModal(true);
            dialog.add(new JLabel("A dialog"));
            dialog.pack();
            dialog.setVisible(true);
        });
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
    }
}