Advertisement

05.11.2008 at 02:44PM PDT, ID: 23393134 | Points: 125
[x]
Attachment Details

How do I read records  from a text file into a JTextArea in JAVA?

Tags: JAVA
Hi,
I am a beginning JAVA student.  I have a class that reads pet records from a text file into a vector and displays it to the screen.  I am enhancing the program to give it a gui interface.  I created a scrollable JTextArea in my GUI class.  I can't figure out how to read the records from the vector into my JTextArea.  Can you help?
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: b108
Question Asked On: 05.11.2008
Participating Experts: 3
Points: 125
Views: 0
Translate:
Loading Advertisement...
05.11.2008 at 04:19PM PDT, ID: 21543886

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.11.2008 at 04:26PM PDT, ID: 21543900

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.11.2008 at 04:43PM PDT, ID: 21543952

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.11.2008 at 06:11PM PDT, ID: 21544153

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.11.2008 at 06:29PM PDT, ID: 21544191

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.11.2008 at 07:19PM PDT, ID: 21544310

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.11.2008 at 07:40PM PDT, ID: 21544398

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.11.2008 at 08:57PM PDT, ID: 21544574

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.11.2008 at 09:23PM PDT, ID: 21544651

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 01:26AM PDT, ID: 21545302

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.11.2008 at 04:19PM PDT, ID: 21543886
You are responsible for doing your homework - EE rules restrict what we can do.

That said:

Check out the JTextArea class methods. They should point you in the right direction.

Take a shot at this assignment, and if you are having trouble, post your code here and we'll look at it.
 
05.11.2008 at 04:26PM PDT, ID: 21543900
No worries, I am writing my own code.  I am just trying to figure out how to do this.  I am getting an exception when the vector is being created.  Code snippets below.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
private class ReadListener implements ActionListener
    {
      public void actionPerformed(ActionEvent e)
      {
         
         String petName = " ";
         String petWeight = " ";
         String petAge = " ";
         
         // Determine which file was selected  
         if (readDogItem.isSelected())
         {
         	 //instansiate a pet object
         	NewPetRecord dog = new NewPetRecord();
         	dog.connectToInputFile(DOGFILE);
  		dog.ReadPetRecords();
  			
  		petName = dog.displayPetName();
  		name.setText(petName);
  			
        	dog.closeInputFile();
         }
 
 
from another class:
public String displayPetName()
	{
		PetRecord pet = (PetRecord) v.elementAt(1);
		return pet.getName();
	}	
Open in New Window
 
05.11.2008 at 04:43PM PDT, ID: 21543952
It's hard to figure out what's going on with incomplete info....

What exception are you getting when the vector is being created?

If you post the rest of the code, it can only help.
 
05.11.2008 at 06:11PM PDT, ID: 21544153
Here is the class that was originally created to read and write pet records from the keyboard.  It worked fine.  I'm just having trouble converting the IO to a GUI.
 
class to read and write binary pet records to a text file
 
 
05.11.2008 at 06:29PM PDT, ID: 21544191
Java offers many options for file i/o.

inputStream = new ObjectInputStream(new FileInputStream(inputFileName));

ObjectInputStream is useful for reading files previously written as ObjectOutputStream java objects, which might not apply here. If you are dealing with just regular text files, consider instead using these classes:

BufferedReader
FileReader


I'll wait to hear from you whether this is helpful before continuing; I suspect this will help you along.
 
05.11.2008 at 07:19PM PDT, ID: 21544310
Also, the ReadPetRecords method could be written in a more Java-y fashion....

String rec;

while ( (rec = inputFileObjectReaderThing.readLine()) != null) {
  process rec
}
 
05.11.2008 at 07:40PM PDT, ID: 21544398
Actually, I am reading a binary file, so I don't think I can use BufferedReader.   The code works fine as is without a gui.  What I am trying to do is get the data from the vector that I store the data in to my JTextField in my gui.
 
05.11.2008 at 08:57PM PDT, ID: 21544574
If its a JTextArea the you can either use myTextArea.append(s) to add to the current text area content, or myTextArea.setText(s) to clear all text and then show most recent text.

If you're using a JTextField, then you only have myTextField.setText(s).

Assuming you're using a JTextArea as with your original post, you could do something like:

public void displayDetail() {
  String output = '';
  Iterator iter = vector.iterator();
  while (iter.hasNext() == true) {
    (Object) obj = (Object) iter.next();
    output += '\n' + obj.toString() + '\n';
  }
  myTextArea.setText(output);
}
 
05.11.2008 at 09:23PM PDT, ID: 21544651
"I am getting an exception when the vector is being created."

I understand that the keyboard i/o (GetPetRecord) works ok. If the change to GUI creates this Vector exception, it's likely that there's a problem before you get to the GUI. Tchuki has given you the answer to a significant piece of the assignment. But are you at that point, or are you experiencing the Vector problem before you get there?
 
05.12.2008 at 01:26AM PDT, ID: 21545302
This :
1.
while (iter.hasNext() == true) {
- is tautologous - you only need : while (iter.hasNext()) {

2.
this won't work :  (Object) obj = (Object) iter.next();

- you can't declare and instantiate an Object using a cast. And if 'obj' has already been declared, there's no point casting it again.

3.
You wouldn't want to repeatedly declare a new object inside a loop anyway.

4.
This is incorrect : String output = '';
- String declarations need double quotes - " ".

5.
So all in all you'd only need :

     while (iter.hasNext()) {
              output += iter.next().toString()+'\n';      
      }
 - in a loop if you did it that way. ;)
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628