Swing - SPLessons

Swing Notepad Application

Home > Lesson > 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. [java]package swing;//create the package[/java] Import all the required packages to build notepad. [java]import javax.swing.*;//import all the required packages. import java.awt.event.*; [/java] Create a class that should implement ActionListener. When click on the button ActionListener needs to be perform on the events [java]public class SwingNotepad implements ActionListener{ [/java] Create a frame, menubar with (file,edit,help), menu items with(cut,copy,paste,selectAll), text field. [java]JFrame f; JMenuBar mb; //created menubar. JMenu file,edit,help; //adding menu items. JMenuItem cut,copy,paste,selectAll;//adding menitems(sub menu). JTextArea ta; [/java] Create the constructor and this constructor name should be same as class name. [java]SwingNotepad()//created the consructor[/java] Include menu items into the frame, copy, paste, selectAll are the sub menus. [java]cut=new JMenuItem("cut"); //cfreated menu items copy=new JMenuItem("copy"); paste=new JMenuItem("paste"); selectAll=new JMenuItem("selectAll");[/java] Add ActionListeners to the menu items. It will be performed when user click on the menu items. [java]cut.addActionListener(this); //added ActionListeners to the menu items. copy.addActionListener(this); paste.addActionListener(this); selectAll.addActionListener(this); [/java] Create menu elements into the menubar. Menubar may consists of sub menus. [java]file=new JMenu("File"); //created menu file edit=new JMenu("Edit"); help=new JMenu("Help"); [/java] Provide edit option to the all menu items. [java]edit.add(cut);//providing edit option edit.add(copy); edit.add(paste); edit.add(selectAll); [/java] Add menubar and text area to the frame. [java]f.add(mb);f.add(ta); f.setLayout(null); f.setSize(500,500); f.setVisible(true); [/java] 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. [java]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(); } [/java] Write the main method. [java]public static void main(String[] args) { new SwingNotepad(); [/java] SwingNotepad.java [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(); } } [/java] 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.