Question

In: Computer Science

Create a graphical spell checker program that The File menu has the three items shown. On...

Create a graphical spell checker program that The File menu has the three items shown. On Open, the program reads a text file (!) with the current directory set to the directory where your program is located. Save saves the current text in the window to the file selected over a dialog and The Edit menu has one menu item: Spell Check.

Solutions

Expert Solution

HI,

I created the program and added the comments.

File menu is showing open and save and edit will show spell check menu.

1. Open method is reading text file and adding content in text area.its just like working editor program.

2. save menus will save the text area content in new file by swing File Chooser.

3. Edit is a graphical menus and showing Spell checker sub menu.

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




class SpellCheckMenu  implements ActionListener
{  
              
          JMenu menu, editMenu;  // Main menu which will show file and edit menus
                  JTextArea ta;    //text area where we will display content from the file 
          JMenuItem menuOpen, menuSave, menuSpell;   //menu items which will be added in menus
                  // class constructor
          SpellCheckMenu(){  
          JFrame frame= new JFrame("Menu and MenuItem Example");  
          JMenuBar menubar=new JMenuBar();   // all the menus will be added in this to show on window
          menu=new JMenu("File");  
                  editMenu=new JMenu("Edit");
         
          menuOpen=new JMenuItem("Open");  
          menuSave=new JMenuItem("Save");  
          menuSpell=new JMenuItem("Spell Check");  
                  
                  menuOpen.addActionListener(this);    //adding  action listner 
                  menuSave.addActionListener(this);    
                  menuSpell.addActionListener(this);    
          
          menu.add(menuOpen);
                  menu.add(menuSave); 
                  editMenu.add(menuSpell);  
          menubar.add(menu);  
                  menubar.add(editMenu);  

                  ta=new JTextArea();    
          ta.setBounds(5,5,360,320);   
                  frame.add(ta);    
          frame.setJMenuBar(menubar);  
          frame.setSize(400,400);  
          frame.setLayout(null);  
                 // frame.pack();
          frame.setVisible(true);  
}  
//even handler which will handle even like open file or save file
public void actionPerformed(ActionEvent e) {    
if(e.getSource()==menuOpen)    {
  try{
  System.out.println("open file");
  openFile();
  }//try block
  catch (Exception  ex)  
    {
        // insert code to run when exception occurs
    }

}
if(e.getSource()==menuSave)   { 
  System.out.println("save file");
  SaveAs();
}
if(e.getSource()==menuSpell)    
  System.out.println("spell check file"); 
 
}     
// open file methos which will read the text file and will display the content of the file in textarea
public void openFile() throws FileNotFoundException {
        // pass the path to the file as a parameter 
        try
    {
    File file = 
      new File("test.txt"); 
    
BufferedReader rd = new BufferedReader(new FileReader(file));
String line;
while ((line = rd.readLine()) != null)
    ta.append(line + "\n");
rd.close();
        }//try block
        catch (Exception  ex)  
    {
        // insert code to run when exception occurs
    }

}
  //this method will read the text area content and will save it in another file by file dialog
   public void SaveAs() {
      JFrame parentFrame = new JFrame();
      final JFileChooser SaveAs = new JFileChooser();
      SaveAs.setApproveButtonText("Save");
      int actionDialog = SaveAs.showOpenDialog(parentFrame);
      if (actionDialog != JFileChooser.APPROVE_OPTION) {
         return;
      }

      File fileName = new File(SaveAs.getSelectedFile() + ".txt");
      BufferedWriter outFile = null;
      try {
         outFile = new BufferedWriter(new FileWriter(fileName));

         ta.write(outFile);   // *** here: ***

      } catch (IOException ex) {
         ex.printStackTrace();
      } finally {
         if (outFile != null) {
            try {
               outFile.close();
            } catch (IOException e) {
               // one of the few times that I think that it's OK
               // to leave this blank
            }
         }
      }
   }
//save file block

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

}  

Thanks


Related Solutions

Create a program that parses a CSV file of product data and prints the items with...
Create a program that parses a CSV file of product data and prints the items with a price that is less than or equal to that input by the user. • Your program should take two arguments: an input file to process and a price limit. • Print only the names of each item to stdout that have a price less than or equal to the given limit. • If the given file does not exist or cannot be opened,...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
C++ Create a recursive program what will test three recursive functions. It would have a menu...
C++ Create a recursive program what will test three recursive functions. It would have a menu like: 1. Recursive factorial 2. Towers of Hanoi 3. Recursive summation 0. Exit Enter selection: 3 Enter number: 4 The recursive summation will take an integer and sum value from 1 to the integer. Don’t enter a number if the selection is 0 for quit. Please have the functions in an implementation file and the prototypes of the functions in a header file and...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
1. create a .txt file and call it fruits.txt, add the following items to the file...
1. create a .txt file and call it fruits.txt, add the following items to the file Apple Banana Peach Strawberry Watermelon Kiwi Grape _______ 2. Write a program that does the following: - Reads the fruit.txt - Converts the contents into all uppercase - Writes the uppercased values into a text file called "UpperFruit.txt" OUTPUT SHOULD BE: APPLE BANANA PEACH STRAWBERRY WATERMELON KIWI GRAPE
Create a java program that has a code file with main() in it and another code...
Create a java program that has a code file with main() in it and another code file with a separate class. You will be creating objects of the class in the running program, just as the chapter example creates objects of the Account class. Your system handles employee records and processes payroll for them. Create a class called Employee that holds the following information: first name, last name, monthly salary, and sales bonus. The class should have all the gets...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside file there should be items: Open, Save, and Save As. Selecting open prompts the user to input a file name to a txt document containing employee information and displays a Jtable with the information, which can be edited. With column headers {"First Name" , "Last Name" , "Occupation" , "Office #"} Example: Gary Osbourn Teacher 113 Michelle Ramirez Teacher 101 Ava Gomez Principal 120...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT