CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2008
    Posts
    1

    Please Help - Writing JFileChooser with Exception Handling

    I have been trying to write a program that will loop until a user selects a file with a Try - Catch statements to no success. If user hits Cancel then I need NullPointerException or if no file found I need a FileNotFoundException

    Here my Instructions:

    ****************Instructions**************

    Write a GUI program that prompts the user to browse and select a file. Then read the selected file using a Scanner object, a line at a time. Copy the file contents and display it in a text area component. If the user did not select a file, then inform the user (using a pop-up window) that a file must be selected and then prompt the user to select a file. Repeat this until the user selects a file. Provide (copy and paste) your Java Source codes.

    • If selected file does not exist, then scanner object may throw a FileNotFoundException.
    • If user selects “Cancel” option in the file open dialog, the scanner object may throw a NullPointerException
    • Think carefully about how to use the try/catch structure in combination with a loop that prompts the user to browse and select a file.

    ****************Instructions****************

    I have this much so far

    *****************************
    //********************************************************************
    // DisplayFile.java Author:
    //
    //
    //********************************************************************

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

    public class DisplayFile
    {
    //-----------------------------------------------------------------
    // Opens a file chooser dialog, reads the selected file and
    // loads it into a text area.
    //-----------------------------------------------------------------
    public static void main (String[] args) throws IOException
    {
    JFrame frame = new JFrame ("Display File");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    JTextArea ta = new JTextArea (20, 30);
    JFileChooser chooser = new JFileChooser();

    int status = chooser.showOpenDialog (null);

    ******************************************
    Please help
    Last edited by verbalh; April 19th, 2008 at 04:56 PM. Reason: more info

  2. #2
    Join Date
    Dec 2010
    Posts
    1

    Re: Please Help - Writing JFileChooser with Exception Handling

    From int status = chooser.showOpenDialog (null);

    if (status != JFileChooser.APPROVE_OPTION)
    ta.setText ("No File Chosen");
    else
    {
    File file = chooser.getSelectedFile();
    Scanner scan = new Scanner (file);

    String info = "";
    while (scan.hasNext())
    info += scan.nextLine() + "\n";

    ta.setText (info);
    }

    frame.getContentPane().add (ta);
    frame.pack();
    frame.setVisible(true);

    }
    }

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Please Help - Writing JFileChooser with Exception Handling

    Thanks for helping out but given that the question was posted 2.5 years ago I'd guess the OP has either found the answer or given up by now. BTW We try not to give code answers to peoples homework questions, guide them by all means but please don't just provide code.

    Please help by answering some of the current questions.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured