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 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...
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...
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...
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 menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program should allow the user to select one of the options according to whether lengths, weights or volume are to be converted, read the volue to be converted and the units, and then call the appropriate function to carry out the conversion In unit out unit I C (inch to centimeter 1 in = 2.4 cm) F C (feet to centimeter 1 ft = 30.4...
We need to create basic program that will simply display a menu and allow the user...
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT