Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add the following features to your program • Count and display the number of cha

ID: 3624897 • Letter: A

Question

Add the following features to your program
• Count and display the number of characters and words in the file
• Change the font type and size
• Search for a pattern in the file. (You may simply display the number of times the pattern exists in the file)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

/**
* The question1 and can open file and save file
* @author Xue
*
*/
public class MyNotePad extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;

//create the file menu
JMenuBar menu =new JMenuBar();
JMenu cd1=new JMenu("File");
JMenu cd2=new JMenu("Edit");
JMenuItem cdx1_cd1=new JMenuItem("New");
JMenuItem cdx2_cd1=new JMenuItem("Open");
JMenuItem cdx3_cd1=new JMenuItem("Save");
JMenuItem cdx4_cd1=new JMenuItem("Save As");
JMenuItem cdx5_cd1=new JMenuItem("Exit");

JMenuItem cdx1_cd2=new JMenuItem("Cut");
JMenuItem cdx2_cd2=new JMenuItem("Copy");
JMenuItem cdx3_cd2=new JMenuItem("Paste");
JMenuItem cdx4_cd2=new JMenuItem("Select All");

JTextArea textArea =new JTextArea();


public MyNotePad(){
super("MyNotePad");

menu.add(cd1);
cd1.add(cdx1_cd1);
cd1.add(cdx2_cd1);
cd1.add(cdx3_cd1);
cd1.add(cdx4_cd1);
cd1.add(new JSeparator());
cd1.add(cdx5_cd1);

menu.add(cd2);
cd2.add(cdx1_cd2);
cd2.add(cdx2_cd2);
cd2.add(cdx3_cd2);
cd2.add(cdx4_cd2);


setJMenuBar(menu);


cdx1_cd1.setActionCommand("cdx1_cd1");
cdx1_cd1.addActionListener(this);

cdx2_cd1.setActionCommand("cdx2_cd1");
cdx2_cd1.addActionListener(this);

cdx3_cd1.setActionCommand("cdx3_cd1");
cdx3_cd1.addActionListener(this);

cdx4_cd1.setActionCommand("cdx4_cd1");
cdx4_cd1.addActionListener(this);

cdx5_cd1.setActionCommand("cdx5_cd1");
cdx5_cd1.addActionListener(this);

cdx1_cd2.setActionCommand("cdx1_cd2");
cdx1_cd2.addActionListener(this);

cdx2_cd2.setActionCommand("cdx2_cd2");
cdx2_cd2.addActionListener(this);

cdx3_cd2.setActionCommand("cdx3_cd2");
cdx3_cd2.addActionListener(this);

cdx4_cd2.setActionCommand("cdx4_cd2");
cdx4_cd2.addActionListener(this);
//build Scroll panel
textArea.setLineWrap(true);
JScrollPane panel=new JScrollPane(textArea);
getContentPane().add(panel,BorderLayout.CENTER);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* build the action listener
*/
public void actionPerformed(ActionEvent e){
//The New function
if(e.getActionCommand().equals("cdx1_cd1")){
textArea.setText("");
}
//The Open function
else if(e.getActionCommand().equals("cdx2_cd1")){
JFileChooser fc=new JFileChooser();
int returnVal=fc.showOpenDialog(MyNotePad.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
this.setTitle(file.getName()+" - MyNotePad");
try{
FileReader fr=new FileReader(file);
int len=(int)file.length();
char []buffer=new char[len];
fr.read(buffer,0,len);
fr.close();
textArea.setText(new String(buffer));
}
catch(Exception ex){}
}
}
//The save function
else if(e.getActionCommand().equals("cdx3_cd1")){
JFileChooser fc=new JFileChooser();
int returnVal=fc.showSaveDialog(MyNotePad.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();

this.setTitle(file.getName()+"MyNotePad");
try{
FileWriter fw=new FileWriter(file);
fw.write(textArea.getText());
fw.close();
}
catch(Exception ej){}

}
}
//The save as function
else if(e.getActionCommand().equals("cdx4_cd1")){
JFileChooser fc=new JFileChooser();
int returnVal=fc.showSaveDialog(MyNotePad.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
this.setTitle(file.getName()+"MyNotePad");
try{
FileWriter fw=new FileWriter(file);
fw.write(textArea.getText());
fw.close();
}
catch(Exception ej){}
}
}
//The exit function
else if(e.getActionCommand().equals("cdx5_cd1")){
System.exit(0);
}//Cut Function
else if(e.getActionCommand().equals("cdx1_cd2")){
textArea.cut();
}//Copy Function
else if(e.getActionCommand().equals("cdx2_cd2")){
textArea.copy();
}//paste function
else if(e.getActionCommand().equals("cdx3_cd2")){
textArea.paste();
}
else if(e.getActionCommand().equals("cdx4_cd2")){
textArea.selectAll();
}
}
/**
* The panel size and show it in GUI
* @param args
*/
public static void main(String[] args){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
MyNotePad visible=new MyNotePad();
visible.setSize(700,600);
visible.setVisible(true);
}
}

