import javax.swing.*; 
import java.awt.*; 

public class Main extends JFrame { 
    MyPanel panel; 

    public Main() { 
        setTitle("This is a frame"); 
        setSize(300, 200); 
        panel = new MyPanel(this); 
        add(panel); 

        setLocationRelativeTo(null); 
        setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    public static void main(String[] args) { 
        EventQueue.invokeLater(() -> { 
            Main frame = new Main(); 
            frame.pack(); 
            frame.setVisible(true); 
        }); 
    } 

    private static class MyPanel extends JPanel { 

        int dialogCounter = 1; 
        final JFrame theParent; 

        public MyPanel(JFrame parent) { 
            super(); 
            theParent = parent; 
            setPreferredSize(new Dimension(300, 200)); 
            JButton button = new JButton("Press the button"); 
            button.addActionListener(e -> showDialog(theParent)); 

            add(button); 
        } 

        private void showDialog(Frame parent) { 
            JDialog dialog = new JDialog(parent, "This is dialog " + dialogCounter, false); 
            setupDialog(dialog); 
        } 

        private void setupDialog(JDialog dialog) { 
            JPanel dialogPanel = new JPanel(); 
            dialogPanel.setPreferredSize(new Dimension(300, 200)); 
            dialogPanel.add(new JLabel("Current dialog count: " + dialogCounter++)); 
            JButton button = new JButton("Open a new modal dialog"); 
            button.addActionListener(e -> showDialog(theParent)); 
            dialogPanel.add(button); 
            dialog.add(dialogPanel); 
            dialog.pack(); 
            dialog.setModal(true); 
            dialog.setVisible(true); 
        } 
    } 
} 