• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read a text file and display in a Pane

 
Foni kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have my below Swing GUI code in which I want to read and display some contents present in text file say (test.txt) which is located in my local disk (C:\test.txt). My pane tab 2 is divided into two halves horizontally.Can anyone suggest me how can I read and write the test.txt file contents under the tab tab2 in the first half of the pane(where currently it is 2 written)?

Sample test.txt present in my local hard disk location


CODE FOR TABBED PANE


Thanks and Regards
Deb
 
Jarrod Rackley
Ranch Hand
Posts: 30
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With info like that I think a properties file would be better.

But I would say use java.io.File and then you could read and print that line by line, or grab chunks based off keywords.
 
Foni kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jarrod Rackley wrote:With info like that I think a properties file would be better.

But I would say use java.io.File and then you could read and print that line by line, or grab chunks based off keywords.


I am new to this concept of reading and writing from text file to the pane window. Can you please demonstrate some codes that will help?
 
Jarrod Rackley
Ranch Hand
Posts: 30
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a simple example that grabs a text file and prints it's contents.



The text file contains:



That should help get you started. You can also check out the java tutorials here and I have found some of the examples and tutorials here helpful as well.

**Edit**
Removed some unused attributes I had in.

**Edit 2**

Instead of doing System.out.println() you can print the text into a JTextArea, JLabel, or a number of other components that you can add to the JPanel. Also if you are going to mess with the String a lot, it might be easier to use a StringBuilder instead of just String.
 
Foni kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jarrod Rackley wrote:Here is a simple example that grabs a text file and prints it's contents.



The text file contains:



That should help get you started. You can also check out the java tutorials here and I have found some of the examples and tutorials here helpful as well.

**Edit**
Removed some unused attributes I had in.

**Edit 2**

Instead of doing System.out.println() you can print the text into a JTextArea, JLabel, or a number of other components that you can add to the JPanel. Also if you are going to mess with the String a lot, it might be easier to use a StringBuilder instead of just String.


Hi Jarrod ,

Thanks for the reply but can you suggest how can I add a text Area to my below panel code since I tried to do it but it is not working? Here I want to add a text area with some heading say [b]"Details" under my panel (TAB2) and then I want to populate the text.txt file contents in that.[/b] . Can you please suggest how can I add a text area in my below code.


 
Jarrod Rackley
Ranch Hand
Posts: 30
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is TextAreaDemo supposed to be a method, constructor or inner class?

If constructor it needs to be named what your class is named. If it's an inner class you should remove the public modifier and add class instead. If it's a method you need a return type or void.


I might be wrong on this, but unless your class extends something like JFrame your code



Will not work.


But for your wanting to add a JTextArea to TAB2



should work for you. Don't forget to setup that layout for it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!