In: Computer Science
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.
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