MORE MENUN EXAMPLES

CONTENTS

1. Introduction
2. Combining menues and text fields
3. Combining menues and text fields (Swing example)
4. Combining menues and text areas
5. Combining menues and text areass (Swing example)


1. INTRODUCTION

In this WWW page I intend to inlude further examples of GUIs involving menues as and when the need arrises.



2. COMBINING MENUES AND TEXT FIELDS

The code presented in Table 1 causes text to appear in the text field each time a menu item is selected. Some example output is given in Figure 1.

// Menu Example 2 
// Frans Coenen
// Thursday 13 March 2003
// Dept. of Comp. Sci., University of Liverpool

import java.awt.*;
import java.awt.event.*;

public class MenuExample2 extends Frame implements WindowListener, ActionListener {
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                       FIELDS                      */ 
    /*                                                   */
    /* ------------------------------------------------- */  
        
    private MenuBar bar  = new MenuBar();
    private Menu    lang = new Menu("LANGUAGES");
    private Menu    para = new Menu("PARADIGM");
    
    private TextField aTextField = new TextField(57);
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                   CONSTRUCTORS                    */ 
    /*                                                   */
    /* ------------------------------------------------- */
    
    public MenuExample2(String text) {
        super(text);
        
	setBackground(Color.magenta);
        setLayout(new FlowLayout());
        addWindowListener(this);
               
        // Create lang menu
        lang.add("Ada");
	lang.add("C++");
        lang.add("Java");
	lang.add("Pascal");
        lang.add("Prolog");
        lang.add("Miranda");
        
        // Create paradigm menu
        para.add("Imperative");
        para.add("Object oriented");
        para.add("Logic");
        para.add("Functional");
        
        // Add items to bar
        bar.add(lang);
        bar.add(para);
        this.setMenuBar(bar);
        
	// Non-editable text field
	aTextField.setEditable(false);
	add(aTextField);
	
        // Add event listeners        
        lang.addActionListener(this);
        para.addActionListener(this);               
        }
                     
    /* ------------------------------------------------- */
    /*                                                   */
    /*                       METHODS                     */ 
    /*                                                   */
    /* ------------------------------------------------- */
        
    /* ACTION PERFORMED */
    
    /* Process textfield events */
                             
    public void actionPerformed(ActionEvent event) {
	    
        // User selected from pradigm menu
        if (event.getSource() == lang) {
            String nameOfItem = event.getActionCommand();
	    outputParadigmForGivenLanguage(nameOfItem);
	    System.out.println(nameOfItem);
            }
        // User selected from language menu
	else if (event.getSource() == para) {             
            String nameOfItem = event.getActionCommand();
	    outputLanguageForGivenParadigm(nameOfItem);
	    System.out.println(nameOfItem);
	    }
        }
    
    /* OUTPUT PARADIGM FOR GIVEN LANGUAGE */
    
    public void outputParadigmForGivenLanguage(String nameOfItem) {
    
        if (nameOfItem.equals("Ada")) aTextField.setText(nameOfItem + 
			" is an Imperative programming langauge");
	else if (nameOfItem.equals("C++")) aTextField.setText(nameOfItem + 
			" is an Object Oriented programming langauge");
        else if (nameOfItem.equals("Java")) aTextField.setText(nameOfItem + 
			" is an Object Oriented programming langauge");
	else if (nameOfItem.equals("Pascal")) aTextField.setText(nameOfItem + 
			" is an Imperative programming langauge");
        else if (nameOfItem.equals("Prolog")) aTextField.setText(nameOfItem + 
			" is an Logic programming langauge");
        else if (nameOfItem.equals("Miranda")) aTextField.setText(nameOfItem + 
			" is an Functional programming langauge");
	else aTextField.setText("UNEXPECTED ERROR!");
	}
	
    /* OUTPUT LANGUAGE FOR GIVEN PARADIGM */
    
