Java Applet Design: File, Edit, and Search Options with ActionPerformed Functions

Introduction

Create an Applet with the menu bar and add the following menus: File, Edit, and Search. Add default menu items to the respective menus. In the File menu, add the following items: New, Open, Save, and Close. While clicking the ‘New’ menu item, it will pop up a message box saying that the ‘New’ menu item has been clicked. The ‘Open’ and ‘Save’ items will bring appropriate dialog boxes, where one can either choose to open or save a file. Here the program will just print the name of the file being to be opened or saved to the console.

Java Applet

An applet is like a small executable code that meets some full applications to contain it or execute it. The different multimedia games, quizzes, and various such interactive programs are created for the web using applets. All applets are subclasses of Applet and must import java.applet. An applet is created by subclassing the java.applet.Applet class. The class java.applet.Applet include several methods that help in controlling the execution of applets.

Firstly, applets are event-driven, an applet represents a set of interrupt service routines. It will wait for an event to happen. By calling the event handler that the applet has provided, the AWT alerts the applet to the occurrence of an event. Once the applet has taken appropriate action, the control returns to the AWT. An Applet should never have control for a long period of time and the control should return to the AWT runtime system as soon as possible. Secondly, the user initiates with an applet. The user will interact when required. An applet receives these interactions as events, to which it reacts.

