«Back to Home

Core Java

Topics

How To Create Edit Menu For Notepad In Swing Java

Create edit menu for Notepad
 
We can easily create edit menu for Notepad in Swing Java.
 
Let’s see an example of edit menu for Notepad in Swing.
 
Code
  1. import javax.swing.*;  
  2. import java.awt.event.*;  
  3. public class NotepadExample implements ActionListener {  
  4.     JFrame jf;  
  5.     JMenuBar jmb;  
  6.     JMenu file, edit, help;  
  7.     JMenuItem cut, copy, paste, selectAll;  
  8.     JTextArea jta;  
  9.     NotepadExample() {  
  10.         jf = new JFrame();  
  11.         cut = new JMenuItem("Cut");  
  12.         copy = new JMenuItem("Copy");  
  13.         paste = new JMenuItem("Paste");  
  14.         selectAll = new JMenuItem("SelectAll");  
  15.         cut.addActionListener(this);  
  16.         copy.addActionListener(this);  
  17.         paste.addActionListener(this);  
  18.         selectAll.addActionListener(this);  
  19.         jmb = new JMenuBar();  
  20.         jmb.setBounds(101030050);  
  21.         file = new JMenu("File");  
  22.         edit = new JMenu("Edit");  
  23.         help = new JMenu("Help");  
  24.         edit.add(cut);  
  25.         edit.add(copy);  
  26.         edit.add(paste);  
  27.         edit.add(selectAll);  
  28.         jmb.add(file);  
  29.         jmb.add(edit);  
  30.         jmb.add(help);  
  31.         jta = new JTextArea();  
  32.         jta.setBounds(530460460);  
  33.         jf.add(jmb);  
  34.         jf.add(jta);  
  35.         jf.setLayout(null);  
  36.         jf.setSize(500500);  
  37.         jf.setVisible(true);  
  38.     }  
  39.     public void actionPerformed(ActionEvent e) {  
  40.         if (e.getSource() == cut) {  
  41.             jta.cut();  
  42.         }  
  43.         if (e.getSource() == paste) {  
  44.             jta.paste();  
  45.         }  
  46.         if (e.getSource() == copy) {  
  47.             jta.copy();  
  48.         }  
  49.         if (e.getSource() == selectAll) {  
  50.             jta.selectAll();  
  51.         }  
  52.     }  
  53.     public static void main(String[] args) {  
  54.         new NotepadExample();  
  55.     }  
  56. }  
  57.    
  58.    
61
62
63

Output

64
 
Summary

Thus, we learnt how to create edit menu for Notepad in Swing Java.