// Example of setting up pull-down Menus and JFileChooser using Swing Classes import java.io.*; // need this to do files import java.awt.event.*; // event processing requirements import javax.swing.*; // we will use Swing... class StudentDB extends JFrame implements ActionListener { JMenuBar mb; JMenuItem openItem, closeItem, exitItem; JMenuItem addFresh, addSoph, addJunior, addSenior, showAll, showSingle, deleteStudent; StudentDB() { mb = new JMenuBar(); JMenu m1 = new JMenu("File"); openItem = new JMenuItem("Open File"); closeItem = new JMenuItem("Close File"); exitItem = new JMenuItem("Exit"); m1.add(openItem); m1.add(closeItem); m1.add(exitItem); JMenu m2 = new JMenu("Commands"); JMenu m3 = new JMenu("Add"); addFresh = new JMenuItem("Add Freshman"); addSoph = new JMenuItem("Add Sophomore"); addJunior = new JMenuItem("Add Junior"); addSenior = new JMenuItem("Add Senior"); deleteStudent = new JMenuItem("Delete Student"); showSingle = new JMenuItem("Show Student"); showAll = new JMenuItem("Show All"); m3.add(addFresh); m3.add(addSoph); m3.add(addJunior); m3.add(addSenior); m2.add(m3); m2.add(deleteStudent); m2.add(showSingle); m2.add(showAll); mb.add(m1); mb.add(m2); setJMenuBar(mb); openItem.addActionListener(this); closeItem.addActionListener(this); exitItem.addActionListener(this); addFresh.addActionListener(this); addSoph.addActionListener(this); addJunior.addActionListener(this); addSenior.addActionListener(this); deleteStudent.addActionListener(this); showSingle.addActionListener(this); showAll.addActionListener(this); } public void actionPerformed (ActionEvent e) { if (e.getSource() == addFresh) { // Freshman selected... JOptionPane.showMessageDialog(null, "Freshman selected!!", "Add Student", JOptionPane.PLAIN_MESSAGE); } else if (e.getSource() == addSoph) { JOptionPane.showMessageDialog(null, "Sophomore selected!!", "Add Student", JOptionPane.PLAIN_MESSAGE); } else if (e.getSource() == addSenior) { JOptionPane.showMessageDialog(null, "Senior selected!!", "Add Student", JOptionPane.PLAIN_MESSAGE); } else if (e.getSource() == addJunior) { JOptionPane.showMessageDialog(null, "Junior selected!!", "Add Student", JOptionPane.PLAIN_MESSAGE); } else if (e.getSource() == deleteStudent) { JOptionPane.showMessageDialog(null, "Delete Student selected!!", "Delete Student", JOptionPane.PLAIN_MESSAGE); } else if (e.getSource() == showSingle) { JOptionPane.showMessageDialog(null, "Display (individual) selected!!", "Display Student", JOptionPane.PLAIN_MESSAGE); } else if (e.getSource() == showAll) { JOptionPane.showMessageDialog(null, "Display ALL Selected!!", "Show all", JOptionPane.PLAIN_MESSAGE); } else if (e.getSource() == exitItem) { setVisible(false); } else if (e.getSource() == openItem) { JFileChooser fc = new JFileChooser(); fc.setFileSelectionMode(JFileChooser.FILES_ONLY); int result = fc.showOpenDialog(this); if (result == JFileChooser.CANCEL_OPTION) JOptionPane.showMessageDialog(null, "You cancelled file selection", "Open File: ERROR", JOptionPane.ERROR_MESSAGE); else { File f = fc.getSelectedFile(); if ( f == null || f.getName().equals("")) JOptionPane.showMessageDialog(null, "Invalid File Name!!", "Open File: ERROR", JOptionPane.ERROR_MESSAGE); else JOptionPane.showMessageDialog(null, "File: " + f.getName(), "Open File Result", JOptionPane.PLAIN_MESSAGE); } } else if (e.getSource() == closeItem) JOptionPane.showMessageDialog(null, "Close is not implemented", "Attention", JOptionPane.PLAIN_MESSAGE); } public static void main (String args[]) { StudentDB application = new StudentDB(); application.setSize(200,200); application.setVisible(true); } }