1

I have a menu bar with 3 menus. Each menu has a menu item. Today and Close items are working very good, but the xyz.txt item is not working.

When I click the xyz.txt item I get this exception: Exception:

java.io.FileNotFoundException: xyz.txt (The system cannot find the file specified)

The xyz.txt file is placed in "AWT menu" project and the MenuDemo class is placed in AWT menu/src/awtmenu. I've tried to put the xyz.txt file in src or in awtmenu package but the problem persists.

Where should I place the xyz.txt file?

Any feedback will be apreciated!

This is the code:

import java.awt.Color;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileReader;
import java.io.IOException;

public class MenuDemo extends Frame implements ActionListener
{
  TextArea ta;
  public MenuDemo()
  {                      // create menu bar
    MenuBar mBar = new MenuBar();
    setMenuBar(mBar);   // add menu bar to frame
                                // create menus
    Menu files = new Menu("Files");
    Menu date = new Menu("Date");
    Menu exit = new Menu("Exit");
    ta = new TextArea(10, 40);
    ta.setBackground(Color.cyan);
                              // add menus to menu bar
    mBar.add(files);
    mBar.add(date);
    mBar.add(exit);
                             // create menu items to menus
    MenuItem mi1 = new MenuItem("xyz.txt");
    files.add(mi1);                 

    date.add(new MenuItem("Today"));
    exit.add(new MenuItem("Close"));
                               // linking listener
    files.addActionListener(this);
    date.addActionListener(this);
    exit.addActionListener(this);

    add(ta, "Center");

    addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent e) {

            System.exit(0); 
        }
    });

    setTitle("Menu Practice");
    setSize(400, 400);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent e)
  {
    String str = e.getActionCommand();

    if(str.equals("Close"))
    {
      System.exit(0);
    }
    else if(str.equals("Today"))
    {
      ta.setText("Today: " + new java.util.Date());
    }
    else
    {
      try
      {
        FileReader fr = new FileReader(str);
        ta.setText("Folloiwing are file contents:\n\n");
        int temp;
        while( (temp = fr.read()) != -1)
        {
          char ch = (char) temp;
          String s1 = String.valueOf(ch);
          System.out.println(s1);
          ta.append(s1);
        }
        fr.close();
      }
      catch(IOException e1)  
      {
        ta.setText("Exception: " + e1);
      }
    }
  }

  public static void main(String args[])
  {
    new MenuDemo();
  }
}
  • Is `xyz.txt` meant to be user editable? What type of information does it contain? – Andrew Thompson May 09 '18 at 16:15
  • 1
    Also.. Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. N.B. One of the great things about Swing, given the need to read files into text components, are methods like [`JTextComponent.read(Reader,Object)`](https://docs.oracle.com/javase/9/docs/api/javax/swing/text/JTextComponent.html#read-java.io.Reader-java.lang.Object-)! ;) – Andrew Thompson May 09 '18 at 16:17
  • 1
    https://i.imgur.com/z3m246i.png My file system can find the file normally... – George Z. May 09 '18 at 16:23
  • @GeorgeZougianos *"My file system can find the file normally..."* How do you package the app. for others to use? – Andrew Thompson May 09 '18 at 16:32
  • @AndrewThompson I can't understand what you are asking clearly. I just did a new project into eclipse, and created the txt inside this project's folder (as i show in the image). Then i pasted the given code and i get no exception – George Z. May 09 '18 at 16:35
  • @GeorgeZougianos Forget yourself & your IDE for a moment. I'm asking - how does your code run for any **user** that's **not on your computer?** Do they have to install Eclipse? Do they have to make a project, paste the code in, run it? There is a reason for asking, and the problem likely applies to both your code & the OP's. BTW - If you don't already know where I'm going by asking about the user, it's a pretty good indication that you have little to contribute to this immediate problem (though you might learn something). – Andrew Thompson May 09 '18 at 16:39
  • @AndrewThompson OP says that he tried to place the txt file in /src folder and inside the package of his class. Well, i placed in the parent folder of the src folder and i had no exceptions. So i guess he should do the same and place the txt in AWT menu/cev.txt ? or least he should give a try. – George Z. May 09 '18 at 16:46
  • *"So i guess he should.."* ..answer the two questions in my first comment, so we can approach the actual requirement with all necessary information. Please either start *your own* question, or be patient and see how this question plays out. – Andrew Thompson May 09 '18 at 16:56
  • Ok, i will be patient and see how this question plays out. – George Z. May 09 '18 at 17:06
  • @GeorgeZougianos Thank you for replay. it was my mistake, I saved the file xyz.txt.txt and this was the problem...now the app is working –  May 09 '18 at 18:46
  • @AndrewThompson the file contains some characters. I know swing is better than awt but I'm a beginner and I want to understand awt first and after few days I will learn about swing. Thank you for replay, it was my mistake, I saved the file xyz.txt.txt and this was the reason for the exception, now the app is working –  May 09 '18 at 18:48
  • @gaby no problem. Im glad you solved the problem. – George Z. May 09 '18 at 18:49

0 Answers0