
import java.lang.*; 
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

class MVC implements Runnable {
	public static void main(String[] args) {
	    System.out.format("### %d main.1 %n", Thread.currentThread().getId());
		
		try {
			SwingUtilities.invokeAndWait(new MVC());
		
		} catch (Exception ex) {
		    System.err.format("*** [%3d] %s %s %n", 
				Thread.currentThread().getId(), ex, ex.getMessage() );
		}
	    
		System.out.format("### %d main.2 %n", Thread.currentThread().getId());
	}

	// ====================================================================

	public void run() {
	    System.out.format("### %d run.1 %n", Thread.currentThread().getId());
		
		JFrame frame = new JFrame();
        frame.setTitle("MVC Table");
		frame.getContentPane().setLayout(new BorderLayout());
		
        Font font = new Font("Monospaced", Font.BOLD, 16);
        
		// prepare table model
		String[] columnNames = new String[] {"Name", "Age"};;
		Object[][] data = new Object[3][];
		data[0] = new Object[] { "Mano", 42};
		data[1] = new Object[] { "Radu", 52};
		data[2] = new Object[] { "Xinfeng", 42};
		final TableModel TM = new DefaultTableModel(data, columnNames);

        JButton UpdateButton = new JButton("Update");
        UpdateButton.setFont(font);
        UpdateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
				System.out.format("### %d actionPerformed.1 %n", 
					Thread.currentThread().getId());
                
				for (int row=0; row < TM.getRowCount(); row++) {
					int age = (int)TM.getValueAt(row, 1);
					TM.setValueAt(age+1, row, 1);
				}
				
				System.out.format("### %d actionPerformed.2 %n", 
					Thread.currentThread().getId());
            }
        });

        JPanel strip = new JPanel(new FlowLayout(FlowLayout.CENTER));
		strip.add(UpdateButton);
        frame.getContentPane().add(strip, BorderLayout.NORTH);

		// prepare table
        JTable T = new JTable(TM);
		T.setFont(font);
		JScrollPane SP = new JScrollPane(T);
		
        frame.getContentPane().add(SP, BorderLayout.CENTER);
		        
		JLabel StatusBar = new JLabel();
		StatusBar.setFont(font);
        frame.getContentPane().add(StatusBar, BorderLayout.SOUTH);
		
        frame.getRootPane().setDefaultButton(UpdateButton);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(250, 200);
        frame.setResizable(true);
        frame.setVisible(true);
	    
		System.out.format("### %d run.2 %n", Thread.currentThread().getId());
	}

	// ====================================================================
	
}
			