package planner.gui; import java.util.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import planner.gui.domain.*; // need access to Domain Objects // Applet is an ActionListener to handle the Domain Logic events // In this sense, Applet is both View and Controller in the MVC architecture public class PlannerApplet extends JApplet implements ActionListener { private GridBagLayout layout; // allows us to specify placement of widgets private GridBagConstraints constraints; // controls the actions of the layout manager private JTextField // list of all TextFields menuName = new JTextField(10); // one TextField for name of menu private JComboBox // list of all ComboBoxes menuList = new JComboBox(); // one ComboBox for a list of menu names private JRadioButton // list of all RadioButtons dinner = new JRadioButton("Dinner",true);// one RadioButton for Dinner (MenuType) private JTextArea ta = new JTextArea(20,40); // just one TextArea for entire application private MenuBook mb = new MenuBook(); // root object of the hierarchy // constructor populates initial data public void init() { // called by main or html layout = new GridBagLayout(); // new layout manager, one of the more powerful setLayout(layout); // this manager will be used for applet constraints = new GridBagConstraints(); // to control the manager constraints.fill = GridBagConstraints.HORIZONAL; // to eliminate narrow fields constraints.insets = new Insets(4,2,4,2); // to add spacing around the widgets addComponent(new JLabel("MENU BOOK"),0,1,1,1); // add Label at row 0, col 1, width 1 cell, height 1 cell // see below for implementation of private addComponent() addComponent(menuName,2,2,1,1); // add TextField to grid JButton showMenuBook = new JButton("Show Book"); // create new Button, can be local, not needed elsewhere addComponent(showMenuBook,0,2,1,1); // add Button to grid // TECHNIQUE #1: Respond to an Event using Applet's actionPerformed() showMenuBook.addActionListener(this); // PlannerApplet "is a" ActionListener and // has a ActionPerformed() method below // "this" is the PlannerApplet itself and // and ActionPerformed will be "callbacked" when // showMenuBook Button is pushed. // Domain Logic, to process this event, will be // implemented in ActionPerformed() (Ex7d) /* //The first way to respond to a Button is the addActionListener(this) above //Here are two more ways of introducing Domain Logic to handle events: // TECHNIQUE #2: Respond to an Event using an INNER class // this is an inner class, specific for one type of action // the code would need one inner class per each possible action // this shows the Domain Logic and GUI as tightly "coupled" // the advantage to any of these approaches is that Domain Logic // can "see" the widgets and operate on them, e.g. "ta", the TextArea class MenuBookListener implements ActionListener { // knows how to respond public void actionPerformed(ActionEvent e) { // must have this method ta.append(mb.toString()); // append the full MenuBook to the ta } } MenuBookListener mbl = new MenuBookListener(); // instantiate this inner class showMenuBook.addActionListener(mbl); // identify this instance as the listener for the button showMenuBook.addActionListener(new MenuBookListener()); // instead: just new the inner class right here // TECHNIQUE #3: Respond to an Event using an ANONYMOUS INNER class showMenuBook.addActionListener( new ActionListener() { // new one without a name public void actionPerformed(ActionEvent e) { // and give the same code as above ta.append(mb.toString()); } } ); */ ButtonGroup bg = new ButtonGroup(); // need to organize the RadioButtons bg.add(dinner); // add one RadioButton to the group addComponent(dinner,1,0,1,1); // place the RadioButton on the grid menuList.addItem("Example menu name"); // add one item to the ComboBox, which lists menu names // how are you going to find all the menu names? addComponent(menuList,1,2,1,1); // add ComboBox to the grid JPanel p = new JPanel(); // need a JPanel for a simpler layout manager p.setLayout(new FlowLayout()); // this layout manager seems to work better with TextAreas // this will always generate scroll bars, however, we'll let the Pane do it only if necessary //JScrollPane scroll = new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); JScrollPane scroll = new JScrollPane(ta); // Pane will determine scrolling, give it the TextArea p.add(scroll); // give scroll to the Panel p.setSize(400,300); // set size of Panel addComponent(p,5,1,5,1); // add panel to the grid, row 5, col 1, width 5 cells, height 1 cell } // helper function to add components to the grid private void addComponent(Component component, // the Component to add int row, // add Component at cell row 0..? int column, // add Component at cell column 0..? int width, // let the Component span this many cells wide int height) { // let the Component span this many cells high constraints.gridx = column; // store the four parameters in the constraints constraints.gridy = row; constraints.gridwidth = width; constraints.gridheight = height; layout.setConstraints(component, constraints); // set the constraints on the layout manager add(component); // before add the actual component to the Applet } // PlannerApplet implements ActionListener // Here is the required method public void actionPerformed(ActionEvent e) { String cmd = (String)e.getActionCommand(); if (cmd.equals("Show Book")) // what event just occurred? ta.append(mb.toString()); // display entire MenuBook to ta // the other button events will be handled here in Ex7d } }