-2

I am writing a code in java to display text file in JTextArea. Can Anyone tell me what is wrong with this code. It is saying cannnot find symbol file..

FOpen.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            final JFileChooser FileDialog = new JFileChooser();
            int ReturnValue=FileDialog.showOpenDialog(null);

            if(ReturnValue==JFileChooser.APPROVE_OPTION)
            {
                File file = FileDialog.getSelectedFile(); 
            }
            BufferedReader in = new BufferedReader(new FileReader(file));
            String line = in.readLine();    
            while(line!=null)
            {
                WritingArea.append(line+"\n");
                line=in.readLine();
            }
        }
    }); 
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Belal Khan
  • 2,099
  • 2
  • 22
  • 32
  • 3
    Look up 'variable scope'. – Andrew Thompson Dec 27 '13 at 13:38
  • Just put everything that's under the if statement, into the if statement, and you should be fine. You really do need to learn scopes though. The error tells you exactly whats wrong – Paul Samsotha Dec 27 '13 at 13:44
  • When I declare the file above if File = file; I get an error that file not been initialized??? – Belal Khan Dec 27 '13 at 13:46
  • @BelalKhan - DO NOT edit the Question in a way that destroys its original meaning. I'm reverting your bad edit. If you have a follow-up Question either ask a new Question, or edit the existing Question to *add* the follow-up. Do it *properly* please. – Stephen C Dec 27 '13 at 14:20

2 Answers2

4

Look up 'variable scope/visibility'. Since the file attribute is declared inside the brackets, only code in that code block has access to it.

Other notes/tips:

  • If ReturnValue!= .. it does not make much sense to continue with the rest, so the rest of that method should also be inside the brackets.
  • That code will bloc the EDT for as long as it takes to load the File. Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling Thread.sleep(n) implement a Swing Timer for repeating tasks or a SwingWorker for long running tasks. See Concurrency in Swing for more details.
  • Please learn common Java naming conventions (specifically the case used for the names) for class, method & attribute names & use them consistently.
  • if WritingArea is a JTextComponent there is an easier way to load the data.
  • Always copy/paste error & exception output.
  • For better help sooner, post an SSCCE.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

You really need to learn about scopes. Currently you File object is encapsulated in the if block's scope. If you want to use that File object anywhere else, it isn't allowed. So put everything into the if block, where they will be in the same scope as the File object

FOpen.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent ae)
    {
        final JFileChooser FileDialog = new JFileChooser();
        int ReturnValue=FileDialog.showOpenDialog(null);

        if(ReturnValue==JFileChooser.APPROVE_OPTION)
        {
            File file = FileDialog.getSelectedFile(); 
            BufferedReader in = new BufferedReader(new FileReader(file));
            String line = in.readLine(); 

            while(line!=null)
            {
                WritingArea.append(line+"\n");
                line=in.readLine();
            }
        }  
    }
});
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • *"Am getting different error now"* Then you should select an answer for this one, and *ask a new question!* SO is not a help-desk, it is a Q&A Site.. – Andrew Thompson Dec 27 '13 at 14:16