    public void outputLanguageForGivenParadigm(String nameOfItem) {
        if (nameOfItem.equals("Imperative")) 
			aTextField.setText("Examples of " + nameOfItem + 
			" programming languages include Ada and pascal");
	else if (nameOfItem.equals("Object oriented")) 
			aTextField.setText("Examples of " + nameOfItem + 
			"programming languages include C++ and Java");
        else if (nameOfItem.equals("Logic")) 
			aTextField.setText("Examples of " + nameOfItem + 
			"programming languages include Prolog");
	else if (nameOfItem.equals("Functional")) 
			aTextField.setText("Examples of " + nameOfItem + 
			"programming languages include Miranda");
	else aTextField.setText("UNEXPECTED ERROR!");
	}
	    
    /* WINDOW EVENT HANDLERS */
     	   
    /* Window Closing */    
    public void windowClosing(WindowEvent event) {
        System.exit(0);
        } 
    /* Window Closed */ 
    public void windowClosed(WindowEvent event) {}
    /* Window Deiconified */   
    public void windowDeiconified(WindowEvent event) {}
    /* Window  Iconified */    
    public void windowIconified(WindowEvent event) {}
    /* Window Activated */   
    public void windowActivated(WindowEvent event) {}
    /* Window Deactivated */   
    public void windowDeactivated(WindowEvent event) {}
    /* Window Opened */    
    public void windowOpened(WindowEvent event) {}
    }
   
/* -------------------------------------------------- */
/*                                                    */
/*                  APPLICATION CLASS                 */
/*                                                    */
/* -------------------------------------------------- */            
    
class MenuExample2App {
    
    /* Main  method */    
    
    public static void main(String[] args) {
        MenuExample2 screen = new MenuExample2("Menu example1 2");
        
        screen.setSize(500,100);
        screen.setVisible(true);
        }
    }

Table 1:Linking menu selections and text fields

MENU TO TEXT FIELD GUI 1

Figure 1: Some output produced by code presemted in Table 1



3. COMBINING MENUES AND TEXT FIELDS (SWING EXAMPLE)

The Java code presented in Table 2 has the same functionality to that presented in table 1 but is encoded using Java Swing. It will be noted that the code is organised in a significantly different manner to that presented in Table 1. Some sample output is presented in Figure 2.

// Menu Example 3 
// Frans Coenen
// Thursday 13 March 2003
// Dept. of Comp. Sci., University of Liverpool

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

public class MenuExample3 extends JFrame implements ActionListener {
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                       FIELDS                      */ 
    /*                                                   */
    /* ------------------------------------------------- */  
        
    private JMenuBar bar  = new JMenuBar();
    private JMenu    lang = new JMenu("LANGUAGES");
    private JMenu    para = new JMenu("PARADIGM");
    
    JMenuItem adaItem, cppItem, javItem, pasItem, proItem, mirItem;
    JMenuItem impItem, objItem, logItem, funItem;
    
    private JTextField aTextField = new JTextField(42);
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                   CONSTRUCTORS                    */ 
    /*                                                   */
    /* ------------------------------------------------- */
    
    public MenuExample3(String text) {
        super(text);
	Container container = getContentPane();
	container.setLayout(new FlowLayout());
	container.setBackground(Color.magenta);
               
        // Create lang menu
	createLangMenuItem(lang,adaItem,"Ada",'A');
	createLangMenuItem(lang,cppItem,"C++",'C');
	createLangMenuItem(lang,javItem,"Java",'J');
	createLangMenuItem(lang,mirItem,"Miranda",'M');
	createLangMenuItem(lang,pasItem,"Pascal",'P');
	createLangMenuItem(lang,proItem,"Prolog",'r');
        
        // Create paradigm menu
	createLangMenuItem(para,impItem,"Imperative",'I');
	createLangMenuItem(para,impItem,"Object oriented",'O');
	createLangMenuItem(para,impItem,"Logic",'L');
	createLangMenuItem(para,impItem,"Functional",'F');
        
        // Add items to bar
        bar.add(lang);
        bar.add(para);
        setJMenuBar(bar);
        
	// Non-editable text field
	aTextField.setEditable(false);
	container.add(aTextField);          
        }
	