Source Code

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
public class Menus extends JFrame implements ActionListener, MenuListener {
    JMenuBar bar;
    JMenu file, edit, search;
    JMenuItem i1, i2, i3;
    public Menus() {
        setTitle("Menus Example by Ashish Bhatnagar");
        bar = new JMenuBar();
        file = new JMenu("File");
        file.setMnemonic('F');
        file.addMenuListener(this);

        i1 = new JMenuItem("New");
        i1.setMnemonic('N');
        i1.addActionListener(this);
        i1.setActionCommand("New");
        file.add(i1);

        i1 = new JMenuItem("Open");
        i1.setMnemonic('O');
        i1.addActionListener(this);
        i1.setActionCommand("Open");
        file.add(i1);

        file.addSeparator();

        i1 = new JMenuItem("Save");
        i1.setMnemonic('S');
        i1.addActionListener(this);
        i1.setActionCommand("Save");
        file.add(i1);

        i1 = new JMenuItem("Close");
        i1.setMnemonic('C');
        i1.addActionListener(this);
        i1.setActionCommand("Close");
        file.add(i1);

        edit = new JMenu("Edit");
        edit.setMnemonic('E');
        edit.addMenuListener(this);

        i2 = new JMenuItem("Undo");
        i2.setMnemonic('U');
        i2.addActionListener(this);
        i2.setActionCommand("Undo");
        edit.add(i2);

        i2 = new JMenuItem("Redo");
        i2.setMnemonic('R');
        i2.addActionListener(this);
        i2.setActionCommand("Redo");
        edit.add(i2);

        i2 = new JMenuItem("Cut");
        i2.setMnemonic('C');
        i2.addActionListener(this);
        i2.setActionCommand("Cut");
        edit.add(i2);
        edit.addSeparator();

        i2 = new JMenuItem("Copy");
        i2.setMnemonic('C');
        i2.addActionListener(this);
        i2.setActionCommand("Copy");
        edit.add(i2);

        i2 = new JMenuItem("Paste");
        i2.setMnemonic('P');
        i2.addActionListener(this);
        i2.setActionCommand("Paste");
        edit.add(i2);

        search = new JMenu("Search");
        search.setMnemonic('S');
        search.addMenuListener(this);

        i3 = new JMenuItem("Find");
        i3.setMnemonic('F');
        i3.addActionListener(this);
        i3.setActionCommand("Find");
        search.add(i3);

        i3 = new JMenuItem("Find Next");
        i3.setMnemonic('N');
        i3.addActionListener(this);
        i3.setActionCommand("Find Next");

        search.add(i3);
        bar.add(file);
        bar.add(edit);
        bar.add(search);
        setJMenuBar(bar);
    }
    class ExtensionFileFilter extends FileFilter {
        private String desc;
        private Hashtable filters = null;
        public ExtensionFileFilter() {
            filters = new Hashtable();
            setDescription("undefined");
        }
        public void addExtension(String extension) {
            filters.put(extension.toLowerCase(), this);
        }
        public String getDescription() {
            return (desc);
        }
        public void setDescription(String desc) {
            this.desc = desc;
        }
        public boolean accept(File f) {
            if (f != null) {
                if (f.isDirectory()) {
                    return true;
                }
                String extension = getExtension(f);
                if (extension != null && filters.get(extension) != null) {
                    return true;
                }
            }
            return false;
        }
        protected String getExtension(File f) {
            if (f != null) {
                String filename = f.getName();
                int i = filename.lastIndexOf('.');
                if (i > 0 && i < filename.length() - 1) {
                    return filename.substring(i + 1).toLowerCase();
                };
            }
            return null;
        }
    }
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        if (s == "New") {
            JOptionPane.showMessageDialog(null, "Hii, Clicking the new option", "New- menu Item", JOptionPane.INFORMATION_MESSAGE);
        } else if (s == "Open") {
            JOptionPane.showMessageDialog(null, "Hii, Clicking the open Option", "Open-Menu Item", JOptionPane.INFORMATION_MESSAGE);
            System.out.println("Opening the Dialog Box");
            JFileChooser jf = new JFileChooser();
            ExtensionFileFilter eff = new ExtensionFileFilter();
            eff.addExtension("java");
            eff.setDescription("Java Source Files");
            jf.addChoosableFileFilter(eff);
            eff = new ExtensionFileFilter();
            eff.addExtension("html");
            eff.addExtension("htm");
            eff.setDescription("HTML Pages");
            jf.addChoosableFileFilter(eff);
            eff = new ExtensionFileFilter();
            eff.addExtension("gif");
            eff.setDescription("Graphical Image Format");
            jf.addChoosableFileFilter(eff);
            eff = new ExtensionFileFilter();
            eff.addExtension("jpeg");
            eff.setDescription("Joint Photographic Expert Group");
            jf.addChoosableFileFilter(eff);
            jf.setFileFilter(eff);
            int returnVal = jf.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                System.out.println("You choose to open this file:" + jf.getSelectedFile().getName());
            } else if (returnVal == JFileChooser.CANCEL_OPTION) {
                System.out.println("You have not chosen any file");
            }
        } else if (s == "Save") {
            JOptionPane.showMessageDialog(null, "Hii, clicking the Save option", "Save-Menu Item", JOptionPane.INFORMATION_MESSAGE);
            JFileChooser jf = new JFileChooser();
            ExtensionFileFilter eff = new ExtensionFileFilter();
            eff.addExtension("Java");
            eff.setDescription("Java Source Files");
            jf.addChoosableFileFilter(eff);
            eff = new ExtensionFileFilter();
            eff.addExtension("html");
            eff.addExtension("htm");
            eff.setDescription("HTML Pages");
            jf.addChoosableFileFilter(eff);
            eff = new ExtensionFileFilter();
            eff.addExtension("gif");
            eff.setDescription("Graphical Image Format");
            jf.addChoosableFileFilter(eff);
            eff = new ExtensionFileFilter();
            eff.addExtension("jpeg");
            eff.setDescription("Joint Photographic Expert Group");
            jf.addChoosableFileFilter(eff);
            jf.setFileFilter(eff);
            jf.showSaveDialog(null);
            int returnVal = jf.showSaveDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                System.out.println("You Choose to save the file is: " + jf.getSelectedFile().getName());
            }
        } else if (s == "Close") {
            System.exit(0);
            JOptionPane.showMessageDialog(null, "Hii, Clicked the 'Close' option", "Close-Menu item", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    public void menuSelected(MenuEvent me) {}
    public void menuDeselected(MenuEvent me) {}
    public void menuCanceled(MenuEvent me) {}
    public static void main(String args[]) {
        JFrame frame = new Menus();
        frame.setSize(600, 200);
        frame.addWindowListener(new closeWindow());
        frame.pack();
        frame.setVisible(true);
    }
}
class closeWindow extends WindowAdapter {
    public void windowClosing(WindowEvent we) {
        Window w = we.getWindow();
        w.dispose();
        System.exit(0);
    }
}

Output

Java applets project demo

Java applets project demo

Summary

While applets are no longer viable, you can achieve similar functionality using modern web technologies like HTML5, JavaScript, and web frameworks. The described approach provides a basic structure, and you can customize and enhance it further based on your specific requirements.

This simple Java applet creates a menu bar with File, Edit, and Search menus. It includes New, Open, Save, and Close menu items under the File menu. The New menu item displays a message box when clicked, and the Open and Save menu items use file choosers to interact with the console by printing the selected file's name.


Similar Articles