Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Saving in Objects

Posted on 2002-04-02
2
Medium Priority
?
165 Views
Last Modified: 2011-09-20
I have a AWT Frame which consists 3 text fields
namely for ex:  emp Name,dept and salary . in addition the frame consists 2 buttons, save and exit.

When i enter the data in the Text Fields and click save
the data should be saved into an object and not inside a database table nor inside a .dat file.

Again I shud retrive back the data from the object and display the contents onto another Screen.

0
Comment
Question by:yrrm
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
msterjev earned 210 total points
ID: 6915242
You can create this object.

public class Credentals implements Serializable
{
     String name;
     String dept;
     String salary;
}

You store variables from text fields as data members of this class.

For example:

credentals.name=nameField.getText();
credentals.dept=deptField.getText();
credentals.salary=salaryField.getText();

The persistance is achieved using ObjectInputStream and ObjectOutputStream.

ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("credentals"));
out.writeObject(credentals);

At the start of the program you can retrieve the credentals with:

ObjectInputStream in=new ObjectInputStream(new FileInputStream("credentals"));
Credentals credentals=(Credentals) in.readObject();
0
 
LVL 3

Expert Comment

by:msterjev
ID: 6915740
The points....
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question