    private void createLangMenuItem(JMenu menu, JMenuItem menuItem, 
    	String text, char c) {
        menuItem = new JMenuItem(text);
	menuItem.setMnemonic(c);
	menu.add(menuItem);
	menuItem.addActionListener(this);
	} 
	                 
    /* ------------------------------------------------- */
    /*                                                   */
    /*                       METHODS                     */ 
    /*                                                   */
    /* ------------------------------------------------- */
        
    /* ACTION PERFORMED */
    
    /* Process textfield events */
                             
    public void actionPerformed(ActionEvent event) {
        String nameOfItem = event.getActionCommand();
	
	outputInformation(nameOfItem);
	System.out.println(nameOfItem);
        }
    
    /* OUTPUT PARADIGM FOR GIVEN LANGUAGE */
    
    public void outputInformation(String nameOfItem) {
    
        if (nameOfItem.equals("Ada")) aTextField.setText(nameOfItem + 
			" is an Imperative programming langauge");
	else if (nameOfItem.equals("C++")) aTextField.setText(nameOfItem + 
			" is an Object Oriented programming langauge");
        else if (nameOfItem.equals("Java")) aTextField.setText(nameOfItem + 
			" is an Object Oriented programming langauge");
	else if (nameOfItem.equals("Pascal")) aTextField.setText(nameOfItem + 
			" is an Imperative programming langauge");
        else if (nameOfItem.equals("Prolog")) aTextField.setText(nameOfItem + 
			" is an Logic programming langauge");
        else if (nameOfItem.equals("Miranda")) aTextField.setText(nameOfItem + 
			" is an Functional programming langauge");
	else if (nameOfItem.equals("Imperative")) 
			aTextField.setText("Examples of " + nameOfItem + 
			" programming languages include Ada and pascal");
	else if (nameOfItem.equals("Object oriented")) 
			aTextField.setText("Examples of " + nameOfItem + 
			"programming languages include C++ and Java");
        else if (nameOfItem.equals("Logic")) 
			aTextField.setText("Examples of " + nameOfItem + 
			"programming languages include Prolog");
	else if (nameOfItem.equals("Functional")) 
			aTextField.setText("Examples of " + nameOfItem + 
			"programming languages include Miranda");
	else aTextField.setText("UNEXPECTED ERROR!");
	}
    }
   
/* -------------------------------------------------- */
/*                                                    */
/*                  APPLICATION CLASS                 */
/*                                                    */
/* -------------------------------------------------- */            
    
class MenuExample3App {
    
    /* Main  method */    
    
    public static void main(String[] args) {
        MenuExample3 screen = new MenuExample3("Menu example1 3");
        screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
        screen.setSize(500,100);
        screen.setVisible(true);
        }
    }

Table 2:Linking menu selections and text fields (Swing example)

MENU TO TEXT FIELD GUI 2

Figure 2: Some output produced by code presemted in Table 2




3. COMBINING MENUES AND TEXT AREAS

The code presented in Table 3 causes text to appear in the text area each time a menu item is selected. Some example output is given in Figure 1.

// Menu Example 4 
// Frans Coenen
// Thursday 13 March 2003
// Dept. of Comp. Sci., University of Liverpool

import java.awt.*;
import java.awt.event.*;

public class MenuExample2 extends Frame implements WindowListener, ActionListener {
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                       FIELDS                      */ 
    /*                                                   */
    /* ------------------------------------------------- */  
        
    private MenuBar bar  = new MenuBar();
    private Menu    lang = new Menu("LANGUAGES");
    private Menu    para = new Menu("PARADIGM");
    
    private TextArea aTextArea;
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                   CONSTRUCTORS                    */ 
    /*                                                   */
    /* ------------------------------------------------- */
    
