1,105,456 Community Members

Display Textbox (JOptionPane) multiples lines of text

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am trying to design a file reader with some sort of GUI so I decided to use JOptionPane. I am a complete noob trying to learn java.

So far, I have managed to make my program produce a pop up for the file location, and also an error message if the program can't locate the file.
However, I cant get the results to display in one single box, it displays one for each line as I could only get it to work in the loop which would obviously do that. How do I go about displaying all the lines from the file in one output box?

package readtextfile;
import java.io.IOException;
import javax.swing.JOptionPane;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        String file_name = JOptionPane.showInputDialog(null, "What's the File Location?");
                
        
        //"C:/Users/MyBook/Documents/TestProgram/testdocument.txt"

        try
        {
            ReadFile file = new ReadFile(file_name);
            String[] arrayLines = file.OpenContents();

            int i;
            for (i=0; i<arrayLines.length; i++)
            {
                JOptionPane.showMessageDialog(null, arrayLines[i]);
            }
        }

        //if there is an error, catch will produce error message
        catch (IOException errorMessage) //errorMessage is a variable, IOException type
        {
            JOptionPane.showMessageDialog(null, "File Not Found!");
            System.out.println(errorMessage.getMessage());
            System.out.printf("File %s can not be found.\n\n", file_name ); //getMessage is a method of IOException
        }
    }
}
package readtextfile;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class ReadFile
{
    private String path;

    public ReadFile(String file_path)
    {
        path = file_path;
    }


    int readLines() throws IOException
    {
        FileReader file = new FileReader(path);
        BufferedReader buffer = new BufferedReader(file);

        String aLine;
        int numberOfLines = 0;

        while ((aLine = buffer.readLine() ) !=null) //read each line until null value reached
        {
            numberOfLines++;
        }

        buffer.close();
        return numberOfLines;
    }

    public String[] OpenContents() throws IOException
    {
        FileReader fr = new FileReader(path);
        BufferedReader buffer = new BufferedReader(fr);

        int numberOfLines = readLines();
        String[] textData = new String[numberOfLines];

        int i;

        for (i=0; i<numberOfLines; i++)
        {
            textData[i] = buffer.readLine();
        }

        buffer.close();
        return textData;
    }
}
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
1
 

How do I go about displaying all the lines from the file in one output box?

Concatenate the lines together separated by new line characters (\n)

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i know how to do it like this:

String message = String.format("What's the File Location?\neg: C:/User/Desktop/Test.txt");
        String file_name = JOptionPane.showInputDialog(null, message);

but not when its an array? can you give me any more advice?

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Remove the lines from the array.

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thats what Im stuck on.. i tried to do that but the best I could do was get the last line of text from the array as the loop kept writting over the previous entry.

I am a complete newbie at this as I have only just started learning this. Could you possibly show me how its done? :)

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Write a loop that goes thru the array and gets its elements one at a time and concatenates that element to the String to be shown in the JOptionPane.
After the loop ends, show the String.

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

how do i do it so that it doesn't overwrite the previous entry in the string? I tried that and just keeps overwritting the previous entry and showing me the last line in the txt file.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Post the code that shows what you are doing.

Do you know how to concatenate Strings?
aString += "add this to the String";

Member Avatar
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks that did the trick! the book I am using hasn't mentioned how to conatenate Strings yet so didnt know to do that, thanks for your help! :)

heres what I ended up doing:

try
        {
            ReadFile file = new ReadFile(file_name);
            String[] arrayLines = file.OpenContents();
            String displayMessage = null;
            

            int i;
            for (i=0; i<arrayLines.length; i++)
            {
                displayMessage += arrayLines[i];
                displayMessage += "\n";
            }
            JOptionPane.showMessageDialog(null, displayMessage);
Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Glad you got it to work.

Question Answered as of 2 Years Ago by NormR1
Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,346 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 

better would be look at JTextArea#write(Writer out) throws IOException then put JTextArea to JOptionPane

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: