// ModelController.java adapted by Dr. I. for CS 102 during Spring, 2003 // Simple controller class for Model-View-Controller import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ModelController extends JPanel { // Model to control private Model model; // JTextField for increment or decrement amount private JTextField amountTextField; // controller constructor public ModelController(Model controlledModel) { super(); // Model to control model = controlledModel; // create JTextField for entering amount amountTextField = new JTextField(10); // create JButton for increment JButton incrementButton = new JButton("Increment"); incrementButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { try { // increment data value by amount entered in amountTextField model.incrementValue(Integer.parseInt(amountTextField.getText())); } catch (NumberFormatException exception) { JOptionPane.showMessageDialog(ModelController.this, "Please enter a valid integer", "Error", JOptionPane.ERROR_MESSAGE ); } } } ); // create JButton for decrement JButton decrementButton = new JButton( "Decrement" ); decrementButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { try { // decrement data value by amount entered in amountTextField model.decrementValue(Integer.parseInt(amountTextField.getText())); } catch (NumberFormatException exception) { JOptionPane.showMessageDialog(ModelController.this, "Please enter a valid integer", "Error", JOptionPane.ERROR_MESSAGE ); } } } ); // lay out controller components setLayout( new FlowLayout() ); add( decrementButton ); add( new JLabel( "Amount: " ) ); add( amountTextField ); add( incrementButton ); } }