import java.awt.Frame; import java.awt.Panel; import java.awt.TextField; import java.awt.Choice; import java.awt.List; import java.awt.Label; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ItemListener; import java.awt.event.WindowListener; import java.awt.event.ActionEvent; import java.awt.event.WindowEvent; import java.awt.event.ItemEvent; public class FindActor extends Frame implements ActionListener, ItemListener, WindowListener { public static void main(String args[]){ new FindActor(); } private TextField firstNames, lastName; private Choice store; private List moviesDisplay; private FindActorController controller = new FindActorController(); private FindActor(){ setTitle("Find in-stock movies with actor"); setLayout(new BorderLayout()); addWindowListener(this); moviesDisplay = new List(); add(moviesDisplay, "Center"); Panel p = new Panel(); p.setLayout(new FlowLayout()); p.add(new Label("Actor's first name(s):", Label.RIGHT)); firstNames = new TextField(20); firstNames.addActionListener(this); p.add(firstNames); p.add(new Label("Last name:", Label.RIGHT)); lastName = new TextField(20); lastName.addActionListener(this); p.add(lastName); p.add(new Label("Store:", Label.RIGHT)); store = new Choice(); store.add("St. Peter"); store.add("Mankato"); store.addItemListener(this); p.add(store); add(p, "North"); setSize(800,250); show(); } public void windowActivated(WindowEvent evt){} public void windowClosed(WindowEvent evt){} public void windowDeactivated(WindowEvent evt){} public void windowDeiconified(WindowEvent evt){} public void windowIconified(WindowEvent evt){} public void windowOpened(WindowEvent evt){} public void windowClosing(WindowEvent evt){ System.exit(0); } public void actionPerformed(ActionEvent evt){ updateResults(); } public void itemStateChanged(ItemEvent evt){ updateResults(); } private void updateResults(){ moviesDisplay.removeAll(); String[] movieNames = controller.getMovies (firstNames.getText(), lastName.getText(), store.getSelectedItem()); for(int i = 0; i < movieNames.length; i++){ moviesDisplay.add(movieNames[i]); } } } class FindActorController { FindActorController() { // Empty constructor for now: // current "dummy" version has nothing to initialize, // but a real version might establish a database connection, // prepare a statement, etc. } String[] getMovies(String firstNames, String lastName, String storeName){ // retrieve the relevant movies from the database and // return an array of Strings containing them; below // sample code shows an easy way to make the array without // knowing in advance how big it is going to be java.util.Vector v = new java.util.Vector(); v.addElement("Sample Movie 1"); // instead of these two lines, loop v.addElement("Sample Movie 2"); // adding each query result to v String[] array = new String[v.size()]; v.copyInto(array); return array; } }