Swing Form in Java


Create a form using JFrame having 2 fields namely Name and Remarks. Also create a button called Insert. This button will insert these data from the form in a text file and later on read the same data and print it. If more number of records are entered it should be appended to the same file. Assume that the text file is already present. Also notify the Exceptions thrown by the above program and give appropriate messages there.
Packages used:
1.      javax.swing.*              : Provides a set of "lightweight" (all-Java language) components that, to the maximum degree possible, work the same on all platforms.
2.      java.awt.*                    : Contains all of the classes for creating user interfaces and for painting graphics and images.
3.      java.awt.event.*          : Provides interfaces and classes for dealing with different types of events fired by AWT components.
4.      javax.swing.event.*    : Provides for events fired by Swing components. It contains event classes and corresponding event listener interfaces for events fired by Swing components in addition to those events in the java.awt.event package.
5.      java.io.*                       : Provides for system input and output through data streams, serialization and the file system.
Classes used:
·         Conatainer              : component that can contain other AWT components. Components added to a container are tracked in a list
·         JLabel                           : Object which can display text,image or both.
·         JTextField                   : A lightweight component that allows the editing of a single line of text
·         JButton                       : An implementation of a "push" button
·         FileOutpustStream      : A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform.
Logical flow of the program:
·         The frame holds two textfields to accept the name and remarks from the user.
·         The text file to which data is to inserted is opened with the object of the class FileOutputStream.
·         To append the data to the text file the object of FileOutputStream must be initialized with the following constructor.
public FileOutputStream(String name,boolean append)”.
If the second parameter which accepts a Boolean value is “true”,then the data is added to end of the text file rather than at the beginning.
·         Object of DataOutputStream is defined to write the data obtained to the text file.


Important notes:
·         In all swing program the package that should be imported is “javax.swing.*”.
·         javax.swing.event.*” package is imported to perform events in swing program.
·         If in the program JFrame is inherited then the inheriting class’s constructor initializes the swing application.
1.If super() method is called ,it should be the first line inside the constructor.
2.Frame’s visibility and size must be set.
·         If the objects of components are declared before the constructor,only initialization of those objects are done inside the constructor.if the objects are declared twice that would generate exception.

0 comments:

Copyright © 2012 OpenTechZone | Kesari Technologies |