import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.*; import java.awt.event.*; public class AddressPrg extends JFrame implements ActionListener { private JButton add = new JButton("Add"); private JButton del = new JButton("Delete"); private JTextField name = new JTextField(); private JTextField email = new JTextField(); private AddressData data = new AddressData(); private JTable table = new JTable(data); private JMenuItem fileNew = new JMenuItem("New"); private JMenuItem fileExit = new JMenuItem("Exit"); private JCheckBoxMenuItem javaLook = new JCheckBoxMenuItem ("Java Look", true); private JCheckBoxMenuItem unixLook = new JCheckBoxMenuItem ("Unix Look"); private JCheckBoxMenuItem winLook = new JCheckBoxMenuItem ("Windows Look"); private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent we) { exitPrg(); } } public AddressPrg() { super("Address Book"); JScrollPane tableScroller = new JScrollPane(table); JSplitPane splitter = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitter.setTopComponent(tableScroller); splitter.setBottomComponent(makePanel()); getContentPane().setLayout(new BorderLayout()); getContentPane().add("Center", splitter); setJMenuBar(makeMenuBar()); add.addActionListener(this); del.addActionListener(this); unixLook.addActionListener(this); javaLook.addActionListener(this); winLook.addActionListener(this); fileExit.addActionListener(this); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); addWindowListener(new WindowCloser()); fileNew.setEnabled(false); splitter.setOneTouchExpandable(true); validate(); pack(); show(); } private JPanel makePanel() { JPanel labels = new JPanel(new GridLayout(2, 1)); labels.add(new JLabel("Name:")); labels.add(new JLabel("Email:")); JPanel fields = new JPanel(new GridLayout(2, 1)); fields.add(name); fields.add(email); JPanel buttons = new JPanel(new GridLayout(2, 1)); buttons.add(add); buttons.add(del); JPanel panel = new JPanel(new BorderLayout()); panel.add("West", labels); panel.add("Center", fields); panel.add("East", buttons); return panel; } private JMenuBar makeMenuBar() { JMenu file = new JMenu("File"); file.add(fileNew); file.addSeparator(); file.add(fileExit); JMenu options = new JMenu("Options"); options.add(unixLook); options.add(javaLook); options.add(winLook); ButtonGroup optionsGroup = new ButtonGroup(); optionsGroup.add(unixLook); optionsGroup.add(javaLook); optionsGroup.add(winLook); JMenuBar bar = new JMenuBar(); bar.add(file); bar.add(options); return bar; } private void exitPrg() { if (data.isDirty()) { int response = JOptionPane.showConfirmDialog(this, "Are you sure you want to exit?"); if (response == JOptionPane.OK_OPTION) System.exit(0); } else System.exit(0); } private void addAddress() { String name_s = name.getText().trim(); String email_s = email.getText().trim(); if ( (!name_s.equals("")) && (!email_s.equals(""))) { data.add(new Address(name_s, email_s)); name.setText(""); email.setText(""); } else JOptionPane.showMessageDialog(this, "You must provide a name and email"); } private void delAddress() { int row = table.getSelectedRow(); if (row >= 0) data.delete(row); else JOptionPane.showMessageDialog(this, "You must select an address to delete"); } private void changeLookAndFeel(JCheckBoxMenuItem option) { String look = "javax.swing.plaf.metal.MetalLookAndFeel"; if (option == unixLook) look = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; else if (option == winLook) look = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; try { UIManager.setLookAndFeel(look); SwingUtilities.updateComponentTreeUI(this); } catch (Exception e) { System.err.println("Exception: " + e); } } public void actionPerformed(ActionEvent ae) { if (ae.getSource() == add) addAddress(); else if (ae.getSource() == del) delAddress(); else if ((ae.getSource() == unixLook) || (ae.getSource() == javaLook) || (ae.getSource() == winLook)) changeLookAndFeel((JCheckBoxMenuItem)ae.getSource()); else if (ae.getSource() == fileExit) exitPrg(); } public static void main(String args[]) { AddressPrg ap = new AddressPrg(); } }