opening a text file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: opening a text file

  1. #1
    Deepal Naidu Guest

    opening a text file


    Hello
    I'm having a little trouble with my first java project and would greatly
    appreciate some help. The task assigned is given below. Im currrently able
    to display the first line of text from a file onto a JTextArea, however i
    require the whole text from a file. The current code i have used is given
    below. Whether this is the right way to go about it, i dont know, hence the
    need for some help.

    CODE:
    private void openFile()
    {
    JFileChooser fileChooser = new JFileChooser();

    fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
    int result = fileChooser.showOpenDialog( this );

    if ( result == JFileChooser.CANCEL_OPTION )
    return;
    fileName = fileChooser.getSelectedFile();

    if ( fileName == null || fileName.getName().equals( "" ) )
    JOptionPane.showMessageDialog( this, "Invalid File Name","Q", JOptionPane.ERROR_MESSAGE
    );
    else {
    try {
    fileBuff = new BufferedReader( new FileReader( fileName ) );
    String line;

    while ( ( line = fileBuff.readLine() )!= null ) {
    JInternalFrame frame = new JInternalFrame( "Recipie Window2", true, true,
    true, true );

    Container c = frame.getContentPane();
    JTextArea t2 = new JTextArea();
    t2.append( line );
    t2.setFont( new Font( t2.getFont().getName(), style, 15 ) );
    c.add( t2, BorderLayout.CENTER );
    c.add( new JScrollPane( t2 ) );
    frame.setSize( 300, 300 );
    frame.setOpaque( true );
    theDesktop.add( frame );

    break;
    }
    repaint();
    }
    catch ( IOException e ) {
    JOptionPane.showMessageDialog( this, "Error Opening File","R", JOptionPane.ERROR_MESSAGE
    );
    }
    }
    }

    TASK:
    Create a programme that allows the user to retrieve Micowave recipies:
    it should:-
    1. allow user to retrieve recipies for at least 10 distinct dishes of your
    choice
    2. require the user to select
    a) the dish required
    b) the number of people for whom the dish should be prepared
    c) the power of the micrwave oven to be used
    3. using files to store the recipies
    4. make some other non-trivial extension of the program.



    Thanking you for your time

    Deepal



  2. #2
    Paul Clapham Guest

    Re: opening a text file

    If your text file contains say 12 lines, then you create 12 JInternalFrames
    and 12 JTextAreas. Each of those 12 JTextAreas contains 1 line of text.
    Instead, your code should first set up your user interface by creating 1
    JInternalFrame and 1 JTextArea and whatever else you need, and only then
    should you read in the text and put it into the JTextArea, like this:

    while ( ( line = fileBuff.readLine() )!= null ) {
    t2.append( line );
    }

    General rule when you are writing programs: try to do only one thing at a
    time.

    PC2

    "Deepal Naidu" <deepal_n@hotmail.com> wrote in message
    news:3adc7c87$1@news.devx.com...
    >
    > Hello
    > I'm having a little trouble with my first java project and would

    greatly
    > appreciate some help. The task assigned is given below. Im currrently able
    > to display the first line of text from a file onto a JTextArea, however i
    > require the whole text from a file. The current code i have used is given
    > below. Whether this is the right way to go about it, i dont know, hence

    the
    > need for some help.
    >
    > CODE:
    > private void openFile()
    > {
    > JFileChooser fileChooser = new JFileChooser();
    >
    > fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
    > int result = fileChooser.showOpenDialog( this );
    >
    > if ( result == JFileChooser.CANCEL_OPTION )
    > return;
    > fileName = fileChooser.getSelectedFile();
    >
    > if ( fileName == null || fileName.getName().equals( "" ) )
    > JOptionPane.showMessageDialog( this, "Invalid File Name","Q",

    JOptionPane.ERROR_MESSAGE
    > );
    > else {
    > try {
    > fileBuff = new BufferedReader( new FileReader( fileName ) );
    > String line;
    >
    > while ( ( line = fileBuff.readLine() )!= null ) {
    > JInternalFrame frame = new JInternalFrame( "Recipie Window2", true,

    true,
    > true, true );
    >
    > Container c = frame.getContentPane();
    > JTextArea t2 = new JTextArea();
    > t2.append( line );
    > t2.setFont( new Font( t2.getFont().getName(), style, 15 ) );
    > c.add( t2, BorderLayout.CENTER );
    > c.add( new JScrollPane( t2 ) );
    > frame.setSize( 300, 300 );
    > frame.setOpaque( true );
    > theDesktop.add( frame );
    >
    > break;
    > }
    > repaint();
    > }
    > catch ( IOException e ) {
    > JOptionPane.showMessageDialog( this, "Error Opening File","R",

    JOptionPane.ERROR_MESSAGE
    > );
    > }
    > }
    > }
    >
    > TASK:
    > Create a programme that allows the user to retrieve Micowave recipies:
    > it should:-
    > 1. allow user to retrieve recipies for at least 10 distinct dishes of your
    > choice
    > 2. require the user to select
    > a) the dish required
    > b) the number of people for whom the dish should be prepared
    > c) the power of the micrwave oven to be used
    > 3. using files to store the recipies
    > 4. make some other non-trivial extension of the program.
    >
    >
    >
    > Thanking you for your time
    >
    > Deepal
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center