// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class MainFrame extends JFrame implements ActionListener {
    private JTextField tf;
    public MainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        setTitle("Dialog demo");
        tf = new JTextField("**********");
        getContentPane().add(tf, BorderLayout.NORTH);
        getContentPane().add(new JLabel("This is a test"), BorderLayout.CENTER);
        JButton btn = new JButton("Open dialog");
        btn.addActionListener(this);
        getContentPane().add(btn, BorderLayout.SOUTH);
        setSize(400, 400);
    }
    public void setTF(String s) {
        tf.setText(s);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JDialog dia = new MyDialog(this);
        dia.setVisible(true);
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new MainFrame();
                f.setVisible(true);
            }
        });
    }
}

class MyDialog extends JDialog implements ActionListener {
    private MainFrame mf;
    private JTextField tf;
    private JButton ok;
    private JButton can;
    public MyDialog(MainFrame mf) {
        super(mf, true);
        this.mf = mf;
        getContentPane().setLayout(new BorderLayout());
        tf = new JTextField();
        getContentPane().add(tf, BorderLayout.NORTH);
        getContentPane().add(new JLabel("This is a test"), BorderLayout.CENTER);
        ok = new JButton("OK");
        ok.addActionListener(this);
        can = new JButton("Cancel");
        can.addActionListener(this);
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(1, 2));
        p.add(ok);
        p.add(can);
        getContentPane().add(p, BorderLayout.SOUTH);
        setSize(200, 200);
    }
    @Override
    public void actionPerformed(ActionEvent ev) {
        if(ev.getSource() == ok) {
            mf.setTF(tf.getText());
        }
        dispose();
    }
}
