JAVA TEXT EDITOR – JAVAPAD MINI CORE JAVA PROJECT

JAVA TEXT EDITOR - JAVAPAD
JAVA TEXT EDITOR – JAVAPAD




We Feel Grateful That We Got This Project From Idris Jega Labaran – PG Student, Lovely Professional University Punjab, India

 

Javapad Project

Overview

There will be times when you will want to edit textual files. Textual files are different from documents created by Word Pad, Word, Word Perfect or some other word processing program in that they contain no embedded codes. Editing these files with a word processing program can introduce special characters that make these files unusable. Instead, use Notepad which allows you to edit the file as pure text.

Textual files will normally have a TEXT file extension, but there are several special purpose files in Windows that must be pure text. Some of the more important of these include your CONFIG.SYS, AUTOEXEC.BAT, WIN.INI and SYSTEM.INI (These files existed in earlier version of Windows, but have been eliminated in Windows XP). You may get to the point of wanting to play with these files. If so, use Notepad to edit these and not Word Pad as your system cannot use the resulting Word Pad file. If you decide you want to create a web page, but don’t have an HTML editor, you will want to use Notepad so as not to introduce those nasty characters associated with Word Pad, etc.

In addition to having a tool to use in editing those special files on your system, learning Notepad can provide a good foundation for learning some of the other tools that come with Windows 95/98/ME/XP as well as a number of the applications that you may want to add to your system later. Notepad is simple to learn but has some features in common with other programs.

SOFTWARE REQUIREMENTS

❖ JAVA PLATFORM WITH JDK

❖ WINDOWS OS

❖ MY ECILLPSE

Functions of Javapad

File Menu

File Menu In Javapad
File Menu In Javapad

This is the File menu. Notice a couple of things about this menu. Notice the “…” at the end of several selections. This indicates that selecting these items will take you to a dialog box that will allow you to carry out that operation. Notice the code down the right side of the menu – Ctrl+N, Ctrl+O, etc. These are hotkeys you can use from within the document without having to go into the menu. For example, holding the key down and pressing N is the same as going into the File menu and selecting New. These hotkeys can save you some time in getting to these functions. I wouldn’t try to memorize these hotkeys right now, but just make a mental note of them – you’ll learn them over time as you find them appropriate to your situation.

New – This will start a new document. If you are currently working on a document that you haven’t saved, the system will prompt you to save the old document before starting a new one.

Open… – This allows you to bring an existing document into Notepad to view and/or edit it. You’ll see the following dialog box when you make this selection:

Open
open dialog in javapad
open dialog in javapad

This is the standard Open dialog box used by most Windows programs. It consists of a toolbar across the top that shows you which folder you are currently in along with a couple of control buttons for getting to where you want to go. The main part of the Open dialog box shows you the files/folders that match the criteria for this application. Next is where the system will tell you the name of the file it thinks you want. Right now, it doesn’t know so it assumes you want to look at all TXT files. Next is where you can tell the system what type of files you are interested in looking at. By default, Notepad looks at Text Documents (*.TXT), but you can change this to look at anything else. Finally, you can either Open the selection or Cancel this operation.

Save – If you are making modifications to an existing file, Save will put the changes back into the file after confirming that you really want to make the changes. If the file you are working on is a new file, Save works the same as Save As….

Save As… – Save As… brings up a dialog box similar to the Open dialog box above. In Notepad you get to save the file wherever you want, but the best place would be in My Documents. It’s easy enough to change folders and save a file anywhere on the system you want. See Naming a File for some suggestions on how to organize your data.
Exit – This shuts down Notepad. If the file you’ve been working on has not been saved, you will be prompted to save the file before Notepad exits.

Edit Menu
edit_menu
edit_menu

The Edit menu contains those functions that allow you to manipulate the information you have on the screen. In this case, there are four items that appear dim in the menu. The reason for this is that these items are not available at the present time. In most cases, you have to have something selected for these items to work. Windows does a pretty good job of communicating with you to tell you what you can and cannot do. Also notice that most of the items in the Edit menu have a key combination on the right (Ctrl+Z, etc.). These are hot keys that can be used from within the document without having to go to the Edit menu. For example, you can do a Copy from within a document by holding down the key and pressing the “C” key. I don’t recommend that you try and memorize these hot keys just now.