Explanation / Answer

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

/**
* The question1 and can open file and save file
* @author Xue
*
*/
public class MyNotePad extends JFrame implements ActionListener
{
/**
*
*/
public String fsize,ftype;
private static final long serialVersionUID = 1L;

//create the file menu
JMenuBar menu =new JMenuBar();
JMenu cd1=new JMenu("File");
JMenu cd2=new JMenu("Edit");
JMenu cd3=new JMenu("AddedFeatures");
JMenuItem cdx1_cd1=new JMenuItem("New");
JMenuItem cdx2_cd1=new JMenuItem("Open");
JMenuItem cdx3_cd1=new JMenuItem("Save");
JMenuItem cdx4_cd1=new JMenuItem("Save As");
JMenuItem cdx5_cd1=new JMenuItem("Exit");

JMenuItem cdx1_cd2=new JMenuItem("Cut");
JMenuItem cdx2_cd2=new JMenuItem("Copy");
JMenuItem cdx3_cd2=new JMenuItem("Paste");
JMenuItem cdx4_cd2=new JMenuItem("Select All");

JMenuItem cdx1_cd3=new JMenuItem("Count");
JMenuItem cdx2_cd3=new JMenuItem("Font");


JTextArea textArea =new JTextArea();


public MyNotePad(){
super("MyNotePad");

menu.add(cd1);
cd1.add(cdx1_cd1);
cd1.add(cdx2_cd1);
cd1.add(cdx3_cd1);
cd1.add(cdx4_cd1);
cd1.add(new JSeparator());
cd1.add(cdx5_cd1);

menu.add(cd2);
cd2.add(cdx1_cd2);
cd2.add(cdx2_cd2);
cd2.add(cdx3_cd2);
cd2.add(cdx4_cd2);

menu.add(cd3);
cd3.add(cdx1_cd3);
cd3.add(cdx2_cd3);

setJMenuBar(menu);


cdx1_cd1.setActionCommand("cdx1_cd1");
cdx1_cd1.addActionListener(this);

cdx2_cd1.setActionCommand("cdx2_cd1");
cdx2_cd1.addActionListener(this);

cdx3_cd1.setActionCommand("cdx3_cd1");
cdx3_cd1.addActionListener(this);

cdx4_cd1.setActionCommand("cdx4_cd1");
cdx4_cd1.addActionListener(this);

cdx5_cd1.setActionCommand("cdx5_cd1");
cdx5_cd1.addActionListener(this);

cdx1_cd2.setActionCommand("cdx1_cd2");
cdx1_cd2.addActionListener(this);

cdx2_cd2.setActionCommand("cdx2_cd2");
cdx2_cd2.addActionListener(this);

cdx3_cd2.setActionCommand("cdx3_cd2");
cdx3_cd2.addActionListener(this);

cdx4_cd2.setActionCommand("cdx4_cd2");
cdx4_cd2.addActionListener(this);

cdx1_cd3.setActionCommand("cdx1_cd3");
cdx1_cd3.addActionListener(this);

cdx2_cd3.setActionCommand("cdx2_cd3");
cdx2_cd3.addActionListener(this);


//build Scroll panel
textArea.setLineWrap(true);
JScrollPane panel=new JScrollPane(textArea);
getContentPane().add(panel,BorderLayout.CENTER);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* build the action listener
*/
public void actionPerformed(ActionEvent e)
{
//The New function
if(e.getActionCommand().equals("cdx1_cd1")){
textArea.setText("");
}
//The Open function
else if(e.getActionCommand().equals("cdx2_cd1")){
JFileChooser fc=new JFileChooser();
int returnVal=fc.showOpenDialog(MyNotePad.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
this.setTitle(file.getName()+" - MyNotePad");
try{
FileReader fr=new FileReader(file);
int len=(int)file.length();
char []buffer=new char[len];
fr.read(buffer,0,len);
fr.close();
textArea.setText(new String(buffer));
}
catch(Exception ex){}
}
}
//The save function
else if(e.getActionCommand().equals("cdx3_cd1")){
JFileChooser fc=new JFileChooser();
int returnVal=fc.showSaveDialog(MyNotePad.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();

this.setTitle(file.getName()+"MyNotePad");
try{
FileWriter fw=new FileWriter(file);
fw.write(textArea.getText());
fw.close();
}
catch(Exception ej){}

}
}
//The save as function
else if(e.getActionCommand().equals("cdx4_cd1")){
JFileChooser fc=new JFileChooser();
int returnVal=fc.showSaveDialog(MyNotePad.this);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file=fc.getSelectedFile();
this.setTitle(file.getName()+"MyNotePad");
try{
FileWriter fw=new FileWriter(file);
fw.write(textArea.getText());
fw.close();
}
catch(Exception ej){}
}
}
//The exit function
else if(e.getActionCommand().equals("cdx5_cd1")){
System.exit(0);
}//Cut Function
else if(e.getActionCommand().equals("cdx1_cd2")){
textArea.cut();
}//Copy Function
else if(e.getActionCommand().equals("cdx2_cd2")){
textArea.copy();
}//paste function
else if(e.getActionCommand().equals("cdx3_cd2")){
textArea.paste();
}
else if(e.getActionCommand().equals("cdx4_cd2")){
textArea.selectAll();
}
else if(e.getActionCommand().equals("cdx1_cd3"))
{
JFileChooser cfc=new JFileChooser();
int returnVal=cfc.showOpenDialog(MyNotePad.this);
if(returnVal == JFileChooser.APPROVE_OPTION){

    try
    {
        File f=cfc.getSelectedFile();
        int nw=countWords(f);
        int nc=countChars(f);
        textArea.setText("");
        String s="Number of Words in Selected File"+nw;
        s=s+" ";
        s=s+"Number of Characters in Selected File"+nc;
        textArea.setText(s);
    }
    catch(IOException ie)
    {
        textArea.setText(ie.toString());
    }

}
}
else if(e.getActionCommand().equals("cdx2_cd3"))
{
        FontSelector fs=new FontSelector(this);
        Font font = new Font(ftype, Font.BOLD, Integer.parseInt(fsize));
        textArea.setFont(font);
        textArea.setForeground(Color.BLUE);
        textArea.setText("Font Size and Color Changed");
}
}
//to count number of characters in file
int countChars(File f)throws IOException
{
    int countChar =0;

    FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
               long numChar = f.length();
               while(numChar>0)
               {
                   countChar++;
                   numChar--;
               }

    return(countChar);
}
//to count number of words in file
int countWords(File f)throws IOException
{
    int numWords =0;
   
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);
             int index = 0;
                    boolean prevwhitespace = true;
                    String line = br.readLine();
           while(line!=null)
           {
            while(index < line.length())
            {
                char c = line.charAt(index++);
                boolean currwhitespace = Character.isWhitespace(c);
                if(prevwhitespace && !currwhitespace)
                {
                    numWords++;
                }
                prevwhitespace= currwhitespace;
            }
            line=br.readLine();
           }
         
    return(numWords);
}

    f=new Frame();
    f.setTitle("Select Color and Size");
    f.setSize(150,150);
    f.setLocation(200, 200);
    f.setLayout(new GridLayout(2,2));
    f.show();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote