Results 1 to 11 of 11

Thread: jfilechooser{resolved}

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Resolved jfilechooser{resolved}

    How would you open a (text)file and display it's contents in a jtextarea?
    Last edited by System_Error; Nov 20th, 2004 at 08:47 AM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Open the file using a FileReader or perhaps FileInputStream + InputStreamReader (if the encoding is weird). Read it completely in. Set the text of the JTextArea using setText(String).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    I should have been a little more clear with the question. I want the user to choose a file using the jfilechooser or showdialog method and then display the file in the textarea. Would I still use the FileReader in this case? That is after I get the selected file the user wants to view?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes. The JFileChooser only comes in when selecting the file name of the file to load.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    Thank you so much for your help.

  6. #6

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    O.K. I just got everything working and now it's only reading one line of the file. I want it to read the whole thing. Could someone please see what I need to do?

    Code:
    public void processOpen()
     {
    	 /* process Opening a new file
    	 */
    	 BufferedReader br = null;
    	 StringTokenizer st;
    	 JFileChooser chooser = new JFileChooser();
    	 int returnVal = chooser.showOpenDialog(this);
    	 String getFile = chooser.getSelectedFile().getPath();
    	 if(returnVal == JFileChooser.APPROVE_OPTION)
    	 {
    		 try {
    			 FileReader filegetter = new FileReader(getFile);
    			 br = new BufferedReader(filegetter);
    			 String line = br.readLine();
    			 st = new StringTokenizer(line);
    			 while (st.hasMoreTokens())
    			 {
    			 textArea2.append(st.nextToken());
    			 }
    		 }
    		 catch (FileNotFoundException ex)
    		 {
    			 ex.printStackTrace();
    		 }
    		 catch (Exception myE)
    		 {
    			 myE.printStackTrace();
    		 }
    		 finally
    		 {
    			 try{
    				 if (br != null)
    				 {
    					 br.close();
    				 }
    			 }
    			 catch(IOException io)
    			 {
    				 io.printStackTrace();
    			 }
    		 }
    	 }
    	 
     }

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You need to call readLine in a loop.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    Could you give me a pointer on what the loops suppose to look like? I don't know really know how to go about it.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Here's how to get all of a file into a string:
    Code:
    	Reader r = new InputStreamReader(new BufferedInputStream(
    		new FileInputStream("obj.xml")), Charset.forName("UTF-8"));
    	char[] buffer = new char[1024];
    	StringBuffer loaded = new StringBuffer();
    	int res;
    	do {
    		res = r.read(buffer);
    		if(res > 0) {
    			loaded.append(buffer, 0, res);
    		}
    	} while(res == 1024);
    	buffer = null;
    	String content = loaded.toString();
    A few notes:
    1) I chose to do the buffering before the stream->reader conversion. It shouldn't really matter either way.
    2) This code loads UTF-8 files. If that's not what you want (text files in Windows are saved as Windows-1215 or something like that by default), you need to change the value in the Charset.forName call.
    3) For large files, this requires a very large amount of memory. It might be wise to do the parsing chunkwise in some fashion and discard the chunk afterwards.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    Thank you so much for taking your time to help me.

  11. #11

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    I found an even easier solution:

    Code:
     JFileChooser chooser = new JFileChooser();
    	 int returnVal = chooser.showOpenDialog(this);
    	 String getFile = chooser.getSelectedFile().getPath();
    	 try {
    		 textArea2.read(new FileReader(getFile),null);
    	 }
    	 catch(IOException e)
    	 {
    		 e.printStackTrace();
    	 }

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