Instead, I would recommend that you just make note of them as you use the menu items and over time pick up on those you use most frequently.

Undo – Notepad does have a rather neat feature called the Undo. In essence, this is your Oops button. If you make a mistake and suddenly say “Oops”, you can select the Undo and reverse whatever it was you just did. Unless I’m mistaken, the Undo item will only work on the most recent change you’ve made to your document.

Cut – Again, this item is only available if you have something selected in your document. Otherwise you have nothing to Cut. You can select some piece of your document by holding down the left mouse button and dragging across the letters/words/sentences that you want to select. Your selection will have white letters and a blue background. Now you will be able to Cut this selection. Cut removes the selected text from your document and puts it on the Clipboard. The information is not totally deleted, but is being held in case you want to put it somewhere else.

Copy – Copy is very similar to Cut. The only difference is that with Copy, your selected text is not removed from the document. Instead, a copy of your selected text is placed on the Clipboard for you to use later.
Paste – Once you have some text on the Clipboard (using either Cut or Copy above), you can move to wherever you would like to put this text and Paste it back into your document. In other words, the combination of Cut and Paste allows you to move text around within your document while the combination of Copy and Paste allows you to duplicate information.

Delete – When you Delete text from your document, it is gone. If you do this by accident, you can use the Undo function to bring it back, but otherwise it is gone. Delete does not place the selected text on the Clipboard.
Select All – If there is something you want to do with all the text in your document at the same time, all you have to do is select the Select All function and your entire document will be selected. Now you can either Cut or Copy all the text to your Clipboard.

Format Menu
format_menu
format_menu

Word Wrap – In some cases, you may want the text you type to appear on the same line, but most of the time, you’d like to be able to see all the text on the screen at the same time. If you only have a little bit of information, you might be able to enlarge the Notepad window to see all the text. Another way of handling this is to select the Word Wrap function. This will take words that don’t fit in the Notepad window and wrap them down to the next line. A check mark appears when word wrap is turned on. Wrapping text enables you to see all the text on the line, but it doesn’t affect the way text appears when it is printed.

Font… – This allows you to change the font used to display the file you have open. It doesn’t change the font of the file itself. It simply allows you to view/edit files using whatever font you wish.

choose_a_font
choose_a_font

Coding

Notepad2.java

 

import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.*;
import java.io.*;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
//import javax.swing.JFontChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JTextArea;public class Notepad2 extends JFrame implements ActionListener
{
JMenuBar mb;
int pos1;
String str= “”;
String fileName = “Untitled”;
JMenu file, edit, help, view,format;
JMenuItem open,newf,save,saveas, exit,copy,selectall, cut,paste,gotos,about,del,replace,wrap,font,background,status;
JTextArea ta;
JPanel jp;
//setTitle(getPath(fileopen));

int filetoopen; // fichier a ouvrir
int filetosave; // fichier a enregistrer
int filetosaveas;
JFileChooser fileopen;
JFileChooser filesave;
JFileChooser filesaveas;
JFileChooser saves;
FontChooser fontDialog = null;
JColorChooser fcolorChooser=null;
JDialog foregroundDialog=null;
int savealready,option;

public Notepad2()
{

jp = new JPanel();
//jp.setBounds(20,20,400,400);
file = new JMenu(“File”);
format = new JMenu(“Format”);
edit = new JMenu(“Edit”);
help = new JMenu(“Help”);
view = new JMenu(“View”);
ta = new JTextArea(40,60);

ta.addFocusListener(new FocusListener() {

public void focusGained(FocusEvent e) {}
public void focusLost(FocusEvent e) {

if(ta.getText().length()==0)
{
copy.setEnabled(false);
cut.setEnabled(false);
paste.setEnabled(false);
gotos.setEnabled(false);
save.setEnabled(false);
selectall.setEnabled(false);

}
else
{
copy.setEnabled(true);
cut.setEnabled(true);
paste.setEnabled(true);
gotos.setEnabled(true);
save.setEnabled(true);
selectall.setEnabled(true);

}

}

});

add(new JScrollPane(ta));
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
//jp.setLayout (new BorderLayout());
open = new JMenuItem(“Open”);
open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
open.addActionListener(this);
newf = new JMenuItem(“New File”);

newf.addActionListener(this);

save= new JMenuItem(“Save”);
save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));

save.addActionListener(this);
wrap= new JMenuItem(“Word Wrap”);

wrap.addActionListener(this);
background= new JMenuItem(“Background Color”);

background.addActionListener(this);

font= new JMenuItem(“Font”);

font.addActionListener(this);

saveas= new JMenuItem(“Save As”);

saveas.addActionListener(this);

exit = new JMenuItem(“Exit”);

exit.addActionListener(this);

copy = new JMenuItem(“Copy”);

copy.addActionListener(this);
del = new JMenuItem(“Delete”);

del.addActionListener(this);
replace = new JMenuItem(“Replace”);

replace.addActionListener(this);

cut= new JMenuItem(“Cut”);

cut.addActionListener(this);

paste = new JMenuItem(“Paste”);

paste.addActionListener(this);

selectall = new JMenuItem(“Select All”);

selectall.addActionListener(this);

status = new JMenuItem(“Status Bar”);

status.addActionListener(this);

about = new JMenuItem(“About US”);

about.addActionListener(this);

gotos = new JMenuItem(“Go To”);

gotos.addActionListener(this);

about= new JMenuItem(“About Us”);

about.addActionListener(this);

//wordwrap= new JMenuItem(“Word Wrap”);

//wordwrap.addActionListener(this);
mb = new JMenuBar();
mb.setBounds(0,0,800,40);
file.add(open);
file.add(newf);
file.add(save);
file.add(saveas);
file.add(exit);
mb.add(file);

edit.add(copy);
edit.add(cut);
edit.add(paste);
edit.add(del);
edit.add(selectall);
edit.add(replace);
edit.add(gotos);
//jp.add(ta);
//jp.add(sp,BorderLayout.CENTER);
format.add(wrap);
format.add(font);
format.add(background);

view.add(status);
help.add(about);

mb.add(edit);
mb.add(format);
mb.add(view);
mb.add(help);
setJMenuBar(mb);
//add(jp,BorderLayout.CENTER);
this.setTitle(fileName+” – JavaPad”);

//ta.addKeyListener();
//pack();

}

public void actionPerformed (ActionEvent e)
{

if(e.getSource()==open)
{
openFile();
}
if(e.getSource() == save)
{
saveFile();
}
if(e.getSource() == saveas)
{
saveAs();
}
if(e.getSource() == exit)
{
System.exit(0);
}
if(e.getSource() == cut)
{
ta.cut();
}

if(e.getSource() == newf)
{
ta.setText(“”);
}

if(e.getSource()==font)
{

if(fontDialog==null)
fontDialog=new FontChooser(ta.getFont());

if(fontDialog.showDialog(this,”Choose a font”))
this.ta.setFont(fontDialog.createFont());

}
if(e.getSource()==copy)
{

str = ta.getSelectedText();
}

if(e.getSource()==background)
{

showForegroundColorDialog();
}
if(e.getSource()==paste)
{
pos1=ta.getCaretPosition();
ta.insert(str,pos1);

}
if(e.getSource()==selectall)
{
String str=ta.getText();
int strLen=str.length();
ta.select(0,strLen);

}
if(e.getSource()==about)
{
JOptionPane.showMessageDialog(this,
“This Project was developed by Team Unstoppable!!!.”);

}
}

void showForegroundColorDialog()
{
if(fcolorChooser==null)
fcolorChooser=new JColorChooser();
if(foregroundDialog==null)
foregroundDialog=JColorChooser.createDialog
(this,
“background”,
false,
fcolorChooser,
new ActionListener()
{public void actionPerformed(ActionEvent evvv){
ta.setForeground(fcolorChooser.getColor());}},
null);

foregroundDialog.setVisible(true);
}

