// 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.*;
import java.awt.event.*;
import javax.swing.*;

public class NewDynNum extends JFrame implements ActionListener {
    private JTextField txt;
    private JButton btn;
    private int n;
    public NewDynNum() {
        n = 0;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        txt = new JTextField();
        txt.setText(Integer.toString(n));
        getContentPane().add(txt, BorderLayout.CENTER);
        btn = new JButton("Count 10 up");
        btn.addActionListener(this);
        getContentPane().add(btn, BorderLayout.SOUTH);
        pack();
    }
    public void incr() {
        n++;
        txt.setText(Integer.toString(n));
    }
    public void actionPerformed(ActionEvent ev) {
        if (ev.getSource() == btn) {
            T t = new T(this);
            t.start();
        }
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                NewDynNum f = new NewDynNum();
                f.setVisible(true);
            }
        });
    }
}

class T extends Thread {
    private NewDynNum dn;
    public T(NewDynNum dn) {
        this.dn = dn;
    }
    public void run() {
        for (int i = 0; i < 10; i++) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    dn.incr();
                }
            });
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
    }
}
