java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4094
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use menus in Swing applications E-mail
User Rating: / 22
PoorBest 
JMenuBar

This Java Swing tip illustrates a method of desinging your own menu items. Developer may use this tip to provide a standard look to their applications where all the options and functionalities of the applications are present as menu items.

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

public class MenuTest extends JFrame {
   private Color colorValues[] 
      Color.black, Color.blue, Color.red, Color.green };   
   private JRadioButtonMenuItem colorItems[], fonts[];
   private JCheckBoxMenuItem styleItems[];
   private JLabel display;
   private ButtonGroup fontGroup, colorGroup;
   private int style;

   public MenuTest()
   {
      super"Using JMenus" );     

      JMenuBar bar = new JMenuBar();  // create menubar
      setJMenuBarbar );  // set the menubar for the JFrame

      // create File menu and Exit menu item
      JMenu fileMenu = new JMenu"File" );
      fileMenu.setMnemonic'F' );
      JMenuItem aboutItem = new JMenuItem"About..." );
      aboutItem.setMnemonic'A' );
      aboutItem.addActionListener(
         new ActionListener() {
            public void actionPerformedActionEvent e )
            {
               JOptionPane.showMessageDialogMenuTest.this,
                  "This is an example\nof using menus",
                  "About", JOptionPane.PLAIN_MESSAGE );
            }
         }
      );
      fileMenu.addaboutItem );

      JMenuItem exitItem = new JMenuItem"Exit" );
      exitItem.setMnemonic'x' );
      exitItem.addActionListener(
         new ActionListener() {
            public void actionPerformedActionEvent e )
            {
               System.exit);
            }
         }
      );
      fileMenu.addexitItem );
      bar.addfileMenu );    // add File menu

      // create the Format menu, its submenus and menu items
      JMenu formatMenu = new JMenu"Format" );  
      formatMenu.setMnemonic'r' );

      // create Color submenu
      String colors[] 
         "Black""Blue""Red""Green" };
      JMenu colorMenu = new JMenu"Color" );
      colorMenu.setMnemonic'C' );
      colorItems = new JRadioButtonMenuItemcolors.length ];
      colorGroup = new ButtonGroup();
      ItemHandler itemHandler = new ItemHandler();

      for int i = 0; i < colors.length; i++ ) {
         colorItems
            new JRadioButtonMenuItemcolors] );
         colorMenu.addcolorItems] );
         colorGroup.addcolorItems] );
         colorItems].addActionListeneritemHandler );
      }

      colorItems].setSelectedtrue );
      formatMenu.addcolorMenu );
      formatMenu.addSeparator();

      // create Font submenu
      String fontNames[] 
         "TimesRoman""Courier""Helvetica" };
      JMenu fontMenu = new JMenu"Font" )
      fontMenu.setMnemonic'n' );
      fonts = new JRadioButtonMenuItemfontNames.length ];
      fontGroup = new ButtonGroup();

      for int i = 0; i < fonts.length; i++ ) {
         fonts=
            new JRadioButtonMenuItemfontNames] );
         fontMenu.addfonts] );
         fontGroup.addfonts] );
         fonts].addActionListeneritemHandler );
      }

      fonts].setSelectedtrue );
      fontMenu.addSeparator();

      String styleNames[] "Bold""Italic" };
      styleItems = new JCheckBoxMenuItemstyleNames.length ];
      StyleHandler styleHandler = new StyleHandler();

      for int i = 0; i < styleNames.length; i++ ) {
         styleItems=
            new JCheckBoxMenuItemstyleNames] );
         fontMenu.addstyleItems] );
         styleItems].addItemListenerstyleHandler );
      }

      formatMenu.addfontMenu );
      bar.addformatMenu );  // add Format menu
     
      display = new JLabel(
         "Sample Text", SwingConstants.CENTER );
      display.setForegroundcolorValues] );
      display.setFont
         new Font"TimesRoman", Font.PLAIN, 72 ) );

      getContentPane().setBackgroundColor.cyan );
      getContentPane().adddisplay, BorderLayout.CENTER );

      setSize500200 );
      show();
   }

   public static void mainString args[] )
   {
      MenuTest app = new MenuTest();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosingWindowEvent e )
            {
               System.exit);
            }
         }
      );
   }

   class ItemHandler implements ActionListener {
      public void actionPerformedActionEvent e )
      {
         for int i = 0; i < colorItems.length; i++ )         
            if colorItems].isSelected() ) {
               display.setForegroundcolorValues] );
               break;
            }
 
         for int i = 0; i < fonts.length; i++ )
            if e.getSource() == fonts] ) {
               display.setFontnew Font
                  fonts].getText(), style, 72 ) );
               break;
            }

         repaint();   
      }
   }

   class StyleHandler implements ItemListener {
      public void itemStateChangedItemEvent e )
      {
         style = 0;

         if styleItems].isSelected() )
            style += Font.BOLD;

         if styleItems].isSelected() )
            style += Font.ITALIC;

         display.setFontnew Font
            display.getFont().getName(), style, 72 ) );

         repaint();
      }
   }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.