Listing 13.1 JMenuBar with JMenu (TJMenuBar.java) // Demonstrates the Swing menu bar and menus /* * * */ import javax.swing.*; import javax.swing.border.*; import java.awt.*; public class TJMenuBar extends JApplet { public void init() { // 1. Get the handle on the applet's content pane. Container container = this.getContentPane(); // 2. Create a menu bar with bevel border and // add it to the applet. JMenuBar menuBar = new JMenuBar(); menuBar.setBorder(new BevelBorder(BevelBorder.RAISED)); container.add(menuBar, BorderLayout.NORTH); // 3. Create menus for a simple editor. JMenu fileMenu = new JMenu("File", true); JMenu editMenu = new JMenu("Edit"); JMenu formatMenu = new JMenu("Format"); JMenu optionsMenu = new JMenu("Options"); // 4. Add the menus to the menu bar. menuBar.add(fileMenu); menuBar.add(editMenu); menuBar.add(formatMenu); menuBar.add(optionsMenu); // 5. Get the handle on each menu to the normal and // selected icons and the mnemonic (short-cut) key. for (int i=0; i * */ import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class TJMenu extends JApplet { public void init() { // 1. Get the handle on the applet's content pane. Container container = this.getContentPane(); // 2. Add a menu bar to the applet. JMenuBar menuBar = new JMenuBar(); menuBar.setBorder(new BevelBorder(BevelBorder.RAISED)); menuBar.setBorderPainted(true); container.add(menuBar, BorderLayout.NORTH); // 3. Add the File menu and its menu items . JMenu fileMenu = new JMenu("File", true); menuBar.add(fileMenu); // 4. Add the submenus to the File menu. fileMenu.add(new JMenuItem("New")); fileMenu.add(new JMenuItem("Open")); fileMenu.addSeparator(); fileMenu.add(new JMenuItem("Save")); fileMenu.add(new JMenuItem("Sava As")); fileMenu.addSeparator(); fileMenu.addSeparator(); // 5. Add the Edit menu and its menu items. JMenu editMenu = new JMenu("Edit"); menuBar.add(editMenu); // 6. Add the submenus to the edit menu. editMenu.add(new JMenuItem("Undo")); editMenu.addSeparator(); editMenu.add(new JMenuItem("Cut")); editMenu.add(new JMenuItem("Copy")); editMenu.add(new JMenuItem("Paste")); // 7. Create and add the Options menu and submenus // and their items. JMenu optionsMenu = new JMenu("Options"); menuBar.add(optionsMenu); // 8. Add the submenus to the Options menu. JMenu bookMarksMenu = new JMenu("Book Marks"); optionsMenu.add(bookMarksMenu); // 9. Add the submenus to the Book Marks menu. JMenuItem addMI = new JMenuItem("Add Alt-K"); bookMarksMenu.add(addMI); JMenuItem editMI = new JMenuItem("Edit Alt-B"); bookMarksMenu.add(editMI); JMenu guideMenu = new JMenu("Guide"); bookMarksMenu.add(guideMenu); // 10. Add the submenus to the Guide menu. JMenuItem whatIsNewMI = new JMenuItem("What's New"); whatIsNewMI.setMnemonic('N'); guideMenu.add(whatIsNewMI); JMenuItem whatIsCoolMI = new JMenuItem("What's Cool"); whatIsCoolMI.setMnemonic('C'); guideMenu.add(whatIsCoolMI); // 11. Finally, add two more submenus to the Options menu. JMenuItem javaConsoleMI = new JMenuItem("Java Console"); optionsMenu.add(javaConsoleMI); JMenuItem addressBookMI = new JMenuItem("Address Book"); optionsMenu.add(addressBookMI); } } Listing 13.3 JCheckBoxMenuItem and JRadioButtonMenuItem (TJCheckBoxMenuItem.java) // Demonstrates the check box menu items // and radio button menu items /* * * */ import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class TJCheckBoxMenuItem extends JApplet { public void init() { // 1. Get the handle on the applet's content pane. Container container = this.getContentPane(); // 2. Create a menu bar and add it to the applet. JMenuBar menuBar = new JMenuBar(); menuBar.setBorder(new BevelBorder(BevelBorder.RAISED)); //menuBar.setBorderPainted(true); container.add(menuBar, BorderLayout.NORTH); // 3. Create and add the File menu and its menu items. JMenu fileMenu = new JMenu("File", true); menuBar.add(fileMenu); // 4. Create the Edit menu and its menu items // and add them to the menu bar. JMenu editMenu = new JMenu("Edit"); menuBar.add(editMenu); // 5. Create the Options menu, submenus, and their items. JMenu optionsMenu = new JMenu("Options"); menuBar.add(optionsMenu); // 6. Create and add the Fonts options menu to the // Options menu. JMenu fontsOptionsMenu = new JMenu("Fonts"); optionsMenu.add(fontsOptionsMenu); // 7. Create radio button menu items and add them // to the Fonts option menu. JRadioButtonMenuItem rbItem; ButtonGroup group = new ButtonGroup(); String[] rbLabels = {"Dialog", "Monospaced", "SansSerif"}; for (int i=0; i * */ import javax.swing.*; import java.awt.*; public class TJToolBar extends JApplet { //1. Create some icons for buttons. Icon newIcon = new ImageIcon("new.gif"); Icon openIcon = new ImageIcon("open.gif"); Icon saveIcon = new ImageIcon("save.gif"); Icon cutIcon = new ImageIcon("cut.gif"); Icon copyIcon = new ImageIcon("copy.gif"); Icon pasteIcon = new ImageIcon("paste.gif"); public void init() { //2. Get the handle on the applets' content pane. Container container = this.getContentPane(); //3. Create a toolbar object. JToolBar toolBar = new JToolBar(); //4. Set the margin between toolbar border and its comps. toolBar.setMargin(new Insets(5,5,5,5)); //5. Create some toolbar buttons. JButton button1 = new JButton(newIcon); JButton button2 = new JButton(openIcon); JButton button3 = new JButton(saveIcon); JButton button4 = new JButton(cutIcon); JButton button5 = new JButton(copyIcon); JButton button6 = new JButton(pasteIcon); //6. Add the buttons to the toolbar with separators. toolBar.add(button1); toolBar.add(button2); toolBar.addSeparator(); toolBar.add(button3); toolBar.addSeparator(); toolBar.addSeparator(); toolBar.add(button4); toolBar.add(button5); toolBar.addSeparator(); toolBar.add(button6); //7. Make a panel with a label. JPanel panel = new JPanel(); JLabel label = new JLabel("Pure JFC", JLabel.CENTER); label.setPreferredSize(new Dimension(350,100)); label.setBackground(Color.white); label.setFont(new Font("Dialog", Font.PLAIN, 40)); label.setOpaque(true); panel.add(label); //8. Add these components to the applet. container.add(toolBar, BorderLayout.NORTH); container.add(panel, BorderLayout.CENTER); } } Listing 13.6 Handling Actions Common to Multiple Controls (TAction.java) // Demonstrates how to handle actions common to multiple // controls such as menu items (in menus and pop-up menus) // and toolbar buttons. import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; // The Swing frame class public class TAction extends JFrame { // Declare the data members CutAction cutAction = null; PasteAction pasteAction = null; Container container = null; JPopupMenu popupMenu = null; JTextArea textArea = null; // Icons to be used over the toolbar buttons // and menu items Icon cutIcon = new ImageIcon("cut.gif"); Icon pasteIcon = new ImageIcon("paste.gif"); public TAction() { // 1. Call the super with the prescribed title. super("TAction Frame"); // 2. Get the handle on the frame's content pane. container = this.getContentPane(); // 3. Create a panel and add it at the top of the frame. JPanel panel = new JPanel(false); container.add(panel, BorderLayout.NORTH); panel.setLayout(new GridLayout(2,1)); // 4. Create a menu bar and attach it to the panel. JMenuBar menuBar = new JMenuBar(); panel.add(menuBar); // 5. Create the File and Edit menus. JMenu fileMenu = new JMenu("File"); JMenu editMenu = new JMenu("Edit"); // 6. Add the menus to the menu bar. menuBar.add(fileMenu); menuBar.add(editMenu); // 7. Create a toolbar and attach it to the panel. JToolBar toolBar = new JToolBar(); panel.add(toolBar); // 8. Create the Action objects for the cut and paste. // operations cutAction = new CutAction("Cut", cutIcon); pasteAction = new PasteAction("Paste", pasteIcon); // 9. Add the sources of common actions to their parents. JMenuItem actionCutItem = editMenu.add(cutAction); JMenuItem actionPasteItem = editMenu.add(pasteAction); JButton cutButton = toolBar.add(cutAction); JButton pasteButton = toolBar.add(pasteAction); cutButton.setText(""); // Tool bar buttons need not have pasteButton.setText(""); // the labels // 10. Create and add the text area to the content pane. textArea = new JTextArea("Test the Action interface " +"by operating the tool bar buttons, " +"\nmenu items in the edit menu, and " +"\nmenu items in the pop-up menu."); textArea.setFont(new Font("Monospaced", Font.PLAIN, 14)); container.add(textArea); // 11. Create a pop-up menu and add it to the text area. popupMenu = new JPopupMenu("Test Popup Menu"); JMenuItem cutPopupMenuItem = popupMenu.add(cutAction); JMenuItem pastePopupMenuItem = popupMenu.add(pasteAction); // 12. Create a pop-up menu listener and register it with // text area. PopupMenuListener pml = new PopupMenuListener(); textArea.addMouseListener(pml); // 13. To close the frame. addWindowListener(new WindowEventHandler()); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); // 14. Set the size of the frame and display it. setSize(350, 200); show(); } // 15. Cut Action class that extends AbstractAction. class CutAction extends AbstractAction { public CutAction(String label, Icon icon) { super(label, icon); } public void actionPerformed(ActionEvent ae) { textArea.cut(); } } // 16.Paste Action class that extends AbstractAction. class PasteAction extends AbstractAction { public PasteAction(String label, Icon icon) { super(label, icon); } public void actionPerformed(ActionEvent ae) { textArea.paste(); } } // 17. The popup menu listener. class PopupMenuListener extends MouseAdapter { public void mousePressed(MouseEvent me) { showPopup(me); } public void mouseReleased(MouseEvent me) { showPopup(me); } private void showPopup(MouseEvent me) { if (me.isPopupTrigger()) { popupMenu.show(me.getComponent(), me.getX(), me.getY()); } } } // 18. Listener class to close the frame. class WindowEventHandler extends WindowAdapter { public void windowClosing(WindowEvent evt) { System.exit(0); } } // 19. The main method. public static void main(String[] args) { TAction actionFrame = new TAction(); } } Listing 13.7 TToolTip.java // Demonstrates the Swing tool tips /* * * */ import javax.swing.*; import java.awt.*; public class TToolTip extends JApplet { // Create some icons for buttons. Icon newIcon = new ImageIcon("new.gif"); Icon openIcon = new ImageIcon("open.gif"); Icon saveIcon = new ImageIcon("save.gif"); Icon cutIcon = new ImageIcon("cut.gif"); Icon copyIcon = new ImageIcon("copy.gif"); Icon pasteIcon = new ImageIcon("paste.gif"); Icon[] icons = {newIcon, openIcon, saveIcon, cutIcon, copyIcon, pasteIcon}; String[] toolTips = {"New", "Open", "Save", "Cut", "Copy", "Paste"}; public void init() { // 1. Get the handle on the applet's content pane. Container container = this.getContentPane(); // 2. Create a toolbar object. JToolBar toolBar = new JToolBar(); // 3. Set the margin between the toolbar border and its comps. toolBar.setMargin(new Insets(5,5,5,5)); // 4. Create some buttons and ToolTips // and add them to the toolbar. Also add // the separators. for (int i=0; i<6; i++) { JButton button = new JButton(icons[i]); button.setToolTipText(toolTips[i]); toolBar.add(button); if (i == 2 || i == 3 || i == 5) toolBar.addSeparator(); if (i == 3) toolBar.addSeparator(); } // 5. Make a panel with a label. JPanel panel = new JPanel(); JLabel label = new JLabel("Pure JFC", JLabel.CENTER); //label.setPreferredSize(new Dimension(350,150)); label.setBackground(Color.white); label.setFont(new Font("Dialog", Font.PLAIN, 40)); label.setOpaque(true); panel.add(label); // 6. Add the toolbar and panel to the applet. container.add(toolBar, BorderLayout.NORTH); container.add(panel, BorderLayout.CENTER); } }