    public MenuExample2(String text) {
        super(text);
        
	setBackground(Color.magenta);
        setLayout(new FlowLayout());
        addWindowListener(this);
               
        // Create lang menu
        lang.add("Ada");
	lang.add("C++");
        lang.add("Java");
	lang.add("Pascal");
        lang.add("Prolog");
        lang.add("Miranda");
        
        // Create paradigm menu
        para.add("Imperative");
        para.add("Object oriented");
        para.add("Logic");
        para.add("Functional");
        
        // Add items to bar
        bar.add(lang);
        bar.add(para);
        this.setMenuBar(bar);
        
	// Text area 
	aTextArea = new TextArea("",5,45,TextArea.SCROLLBARS_BOTH);
	add(aTextArea);
	
        // Add event listeners        
        lang.addActionListener(this);
        para.addActionListener(this);               
        }
                     
    /* ------------------------------------------------- */
    /*                                                   */
    /*                       METHODS                     */ 
    /*                                                   */
    /* ------------------------------------------------- */
        
    /* ACTION PERFORMED */
    
    /* Process textfield events */
                             
    public void actionPerformed(ActionEvent event) {
	    
        // As in Table 1
	
        }
    
    /* OUTPUT PARADIGM FOR GIVEN LANGUAGE */
    
    public void outputParadigmForGivenLanguage(String nameOfItem) {
    
        // As in Table 1
	
	}
	
    /* OUTPUT LANGUAGE FOR GIVEN PARADIGM */
    
    public void outputLanguageForGivenParadigm(String nameOfItem) {
        if (nameOfItem.equals("Imperative")) 
			aTextArea.setText("Examples of the " + nameOfItem + 
			"\nprogramming languages include Ada and pascal");
	else if (nameOfItem.equals("Object oriented")) 
			aTextArea.setText("Examples of the " + nameOfItem + 
			"\nprogramming languages include C++ and Java");
        else if (nameOfItem.equals("Logic")) 
			aTextArea.setText("Examples of the " + nameOfItem + 
			"\nprogramming languages include Prolog");
	else if (nameOfItem.equals("Functional")) 
			aTextArea.setText("Examples of the " + nameOfItem + 
			"\nprogramming languages include Miranda");
	else aTextArea.setText("UNEXPECTED ERROR!");
	}
	    
    /* WINDOW EVENT HANDLERS */
     	   
    // As in table 1
   
/* -------------------------------------------------- */
/*                                                    */
/*                  APPLICATION CLASS                 */
/*                                                    */
/* -------------------------------------------------- */            
    
class MenuExample4App {
    
    /* Main  method */    
    
    public static void main(String[] args) {
        MenuExample4 screen = new MenuExample4("Menu example1 2");
        
        screen.setSize(500,200);
        screen.setVisible(true);
        }
    }

Table 3:Linking menu selections and text areas

MENU TO TEXT FIELD GUI 1

Figure 1: Some output produced by code presemted in Table 3



4. COMBINING MENUES AND TEXT AREAS (SWING EXAMPLE)

The Java code presented in Table 4 has the same functionality to that presented in table 3 but is encoded using Java Swing. Some sample output is presented in Figure 2.

// Menu Example 5 
// Frans Coenen
// Thursday 13 March 2003
// Dept. of Comp. Sci., University of Liverpool

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

public class MenuExample5 extends JFrame implements ActionListener {
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                       FIELDS                      */ 
    /*                                                   */
    /* ------------------------------------------------- */  
        
    private JMenuBar bar  = new JMenuBar();
    private JMenu    lang = new JMenu("LANGUAGES");
    private JMenu    para = new JMenu("PARADIGM");
    
    private JMenuItem adaItem, cppItem, javItem, pasItem, proItem, mirItem;
    private JMenuItem impItem, objItem, logItem, funItem;
    
    private JTextArea aTextArea;
    
    /* ------------------------------------------------- */
    /*                                                   */
    /*                   CONSTRUCTORS                    */ 
    /*                                                   */
    /* ------------------------------------------------- */
    
