Magazine
 
Tips & Tricks
 

1- Create your own Notepad in Java

You must have worked with Notepad to write programs. Now its turn to create notepad by own with the help of java language. This section explains some basic functionalities of notepad which will help creating full fleshed notepad application. So just go through the example and see how it works.

First, a class extending JFrame and implementing ActionListener is created. JFrame is the main container for swing-based application. This program sets look and feel of the working platform by the statement

UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName()); When we add components to a JFrame we don’t directly add them to the JFrame but we have to specify the pane of the JFrame’s JRootPane In this example components are added to the contentPane which can be got by calling getContentPane(). The default behavior, when the user attempts to close the window, is to simply hide the JFrame. To change the default behavior, invoke setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE) method which Disposes the frame automatically when it closes. Within the class, a menu bar, a menu and some of its menu items are created and added to the appropriate component. Action listeners are added to the menu items to listen the action when they are clicked and appropriate functions are implemented that functions according to the
item clicked. For example, when user clicks on open menu item then listener listens the action and open() method is called that results in displaying the content of the file.

Notepad.class:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.text.*;
public class Notepad extends JFrame
implements ActionListener {

 

JMenuBar menuBar;
JMenu menu;
JMenuItem open,copy,cut,paste,save,quit;
JTextArea field;
FileFilterClass javaFileFilter = new
FileFilterClass ();

File file = new File (“file.java”);
public Notepad1(String title){
super(title);
try {
//Set Look and Feel of the current plateform.
UIManager.setLookAndFee
l(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception ex) {
System.err.println(“Error loading System
specific Look and Feel. “ + ex);
}
Container contentpane = getContentPane ();
contentpane.setLayout (
new BorderLayout () );
this.setSize(300, 300);
// Dispose the frame automatically when it
closes.
this.setDefaultCloseOperation
(JFrame.DISPOSE_ON_CLOSE);
field = new JTextArea();
field.setDragEnabled(true);
contentpane.add ( field, “Center”);
//Create Menu Bar.
menuBar = new JMenuBar();
setJMenuBar(menuBar);
//Create File menu.
menu = new JMenu(“File”);
//Activate the keyboard shortcut for File menu.
menu.setMnemonic(KeyEvent.VK_F);//It
underlines the character ‘F’ passed into the
setMnemonic( ) method.
//Create menu items.
open = new JMenuItem(“Open”);
copy = new JMenuItem(“Copy”);
cut = new JMenuItem(“Cut”);
paste = new JMenuItem(“Paste”);
save = new JMenuItem(“Save”);
quit = new JMenuItem(“Quit”);
//Add items to the menu.
menu.add(open);
menu.add(copy);

Oct 2007 | Java Jazz Up | 68
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78,   Download PDF