Home | | Web Programming | Put the Entire MenuDemo Program Together

Chapter: Java The Complete Reference : Introducing GUI Programming with Swing : Introducing Swing Menus

Put the Entire MenuDemo Program Together

Throughout the course of this discussion, many changes and additions have been made to the MenuDemo program shown at the start of the chapter.

Put the Entire MenuDemo Program Together


Throughout the course of this discussion, many changes and additions have been made to the MenuDemo program shown at the start of the chapter. Before concluding, it will be helpful to assemble all the pieces. Doing so not only eliminates any ambiguity about the way the pieces fit together, but it also gives you a complete menu demonstration program that you can experiment with.


The following version of MenuDemo includes all of the additions and enhancements described in this chapter. For clarity, the program has been reorganized, with separate methods being used to construct the various menus and toolbar. Notice that several of the menu-related variables, such as jmb, jmFile, and jtb, have been made into instance variables.

// The complete MenuDemo program.


import java.awt.*; import java.awt.event.*; import javax.swing.*;


class MenuDemo implements ActionListener {


JLabel jlab;


JMenuBar jmb;


JToolBar jtb;


JPopupMenu jpu;


DebugAction setAct;


DebugAction clearAct;


DebugAction resumeAct;


MenuDemo() {


// Create a new JFrame container.


JFrame jfrm = new JFrame("Complete Menu Demo");


     ///Use default border layout.


     //Give the frame an initial size.


     jfrm.setSize(360, 200);


     ///Terminate the program when the user closes the application.




     //Create a label that will display the menu selection.


jlab = new JLabel();


     //Create the menu bar.

     jmb = new JMenuBar();


//Make the File menu.


     //Construct the Debug actions.



     //Make the toolbar.




     //Make the Options menu.




     //Make the Help menu.




     //Make the Edit popup menu.




     //Add a listener for the popup trigger.


     jfrm.addMouseListener(new MouseAdapter() {


public void mousePressed(MouseEvent me) { if(me.isPopupTrigger())


jpu.show(me.getComponent(), me.getX(), me.getY());




public void mouseReleased(MouseEvent me) { if(me.isPopupTrigger())


jpu.show(me.getComponent(), me.getX(), me.getY());






     //Add the label to the center of the content pane.

     jfrm.add(jlab, SwingConstants.CENTER);


     //Add the toolbar to the north position of


     //the content pane.


jfrm.add(jtb, BorderLayout.NORTH);


     //Add the menu bar to the frame.



     //Display the frame.






     //Handle menu item action events.


     //This does NOT handle events generated


     //by the Debug options.


public void actionPerformed(ActionEvent ae) {


     Get the action command from the menu selection. String comStr = ae.getActionCommand();


     //If user chooses Exit, then exit the program.


     if(comStr.equals("Exit")) System.exit(0);


     //Otherwise, display the selection.


     jlab.setText(comStr + " Selected");



     //An action class for the Debug menu


     //and toolbar.


class DebugAction extends AbstractAction {


public DebugAction(String name, Icon image, int mnem, int accel, String tTip) {


super(name, image); putValue(ACCELERATOR_KEY,


KeyStroke.getKeyStroke(accel, InputEvent.CTRL_DOWN_MASK));


putValue(MNEMONIC_KEY, new Integer(mnem)); putValue(SHORT_DESCRIPTION, tTip);



     //Handle events for both the toolbar and the


     //Debug menu.


public void actionPerformed(ActionEvent ae) { String comStr = ae.getActionCommand();


jlab.setText(comStr + " Selected");


     //Toggle the enabled status of the


     //Set and Clear Breakpoint options.


     if(comStr.equals("Set Breakpoint")) {





} else if(comStr.equals("Clear Breakpoint")) {

clearAct.setEnabled(false); setAct.setEnabled(true);








// Create the File menu with mnemonics and accelerators.

void makeFileMenu() {


JMenu jmFile = new JMenu("File"); jmFile.setMnemonic(KeyEvent.VK_F);


JMenuItem jmiOpen = new JMenuItem("Open", KeyEvent.VK_O);


jmiOpen.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_O,




JMenuItem jmiClose = new JMenuItem("Close", KeyEvent.VK_C);


jmiClose.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_C,




JMenuItem jmiSave = new JMenuItem("Save", KeyEvent.VK_S);


jmiSave.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_S,



JMenuItem jmiExit = new JMenuItem("Exit", KeyEvent.VK_E);


jmiExit.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_E,
















// Add the action listeners for the File menu.

jmiOpen.addActionListener(this); jmiClose.addActionListener(this); jmiSave.addActionListener(this); jmiExit.addActionListener(this);




// Create the Options menu.

void makeOptionsMenu() {


JMenu jmOptions = new JMenu("Options");


// Create the Colors submenu.


JMenu jmColors = new JMenu("Colors");


     //Use check boxes for colors. This allows


     //the user to select more than one color.


     JCheckBoxMenuItem jmiRed = new JCheckBoxMenuItem("Red"); JCheckBoxMenuItem jmiGreen = new JCheckBoxMenuItem("Green"); JCheckBoxMenuItem jmiBlue = new JCheckBoxMenuItem("Blue");


     //Add the items to the Colors menu.










// Create the Priority submenu.


JMenu jmPriority = new JMenu("Priority");


     //Use radio buttons for the priority setting.


     //This lets the menu show which priority is used


     //but also ensures that one and only one priority


     //can be selected at any one time. Notice that


     //the High radio button is initially selected.


     JRadioButtonMenuItem jmiHigh =


new JRadioButtonMenuItem("High", true); JRadioButtonMenuItem jmiLow =


new JRadioButtonMenuItem("Low");


Add the items to the Priority menu. jmPriority.add(jmiHigh);





     //Create a button group for the radio button


     //menu items.


ButtonGroup bg = new ButtonGroup(); bg.add(jmiHigh);




     //Now, create a Debug submenu that goes under


     //the Options menu bar item. Use actions to


     //create the items.


JMenu jmDebug = new JMenu("Debug"); JMenuItem jmiSetBP = new JMenuItem(setAct);


JMenuItem jmiClearBP = new JMenuItem(clearAct); JMenuItem jmiResume = new JMenuItem(resumeAct);


     //Add the items to the Debug menu.

     jmDebug.add(jmiSetBP); jmDebug.add(jmiClearBP); jmDebug.add(jmiResume); jmOptions.add(jmDebug);


     //Create the Reset menu item.


JMenuItem jmiReset = new JMenuItem("Reset");

jmOptions.addSeparator(); jmOptions.add(jmiReset);


     //Finally, add the entire options menu to


     //the menu bar




     //Add the action listeners for the Options menu,


     //except for those supported by the Debug menu.


     jmiRed.addActionListener(this); jmiGreen.addActionListener(this); jmiBlue.addActionListener(this); jmiHigh.addActionListener(this); jmiLow.addActionListener(this); jmiReset.addActionListener(this);




// Create the Help menu.

void makeHelpMenu() {


JMenu jmHelp = new JMenu("Help");


// Add an icon to the About menu item.

ImageIcon icon = new ImageIcon("AboutIcon.gif");


JMenuItem jmiAbout = new JMenuItem("About", icon);

jmiAbout.setToolTipText("Info about the MenuDemo program.");




// Add action listener for About.





     //Construct the actions needed by the Debug menu


     //and toolbar.


void makeActions() {


// Load the images for the actions.


ImageIcon setIcon = new ImageIcon("setBP.gif");

ImageIcon clearIcon = new ImageIcon("clearBP.gif");

ImageIcon resumeIcon = new ImageIcon("resume.gif");


// Create actions.

setAct =

new DebugAction("Set Breakpoint", setIcon, KeyEvent.VK_S, KeyEvent.VK_B,


"Set a break point.");


clearAct =


new DebugAction("Clear Breakpoint", clearIcon, KeyEvent.VK_C, KeyEvent.VK_L,


"Clear a break point.");


resumeAct =


new DebugAction("Resume", resumeIcon, KeyEvent.VK_R, KeyEvent.VK_R,


"Resume execution after breakpoint.");


// Initially disable the Clear Breakpoint option.





// Create the Debug toolbar.

void makeToolBar() {


     //Create the toolbar buttons by using the actions.

     JButton jbtnSet = new JButton(setAct);


JButton jbtnClear = new JButton(clearAct); JButton jbtnResume = new JButton(resumeAct);


     //Create the Debug toolbar.


jtb = new JToolBar("Breakpoints");


// Add the buttons to the toolbar.

jtb.add(jbtnSet); jtb.add(jbtnClear); jtb.add(jbtnResume);



// Create the Edit popup menu.

void makeEditPUMenu() {


jpu = new JPopupMenu();


     Create the popup menu items JMenuItem

     jmiCut = new JMenuItem("Cut");


JMenuItem jmiCopy = new JMenuItem("Copy");

JMenuItem jmiPaste = new JMenuItem("Paste");


     //Add the menu items to the popup menu.







     //Add the Edit popup menu action listeners.

     jmiCut.addActionListener(this); jmiCopy.addActionListener(this); jmiPaste.addActionListener(this);




public static void main(String args[]) {


// Create the frame on the event dispatching thread.

SwingUtilities.invokeLater(new Runnable() {


public void run() { new MenuDemo();









Continuing Your Exploration of Swing

Swing defines a very large GUI toolkit. It has many more features that you will want to explore on your own. For example, it supplies dialog classes, such as JOptionPane and JDialog, that you can use to streamline the construction of dialog windows. It also provides additional controls beyond those introduced in Chapter 31. Two you will want to explore are JSpinner (which creates a spin control) and JFormattedTextField (which supports formatted text). You will also want to experiment with defining your own models for the various components. Frankly, the best way to become familiar with Swing’s capabilities is to experiment with it.

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : Introducing GUI Programming with Swing : Introducing Swing Menus : Put the Entire MenuDemo Program Together |

Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.