import javax.swing.*; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; import java.io.*; // bring in all the stream i/o stuff import java.text.*; public class IOSample extends JFrame{ // inherit JFrame StudentList studentList = new StudentList(); /** * This Constructor sets up the window and creates a simple menu */ public IOSample(String[] arguments) { // // setup the frames banner/title bar // super("IOSample"); // call JFrame's constructor and give it a title setSize(500,300); // set the width of the window to 500 pixels, height to 300. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit the whole program when this frame is closed // // Create a simple menu // JMenuBar mainMenu = new JMenuBar(); // create a menubar JMenu fileMenu = new JMenu("File"); // Create the file menu fileMenu.setMnemonic(KeyEvent.VK_F); // set its Alt character sequence addMenuItem(fileMenu, "Open", KeyEvent.VK_O, KeyEvent.VK_O,KeyEvent.CTRL_MASK,new FileOpenMenuListener()); addMenuItem(fileMenu, "Report", KeyEvent.VK_R, KeyEvent.VK_R,KeyEvent.CTRL_MASK,new FileReportMenuListener()); fileMenu.addSeparator(); // add a separator between open and exit addMenuItem(fileMenu, "Exit", KeyEvent.VK_X,KeyEvent.VK_X,KeyEvent.CTRL_MASK,new FileExitMenuListener()); mainMenu.add(fileMenu); // add the file menu to the main menu bar setJMenuBar(mainMenu); // // if the first argument exists, use it to load the studentlist // getContentPane().add("Center",studentList); // place studentList on the content pane if(arguments.length > 0) { try{ studentList.load(arguments[0]); setTitle(arguments[0]); } catch(IOException e) { setTitle(""); JOptionPane.showMessageDialog(this,"Unable to load file : "+arguments[0], "IOException",JOptionPane.ERROR_MESSAGE); // popup an error message } } } /** * This routine creates and adds a menu item to a menu * @param menu reference to a JMenu object to add this item to * @param text text string to appear on the menu * @param mnemonic virtual key to use as the mnemonic for the item e.g. KeyEvent.VK_F * @param acceleratorKey virtual key to use for the keyboard accelerator, e.g. KeyEvent.VK_O * @param acceleratorModifier virtual mask to use as a modifier, eg. KeyEvent.CTRL_MASK * @param listener reference to an object that implements an ActionListener */ void addMenuItem(JMenu menu, String text, int mnemonic, int acceleratorKey, int acceleratorModifier, ActionListener listener) { JMenuItem anItem; anItem = new JMenuItem(text,mnemonic); // create a menu item. anItem.setAccelerator(KeyStroke.getKeyStroke(acceleratorKey,acceleratorModifier)); // define accelerator for open anItem.addActionListener(listener); // tell menuitem what object will listen for it. menu.add(anItem); // add the item to the menu } /********************************* * Main routine */ public static void main(String[] arguments){ IOSample fr = new IOSample(arguments); // create the sample 2 frame fr.setVisible(true); // allow it to be seen } /******************************************************************* * Inner Classes, one acts as a listener for each user menu item * */ /** * Handle the File-Exit menu item */ class FileExitMenuListener implements ActionListener{ public void actionPerformed(ActionEvent event) { System.exit(0); } } /** * Handle the File-Open menu item */ class FileOpenMenuListener implements ActionListener{ public void actionPerformed(ActionEvent event) { String input; JFileChooser dialog = new JFileChooser(); // create the chooser object int result = dialog.showOpenDialog(IOSample.this); // use Open for input file if(result == JFileChooser.APPROVE_OPTION) { input = dialog.getSelectedFile().getPath(); try{ studentList.load(input); setTitle(input); } catch(IOException e) { JOptionPane.showMessageDialog(IOSample.this,"Unable to load file : "+input, "IOException",JOptionPane.ERROR_MESSAGE); // popup an error message setTitle(""); } } else return; } } /** * Handle the File-Report menu item */ class FileReportMenuListener implements ActionListener{ public void actionPerformed(ActionEvent event) { String output; JFileChooser dialog = new JFileChooser(); // create the chooser object int result = dialog.showSaveDialog(IOSample.this); // use Save for output file if(result == JFileChooser.APPROVE_OPTION) { output = dialog.getSelectedFile().getPath(); studentList.summaryHTMLReport(output); } else return; } } }// end of the class