Swing - SPLessons

Swing Notepad Application

Chapter 25

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Swing Notepad Application

Swing Notepad Application

shape Description

Swing Notepad Application is a program used to edit the text files and to store the files.The excellent feature of Swing Notepad Application is UTF-8(encoding character). Developers will use Swing Notepad Application to write the source code because it provides encoding format where as word document stores the data in binary format.

Features of Swing Notepad Application as follows.

Swing Notepad

shape Example

Create the package name as Swing, all the components will be stored under this package.

package swing;//create the package

Import all the required packages to build notepad.

import javax.swing.*;//import all the required packages.  
import java.awt.event.*;  

Create a class that should implement ActionListener. When click on the button ActionListener needs to be perform on the events

public class SwingNotepad implements ActionListener{ 

Create a frame, menubar with (file,edit,help), menu items with(cut,copy,paste,selectAll), text field.

JFrame f;  
JMenuBar mb;  //created menubar.
JMenu file,edit,help;  //adding menu items.
JMenuItem cut,copy,paste,selectAll;//adding menitems(sub menu).  
JTextArea ta;  

Create the constructor and this constructor name should be same as class name.

SwingNotepad()//created the consructor

Include menu items into the frame, copy, paste, selectAll are the sub menus.

cut=new JMenuItem("cut");  //cfreated menu items
copy=new JMenuItem("copy");  
paste=new JMenuItem("paste");  
selectAll=new JMenuItem("selectAll");

Add ActionListeners to the menu items. It will be performed when user click on the menu items.

cut.addActionListener(this); //added ActionListeners to the menu items.
copy.addActionListener(this);  
paste.addActionListener(this);  
selectAll.addActionListener(this); 

Create menu elements into the menubar. Menubar may consists of sub menus.

file=new JMenu("File"); //created menu file
edit=new JMenu("Edit");  
help=new JMenu("Help");  

Provide edit option to the all menu items.

edit.add(cut);//providing edit option
edit.add(copy);
edit.add(paste);
edit.add(selectAll); 

Add menubar and text area to the frame.

f.add(mb);f.add(ta);  
  
f.setLayout(null);  
f.setSize(500,500);  
f.setVisible(true); 

Action needs to be performed when user click on the buttons.When user click on cut the text will be vanished and it can be placed any where with paste option.Cut and paste both uses paste operation.

public void actionPerformed(ActionEvent e) {  
if(e.getSource()==cut)  
ta.cut();  
if(e.getSource()==paste)  
ta.paste();  
if(e.getSource()==copy)  
ta.copy();  
if(e.getSource()==selectAll)  
ta.selectAll();  
}  

Write the main method.

public static void main(String[] args) {  
    new SwingNotepad();  

SwingNotepad.java

package swing;

import javax.swing.*;  
import java.awt.event.*;  
  
public class SwingNotepad implements ActionListener{  
JFrame f;  
JMenuBar mb;  
JMenu file,edit,help;  
JMenuItem cut,copy,paste,selectAll;  
JTextArea ta;  
      
SwingNotepad(){  
f=new JFrame();  
  
cut=new JMenuItem("cut");  
copy=new JMenuItem("copy");  
paste=new JMenuItem("paste");  
selectAll=new JMenuItem("selectAll");  
  
cut.addActionListener(this);  
copy.addActionListener(this);  
paste.addActionListener(this);  
selectAll.addActionListener(this);  
  
mb=new JMenuBar();  
mb.setBounds(5,5,400,40);  
  
file=new JMenu("File");  
edit=new JMenu("Edit");  
help=new JMenu("Help");  
  
edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);  
  
  
mb.add(file);mb.add(edit);mb.add(help);  
  
ta=new JTextArea();  
ta.setBounds(5,30,460,460);  
  
f.add(mb);f.add(ta);  
  
f.setLayout(null);  
f.setSize(500,500);  
f.setVisible(true);  
}  
  
public void actionPerformed(ActionEvent e) {  
if(e.getSource()==cut)  
ta.cut();  
if(e.getSource()==paste)  
ta.paste();  
if(e.getSource()==copy)  
ta.copy();  
if(e.getSource()==selectAll)  
ta.selectAll();  
}  
  
public static void main(String[] args) {  
    new SwingNotepad();  
}  
}  

Output:
Output will be as follows.Where user can write the data, edit the data, store the data that can be readable.

Summary

shape Key Points

  • The great feature of notepad is UTF-8.
  • Notepad only supports to text editors, it does not support images.
  • ActionListener interface should perform on the components to register the objects.