// File: DualModal.java
//
// Demonstrate a pair of non-modal dialogs

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DualModal {

    private JButton button1 = new JButton("Left");
    private JButton button2 = new JButton("Right");
    private final JFrame frame1 = new JFrame("Left");
    private final JFrame frame2 = new JFrame("Right");

    public static void main(String[] args) {
        new DualModal();
    }

    public DualModal() {
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.add(button1);
        frame2.add(button2);
        ActionListener listener = new ButtonHandler();
        button1.addActionListener(listener);
        button2.addActionListener(listener);
        frame1.setBounds(100, 100, 200, 200);
        frame1.setVisible(true);
        frame2.setBounds(400, 100, 200, 200);
        frame2.setVisible(true);
    }

    private class ButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton) e.getSource();
             String msg = (source == button1) ? "New Left Label" : "New Right Label";
            JOptionPane pane = new JOptionPane(msg, JOptionPane.QUESTION_MESSAGE);
            pane.setWantsInput(true);
            JDialog dialog = pane.createDialog(frame2, "Enter Text");
            // dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
            // dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
            dialog.setVisible(true);
            String text = (String) pane.getInputValue();

            if (!JOptionPane.UNINITIALIZED_VALUE.equals(text) && text.trim().length() > 0) {
                source.setText(text);
            }
        }

    }

}
