// 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.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class Formatting extends JFrame {
    private JFormattedTextField jftf;
    private JButton jbtn;
    public Formatting() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        setTitle("Formatting");
        jftf = new JFormattedTextField();
        jftf.setValue(new Foobar(177));
        getContentPane().add(jftf, BorderLayout.CENTER);
        jbtn = new JButton("Test");
        jbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ((Foobar)jftf.getValue()).test();
            }
        });
        getContentPane().add(jbtn, BorderLayout.SOUTH);
        pack();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new Formatting();
                f.setVisible(true);
            }
        });
    }
}

class Foobar {
    private int v;
    public Foobar(int v) {
        this.v = v;
    }
    public int getV() {
        return v;
    }
    public void setV(int v) {
        this.v = v;
    }
    @Override
    public String toString() {
        return Integer.toString(v);
    }
    public void test() {
        JOptionPane.showMessageDialog(null, "v=" + v);
    }
}