void openFile()
{
JFileChooser fc = new JFileChooser();
int i = fc.showOpenDialog(this);
filetoopen = i;
fileopen = fc;

if(filetoopen == JFileChooser.APPROVE_OPTION)
{
ta.setText(“”);
try{
Scanner scan = new Scanner(new FileReader(fileopen.getSelectedFile().getPath()));
while (scan.hasNext())
ta.append(scan.nextLine()+”\n”);
fileName = fileopen.getSelectedFile().getName();
this.setTitle(fileName+” – JavaPad”);

savealready=0;
} catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}

}

void saveFile()
{

if(savealready==1)
{

try {
BufferedWriter out = new BufferedWriter(new FileWriter(filesave.getSelectedFile().getPath()));
out.write(ta.getText());
out.close();
savealready=1;
fileName = filesave.getSelectedFile().getName();
this.setTitle(fileName+” – JavaPad”);

} catch (Exception ex) {
System.out.println(ex.getMessage());
}

}

else
{

saves = new JFileChooser();
option = saves.showSaveDialog(this);
filetosave = option;
filesave = saves;
if (filetosave == JFileChooser.APPROVE_OPTION){
try {
BufferedWriter out = new BufferedWriter(new FileWriter(filesave.getSelectedFile().getPath()));
out.write(ta.getText());
out.close();
savealready=1;
fileName = filesave.getSelectedFile().getName();
this.setTitle(fileName+” – JavaPad”);

} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
}

void saveAs()
{
JFileChooser saveas = new JFileChooser();
int option = saveas.showSaveDialog(this);
filetosaveas = option;
filesaveas = saveas;
if (filetosaveas == JFileChooser.APPROVE_OPTION){
try {
BufferedWriter out = new BufferedWriter(new FileWriter(filesaveas.getSelectedFile().getPath()));
out.write(ta.getText());
out.close();

savealready =0; //To reset the savedfile to 1.
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}

void displayContent(String fpath){
try{
BufferedReader br=new BufferedReader(new FileReader(fpath));
String s1=””,s2=””;

while((s1=br.readLine())!=null){
s2+=s1+”\n”;
}
ta.setText(s2);
br.close();
}catch (Exception e) {e.printStackTrace(); }
}

public static void main(String args[])

{
Notepad2 np=new Notepad2();
StatusBar statusBar = new StatusBar();
np.getContentPane().add(statusBar, java.awt.BorderLayout.SOUTH);
//np.setSize(800,800);
//np.setLayout(null);
np.pack();
np.setVisible(true);

np.setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

class StatusBar extends JLabel {

/** Creates a new instance of StatusBar */
public StatusBar() {
super();
super.setPreferredSize(new Dimension(100, 16));
setMessage(“Ready”);
}

public void setMessage(String message) {
setText(” “+message);
}
}

FontChooser class

FontChooser.java

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
/******************************************************/
class FontDemo extends JFrame
{
FontChooser dialog=null;
JTextArea ta;
JButton fontButton;

FontDemo()
{
super(“Font”);

ta=new JTextArea(7,20);
fontButton=new JButton(“Set Font”);

ActionListener ac=new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
if(dialog==null)
dialog=new FontChooser(ta.getFont());
if(dialog.showDialog(FontDemo.this,”Choose a font”))
{
FontDemo.this.ta.setFont(dialog.createFont());
}
}
};
fontButton.addActionListener(ac);

add(ta,BorderLayout.CENTER);
add(fontButton,BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(50,50,400,400);
ta.append(“Hello dear. how r u?”);
ta.append(“\n\n A quick brown fox jumps over the lazy dog.”);
ta.append(“\n\n0123456789”);
ta.append(“\n~!@#$%^&*()_+|?><“);
setVisible(true);
}
////////////////////////////////
public static void main(String[] args)
{
new FontDemo();
}

}
/******************************************************/
public class FontChooser extends JPanel //implements ActionListener
{
private Font thisFont;

private JList jFace, jStyle, jSize;

private JDialog dialog;
private JButton okButton;

JTextArea tf;

private boolean ok;

public FontChooser(Font withFont)
{
thisFont=withFont;

////////////////////
String[] fontNames=
GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
jFace=new JList(fontNames); jFace.setSelectedIndex(0);

jFace.addListSelectionListener(new ListSelectionListener()
{public void valueChanged(ListSelectionEvent ev){tf.setFont(createFont());}});

String[] fontStyles={“Regular”,”Italic”,”Bold”,”Bold Italic”};
jStyle=new JList(fontStyles);jStyle.setSelectedIndex(0);

jStyle.addListSelectionListener(new ListSelectionListener()
{public void valueChanged(ListSelectionEvent ev){tf.setFont(createFont());}});

String[] fontSizes=new String[30];
for(int j=0; j<30; j++)
fontSizes[j]=new String(10+j*2+””);
jSize=new JList(fontSizes); jSize.setSelectedIndex(0);

jSize.addListSelectionListener(new ListSelectionListener()
{public void valueChanged(ListSelectionEvent ev){tf.setFont(createFont());}});

JPanel jpLabel=new JPanel();
jpLabel.setLayout(new GridLayout(1,3));

jpLabel.add(new JLabel(“Font”,JLabel.CENTER));
jpLabel.add(new JLabel(“Font Style”,JLabel.CENTER));
jpLabel.add(new JLabel(“Size”,JLabel.CENTER));

JPanel jpList=new JPanel();
jpList.setLayout(new GridLayout(1,3));

jpList.add(new JScrollPane(jFace));
jpList.add(new JScrollPane(jStyle));
jpList.add(new JScrollPane(jSize));

okButton=new JButton(“OK”);
JButton cancelButton=new JButton(“Cancel”);

okButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
ok=true;
FontChooser.this.thisFont=FontChooser.this.createFont();
dialog.setVisible(false);
}
});

cancelButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
dialog.setVisible(false);
}
});

JPanel jpButton=new JPanel();
jpButton.setLayout(new FlowLayout());
jpButton.add(okButton);
jpButton.add(new JLabel(” “));//dummy Label
jpButton.add(cancelButton);

tf=new JTextArea(5,30);
JPanel jpTextField=new JPanel();
jpTextField.add(new JScrollPane(tf));

JPanel centerPanel=new JPanel();
centerPanel.setLayout(new GridLayout(2,1));
centerPanel.add(jpList);
centerPanel.add(jpTextField);

setLayout(new BorderLayout());
add(jpLabel,BorderLayout.NORTH);
add(centerPanel,BorderLayout.CENTER);
add(jpButton,BorderLayout.SOUTH);
add(new JLabel(” “),BorderLayout.EAST);//dummy label
add(new JLabel(” “),BorderLayout.WEST);//dummy label

tf.setFont(thisFont);
tf.append(“\nA quick brown fox jumps over the lazy dog.”);
tf.append(“\n0123456789”);
tf.append(“\n~!@#$%^&*()_+|?><\n”);

}
//////////////////////////
public Font createFont()
{
Font fnt=thisFont;
int fontstyle=Font.PLAIN;
int x=jStyle.getSelectedIndex();

switch(x)
{
case 0:
fontstyle=Font.PLAIN; break;
case 1:
fontstyle=Font.ITALIC; break;
case 2:
fontstyle=Font.BOLD; break;
case 3:
fontstyle=Font.BOLD+Font.ITALIC; break;
}

int fontsize=Integer.parseInt((String)jSize.getSelectedValue());
String fontname=(String)jFace.getSelectedValue();

fnt=new Font(fontname,fontstyle,fontsize);

return fnt;

}
//////////////////////////////////
public boolean showDialog(Component parent, String title)
{
ok=false;

Frame owner=null;
if(parent instanceof Frame)
owner=(Frame)parent;
else
owner=(Frame)SwingUtilities.getAncestorOfClass(Frame.class,parent);
if(dialog==null || dialog.getOwner()!=owner)
{
dialog=new JDialog(owner,true);
dialog.add(this);
dialog.getRootPane().setDefaultButton(okButton);
dialog.setSize(400,325);
}

dialog.setTitle(title);
dialog.setVisible(true);
//System.out.println(dialog.getWidth()+” “+dialog.getHeight());
return ok;
}
//////////////////////////////
}




One Comment

  1. 98Jeffery said:

    Hello admin, i must say you have hi quality content here.
    Your blog can go viral. You need initial traffic only. How to get it?
    Search for: make your content go viral Wrastain’s tools

    December 8, 2017
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code