// File: Form.java (Module 11) // // Author: Rahul Simha // Created: November 4, 1998. // // Same as Form5.java. import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; class NewFrame extends Frame { // Data. Panel centerpanel; // For the questions. ScrollPane sc; // For placing the centerpanel. TextField lastname_tf; // To get the lastname. String lastname; TextField firstname_tf; // Firstname. String firstname; TextField networth_tf; // Net worth String networth; TextArea address_ta; // Address (TextArea) String address; MenuBar mb; // The menubar. PopupMenu pm; // The popup menu. // Constructor. public NewFrame (int width, int height) { this.setTitle ("Snoot Club Membership Form"); this.setResizable (true); this.setBackground (Color.white); this.setSize (width, height); // this.setLayout (new BorderLayout()); // First, a welcome message: Label L = new Label ("Please fill out the following form:"); L.setFont (new Font ("Serif", Font.BOLD | Font.ITALIC, 15)); L.setForeground (Color.blue); this.add (L, BorderLayout.NORTH); // Now the center panel. centerpanel = new Panel (); centerpanel.setLayout (new GridLayout (4,1)); centerpanel.add (first_name ()); centerpanel.add (last_name ()); centerpanel.add (net_worth ()); centerpanel.add (address ()); // Next, the scrollpane. sc = new ScrollPane (); sc.add (centerpanel); this.add (sc, BorderLayout.CENTER); // The menubar mb = new MenuBar (); // Add a "File" menu with two items. Menu file_menu = new Menu ("File"); file_menu.add ("Load"); file_menu.add ("LoadFromFile"); file_menu.add ("Quit"); file_menu.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { String item = a.getActionCommand (); if (item.equals ("Load")) load (); if (item.equals ("LoadFromFile")) load_new (); else if (item.equals ("Quit")) System.exit (0); } } ); mb.add (file_menu); // Add an "Action" menu with two items. Menu action_menu = new Menu ("Action"); action_menu.add ("Submit"); action_menu.add ("Clear"); action_menu.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { String item = a.getActionCommand (); if (item.equals ("Submit")) submit (); else if (item.equals ("Clear")) { firstname = ""; firstname_tf.setText (firstname); lastname = ""; lastname_tf.setText (lastname); networth = ""; networth_tf.setText (networth); address = ""; address_ta.setText (address); } } } ); mb.add (action_menu); // Note how a menubar is added to a Frame. this.setMenuBar (mb); // A pop-up menu pm = new PopupMenu (); pm.add ("Top"); pm.add ("Bottom"); pm.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { String item = a.getActionCommand (); if (item.equals ("Top")) top (); else if (item.equals ("Bottom")) bottom (); } } ); // We add this to the frame. this.add (pm); // Finally, show the frame. this.setVisible (true); } // No-parameter constructor. public NewFrame () { this (500, 300); } // Read in the first name. Panel first_name () { Panel subpanel = new Panel (); Label L = new Label ("First Name"); L.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L); // Listen for a Popup click. L.addMouseListener (getMouseListener (L)); firstname_tf = new TextField (20); firstname_tf.setForeground (Color.blue); firstname_tf.addTextListener ( new TextListener () { public void textValueChanged (TextEvent t) { firstname = firstname_tf.getText(); } } ); subpanel.add (firstname_tf); subpanel.addMouseListener (getMouseListener(subpanel)); return subpanel; } // Get last name. Panel last_name () { Panel subpanel = new Panel (); Label L = new Label ("Last Name"); L.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L); // Listen for a Popup click. L.addMouseListener (getMouseListener (L)); lastname_tf = new TextField (20); lastname_tf.setForeground (Color.blue); lastname_tf.addTextListener ( new TextListener () { public void textValueChanged (TextEvent t) { lastname = lastname_tf.getText(); } } ); subpanel.add (lastname_tf); subpanel.addMouseListener (getMouseListener(subpanel)); return subpanel; } // Get net worth. Panel net_worth () { Panel subpanel = new Panel (); Label L = new Label ("Net worth (in millions)"); L.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L); // Listen for a Popup click. L.addMouseListener (getMouseListener (L)); networth_tf = new TextField (10); networth_tf.setForeground (Color.blue); networth_tf.addTextListener ( new TextListener () { public void textValueChanged (TextEvent t) { networth = networth_tf.getText(); } } ); subpanel.add (networth_tf); subpanel.addMouseListener (getMouseListener(subpanel)); return subpanel; } // Get address via a TextArea. Panel address () { Panel subpanel = new Panel (); Label L = new Label ("Address"); L.setFont (new Font ("SansSerif", Font.ITALIC, 20)); subpanel.add (L); // Listen for a Popup click. L.addMouseListener (getMouseListener (L)); address_ta = new TextArea (4, 30); address_ta.setForeground (Color.blue); address_ta.addTextListener ( new TextListener () { public void textValueChanged (TextEvent t) { address = address_ta.getText(); } } ); subpanel.add (address_ta); subpanel.addMouseListener (getMouseListener(subpanel)); return subpanel; } // Process data when ready. void submit () { // Create a file by concatenating firstname and lastname. String filename = firstname + lastname; if (filename.length() == 0) return; // We will use a "Properties" instance to store data. Properties p = new Properties (); p.put ("firstname", firstname); p.put ("lastname", lastname); p.put ("networth", networth); p.put ("address", address); // Use the "save" feature of the Properties instance. try { FileOutputStream f = new FileOutputStream (filename); p.save (f, "Member information"); } catch (IOException e) { System.out.println (e); System.exit (0); } } // Load information from an existing Properties file. Properties info; // We will use a File instance. void load () { String dir = null; String filename = firstname + lastname; File file = new File (dir, filename); load (file); } Dialog error_dialog; void load (File file) { info = new Properties (); try { FileInputStream f = new FileInputStream (file); info.load (f); firstname = info.getProperty ("firstname"); firstname_tf.setText (firstname); lastname = info.getProperty ("lastname"); lastname_tf.setText (lastname); networth = info.getProperty ("networth"); networth_tf.setText (networth); address = info.getProperty ("address"); address_ta.setText (address); } catch (IOException e) { // Handle a file error more gracefully. error_dialog = new Dialog (this, true); error_dialog.setSize (200, 100); error_dialog.setBackground (Color.yellow); Label L = new Label ("File does not exist"); Button ok = new Button ("OK"); ok.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { error_dialog.dispose(); } } ); error_dialog.setLayout (new BorderLayout()); error_dialog.add (L, BorderLayout.CENTER); error_dialog.add (ok, BorderLayout.SOUTH); // Need to make it visible. error_dialog.setVisible (true); } } // Use a file dialog. void load_new () { // Create the FileDialog instance. FileDialog fd = new FileDialog (this, "File to load from", FileDialog.LOAD); // Note: need to make it visible. fd.setVisible (true); // When the user is done, get the info. String dir = fd.getDirectory (); String filename = fd.getFile (); String fullname = dir + filename; // Create a full filename. File file = new File (fullname); load (file); } // Create a Listener for popup's. MouseListener getMouseListener (Component c) { class PopupMouseListener extends MouseAdapter { Component c; public PopupMouseListener (Component c) { this.c = c; } public void mousePressed (MouseEvent m) { // We need this special check. if (m.isPopupTrigger ()) pm.show (c, m.getX(), m.getY()); // Note: this "show" method is not deprecated. } } return new PopupMouseListener (c); } // Handle the "go to top" event. void top () { sc.setScrollPosition (0,0); } // Handle the "go to bottom" event. void bottom () { int height = sc.getViewportSize().height; sc.setScrollPosition (0,height); } } public class Form { public static void main (String[] argv) { NewFrame nf = new NewFrame (500, 300); } }