We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Display Textbox (JOptionPane) multiples lines of text

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;
    }
}
3
Contributors
10
Replies
18 Hours
Discussion Span
1 Year Ago
Last Updated
12
Views
Question
Answered
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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)

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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?

akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Remove the lines from the array.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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? :)

akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Post the code that shows what you are doing.

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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);
akingcool
Newbie Poster
22 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Glad you got it to work.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Question Answered as of 1 Year Ago by NormR1

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

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0853 seconds using 2.69MB