// MenuDemoWithToolbarApplication.java (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-11-26 File created. // 2006-11-26 Last modification. // This program is a modified version of MenuDemoApplication.java. // This version provides a toolbar in addition to the menus. // See notes at the end of this file. import java.awt.* ; import java.awt.event.*; import javax.swing.* ; class MenuDemoFrameWithToolbar extends JFrame implements ActionListener { // The following is an inner class whose objects represent the // activities that can be carried out through the toolbar. // An inner class is a class inside another class. // It is possible to access the members of the outer class // from an inner class method. class ToolbarAction extends AbstractAction { public ToolbarAction( String action_name ) { putValue( Action.NAME, action_name ) ; } // The following actionPerformed() method is called when the // toolbar buttons are pressed. Here we make the program behave as if // selections were made via the menus. The text on the screen will // not change when the toolbar buttons are pressed. public void actionPerformed( ActionEvent event ) { if ( this == toolbar_small_text_action ) { small_text_menu_item.setSelected( true ) ; } else if ( this == toolbar_medium_text_action ) { medium_text_menu_item.setSelected( true ) ; } else if ( this == toolbar_large_text_action ) { large_text_menu_item.setSelected( true ) ; } else if ( this == toolbar_cyan_background_action ) { if ( cyan_background_menu_item.isSelected() ) { cyan_background_menu_item.setSelected( false ) ; getContentPane().setBackground( Color.white ) ; } else { cyan_background_menu_item.setSelected( true ) ; getContentPane().setBackground( Color.cyan ) ; } } else if ( this == toolbar_quit_action ) { setVisible( false ) ; System.exit( 0 ) ; } repaint() ; } } 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" ) ; ButtonGroup text_size_button_group = new ButtonGroup() ; JMenuItem about_menu_item = new JMenuItem( "About" ) ; String last_selected_menu_item = "NO SELECTIONS MADE." ; Action toolbar_quit_action = new ToolbarAction( "Quit" ) ; Action toolbar_cyan_background_action = new ToolbarAction( "CyanBackground" ) ; Action toolbar_small_text_action = new ToolbarAction( "Small" ) ; Action toolbar_medium_text_action = new ToolbarAction( "Medium" ) ; Action toolbar_large_text_action = new ToolbarAction( "Large" ) ; JToolBar toolbar_of_this_application = new JToolBar() ; public MenuDemoFrameWithToolbar() { 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 ) ; 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 ) ; toolbar_of_this_application.add( toolbar_quit_action ) ; toolbar_of_this_application.add( toolbar_cyan_background_action ) ; toolbar_of_this_application.add( toolbar_small_text_action ) ; toolbar_of_this_application.add( toolbar_medium_text_action ) ; toolbar_of_this_application.add( toolbar_large_text_action ) ; getContentPane().add( toolbar_of_this_application, BorderLayout.NORTH ) ; } // 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 MenuDemoWithToolbarApplication { public static void main( String[] not_in_use ) { MenuDemoFrameWithToolbar this_application_window = new MenuDemoFrameWithToolbar() ; this_application_window.setVisible( true ) ; } } /* NOTES: If you like to have so called 'tool tips' connected to the toolbar buttons, you can do it with the following statements: toolbar_quit_action.putValue( Action.SHORT_DESCRIPTION, "Quit this application" ) ; toolbar_cyan_background_action.putValue( Action.SHORT_DESCRIPTION, "Change background color" ) ; toolbar_small_text_action.putValue( Action.SHORT_DESCRIPTION, "Small text size" ) ; toolbar_medium_text_action.putValue( Action.SHORT_DESCRIPTION, "Medium text size" ) ; toolbar_large_text_action.putValue( Action.SHORT_DESCRIPTION, "Large text size" ) ; */