// MenuDemoWithPopupMenuSimple.java (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-11-22 File created. // 2006-11-22 Last modification. // This is a simplified version of program MenuDemoWithPopupMenu- // Application.java. This version does not provide a conventional // Settings menu, i.e., Settings can be changed only through the // pop-up menu. In MenuDemoWithPopupMenuApplication.java you can // modify the settings either through the conventional Settings menu // or through the pop-up Settings menu. // At least in a Windows PC, you have to use the RIGHT mouse button to // make the pop-up menu active. import java.awt.* ; import java.awt.event.*; import javax.swing.* ; class MenuDemoFrame extends JFrame implements ActionListener, MouseListener { static final int WINDOW_WIDTH = 500 ; static final int WINDOW_HEIGHT = 400 ; JMenuItem file_open_menu_item = new JMenuItem( "Open" ) ; JMenuItem file_save_menu_item = new JMenuItem( "Save" ) ; JMenuItem file_exit_menu_item = new JMenuItem( "Exit" ) ; JMenuItem edit_copy_menu_item = new JMenuItem( "Copy" ) ; JMenuItem edit_paste_menu_item = new JMenuItem( "Paste" ) ; JCheckBoxMenuItem cyan_background_menu_item = new JCheckBoxMenuItem( "Cyan background" ) ; JRadioButtonMenuItem small_text_menu_item = new JRadioButtonMenuItem( "Small" ) ; JRadioButtonMenuItem medium_text_menu_item = new JRadioButtonMenuItem( "Medium" ) ; JRadioButtonMenuItem large_text_menu_item = new JRadioButtonMenuItem( "Large" ) ; JMenuItem about_menu_item = new JMenuItem( "About" ) ; String last_selected_menu_item = "NO SELECTIONS MADE." ; JPopupMenu settings_popup_menu = new JPopupMenu() ; public MenuDemoFrame() { getContentPane().setBackground( Color.white ) ; setSize( WINDOW_WIDTH, WINDOW_HEIGHT ) ; setTitle( "GUI APPLICATION WITH MENUS" ) ; file_open_menu_item.setMnemonic( 'O' ) ; file_open_menu_item.addActionListener( this ) ; file_save_menu_item.setMnemonic( 'S' ) ; file_save_menu_item.addActionListener( this ) ; file_exit_menu_item.setMnemonic( 'x' ) ; file_exit_menu_item.addActionListener( this ) ; JMenu file_menu = new JMenu( "File" ) ; file_menu.setMnemonic( 'F' ) ; file_menu.add( file_open_menu_item ) ; file_menu.add( file_save_menu_item ) ; file_menu.add( file_exit_menu_item ) ; edit_copy_menu_item.addActionListener( this ) ; edit_paste_menu_item.addActionListener( this ) ; JMenu edit_menu = new JMenu( "Edit" ) ; edit_menu.add( edit_copy_menu_item ) ; edit_menu.add( edit_paste_menu_item ) ; // JMenu settings_menu = new JMenu( "Settings" ) ; cyan_background_menu_item.addActionListener( this ) ; // settings_menu.add( cyan_background_menu_item ) ; small_text_menu_item.addActionListener( this ) ; medium_text_menu_item.addActionListener( this ) ; large_text_menu_item.addActionListener( this ) ; medium_text_menu_item.setSelected( true ) ; ButtonGroup text_size_button_group = new ButtonGroup() ; text_size_button_group.add( small_text_menu_item ) ; text_size_button_group.add( medium_text_menu_item ) ; text_size_button_group.add( large_text_menu_item ) ; JMenu text_size_menu = new JMenu( "Text size" ) ; text_size_menu.add( small_text_menu_item ) ; text_size_menu.add( medium_text_menu_item ) ; text_size_menu.add( large_text_menu_item ) ; // settings_menu.add( text_size_menu ) ; JMenu help_menu = new JMenu( "Help" ) ; about_menu_item.addActionListener( this ) ; help_menu.add( about_menu_item ) ; JMenuBar menubar_of_this_window = new JMenuBar() ; menubar_of_this_window.add( file_menu ) ; menubar_of_this_window.add( edit_menu ) ; // menubar_of_this_window.add( settings_menu ) ; menubar_of_this_window.add( help_menu ) ; setJMenuBar( menubar_of_this_window ) ; setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ; // Let's put the items of the settings menu also to the popup menu. settings_popup_menu.add( cyan_background_menu_item ) ; settings_popup_menu.add( text_size_menu ) ; addMouseListener(this); } // This program implements the MouseListener interface. // The pop-up menu must be shown when the mouse is pressed and // released. public void mouseClicked( MouseEvent event ) {} public void mouseEntered( MouseEvent event ) {} public void mouseExited( MouseEvent event ) {} public void mousePressed( MouseEvent event ) { if ( event.isPopupTrigger() ) { settings_popup_menu.show( event.getComponent(), event.getX(), event.getY() ) ; } } public void mouseReleased( MouseEvent event ) { if ( event.isPopupTrigger() ) { settings_popup_menu.show( event.getComponent(), event.getX(), event.getY() ) ; } } // NOTE: The rest of this program is the same as the corresponding // part of program MenuDemoApplication.java. // The following method reacts to the menu selections. // In most cases the only reaction is that the text that // appears on the screen is changed. // Background color is set to cyan if that selection is made. // Help -> About selection brings a message dialog to the screen. // File -> Exit selection terminates the application. public void actionPerformed( ActionEvent event ) { if ( event.getSource() instanceof JMenuItem ) { JMenuItem selected_menu_item = (JMenuItem) event.getSource() ; last_selected_menu_item = selected_menu_item.getText() ; if ( cyan_background_menu_item.isSelected() == true ) { getContentPane().setBackground( Color.cyan ) ; } else { getContentPane().setBackground( Color.white ) ; } if ( selected_menu_item == file_exit_menu_item ) { setVisible( false ) ; System.exit( 0 ) ; } else if ( selected_menu_item == about_menu_item ) { JOptionPane.showMessageDialog( this, "This is a simple GUI application" + "\nwith menus.", "About", JOptionPane.PLAIN_MESSAGE ) ; } repaint() ; } } public void paint( Graphics graphics ) { super.paint( graphics ) ; if ( small_text_menu_item.isSelected() == true ) { graphics.setFont( new Font( "Serif", Font.PLAIN, 10 ) ) ; } if ( medium_text_menu_item.isSelected() == true ) { graphics.setFont( new Font( "Serif", Font.PLAIN, 14 ) ) ; } if ( large_text_menu_item.isSelected() == true ) { graphics.setFont( new Font( "Serif", Font.PLAIN, 18 ) ) ; } graphics.drawString( "Last selected menu item: \"" + last_selected_menu_item + "\"", 100, 200 ) ; } } public class MenuDemoWithPopupMenuSimple { public static void main( String[] not_in_use ) { MenuDemoFrame this_application_window = new MenuDemoFrame() ; this_application_window.setVisible( true ) ; } }