    public MenuExample3(String text) {
        super(text);
	Container container = getContentPane();
	container.setLayout(new FlowLayout());
	container.setBackground(Color.magenta);
               
        // Create lang menu
	createLangMenuItem(lang,adaItem,"Ada",'A');
	createLangMenuItem(lang,cppItem,"C++",'C');
	createLangMenuItem(lang,javItem,"Java",'J');
	createLangMenuItem(lang,mirItem,"Miranda",'M');
	createLangMenuItem(lang,pasItem,"Pascal",'P');
	createLangMenuItem(lang,proItem,"Prolog",'r');
        
        // Create paradigm menu
	createLangMenuItem(para,impItem,"Imperative",'I');
	createLangMenuItem(para,impItem,"Object oriented",'F');
	createLangMenuItem(para,impItem,"Logic",'L');
	createLangMenuItem(para,impItem,"Functional",'O');
        
        // Add items to bar
        bar.add(lang);
        bar.add(para);
        setJMenuBar(bar);
        
	// Text area
	aTextArea = new JTextArea(5,30);
        container.add(new JScrollPane(aTextArea));     
        }
	
    private void createLangMenuItem(JMenu menu, JMenuItem menuItem, 
    	
	// As in Table 2
	
	} 
	                 
    /* ------------------------------------------------- */
    /*                                                   */
    /*                       METHODS                     */ 
    /*                                                   */
    /* ------------------------------------------------- */
        
    /* ACTION PERFORMED */
    
    /* Process textfield events */
                             
    public void actionPerformed(ActionEvent event) {
        
	// As in table 2
	
        }
    
    /* OUTPUT PARADIGM FOR GIVEN LANGUAGE */
    
    public void outputInformation(String nameOfItem) {
    
        if (nameOfItem.equals("Ada")) aTextField.setText(nameOfItem + 
			" is an Imperative programming langauge");
	else if (nameOfItem.equals("C++")) aTextField.setText(nameOfItem + 
			" is an Object Oriented programming langauge");
        else if (nameOfItem.equals("Java")) aTextField.setText(nameOfItem + 
			" is an Object Oriented programming langauge");
	else if (nameOfItem.equals("Pascal")) aTextField.setText(nameOfItem + 
			" is an Imperative programming langauge");
        else if (nameOfItem.equals("Prolog")) aTextField.setText(nameOfItem + 
			" is an Logic programming langauge");
        else if (nameOfItem.equals("Miranda")) aTextField.setText(nameOfItem + 
			" is an Functional programming langauge");
	else if (nameOfItem.equals("Imperative")) 
			aTextArea.setText("Examples of " + nameOfItem + 
			"\nprogramming languages include Ada and pascal");
	else if (nameOfItem.equals("Object oriented")) 
			aTextArea.setText("Examples of " + nameOfItem + 
			"\nprogramming languages include C++ and Java");
        else if (nameOfItem.equals("Logic")) 
			aTextArea.setText("Examples of " + nameOfItem + 
			"\nprogramming languages include Prolog");
	else if (nameOfItem.equals("Functional")) 
			aTextArea.setText("Examples of " + nameOfItem + 
			"\nprogramming languages include Miranda");
	else aTextField.setText("UNEXPECTED ERROR!");
	}
    }
   
/* -------------------------------------------------- */
/*                                                    */
/*                  APPLICATION CLASS                 */
/*                                                    */
/* -------------------------------------------------- */            
    
class MenuExample3App {
    
    /* Main  method */    
    
    public static void main(String[] args) {
        MenuExample5 screen = new MenuExample5("Menu example1 3");
        screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
        screen.setSize(500,200);
        screen.setVisible(true);
        }
    }

Table 2:Linking button presses and text areas (Swing example)

MENU TO TEXT AREA GUI 2

Figure 2: Some output produced by code presemted in Table 2




Created and maintained by Frans Coenen. Last updated 13 March 2003