Showing posts with label Java Swing. Show all posts
Showing posts with label Java Swing. Show all posts

Monday, February 14, 2011

Java Swing applications -- Telephone and Address Book -- II

Implementation:


The Telephone Book application is now designed and we need to just put the coding for that. We already know which objects in swing package are used as the components in the panels for the application. For example, we have mentioned in the last post about some components such as Drop-down list (JComboBox), Labels for the appropriate components(JLabel), Buttons (JButton), Text-Boxes (JTextBox), Scroll Panes (JScrollPane), Results displayed in Tables (JTable), Confirmation of the results through Dialogs (JDialog) etc.

The Front end is Java Swing while the application has a database connectivity with MS Access database. As mentioned in the previous post, there is only one table in the database. The coding is done in two packages: 
  1. TeleAddress package for Component design and Action listeners &
  2. dao package for accessing database from the MS Access database through JDBC API
In all the classes, the constructors play a key role with initializing the frame window processing purposes. Mouse action listeners are applied to the buttons in the interface that interacts with the user, most of them being applied when the user clicks the mouse.

The confirmation dialogs are present in a separate class, named Dialogs with separate dialogs for contact creation, updating contacts and contact-deletion. The Dialogs class contains the following:
  1. Two Constructors -- one for Deletion and the other for Updation / Addition of contacts
  2. dialog( ) and deldialog( ) functions for processing one of the two constructors.
The TelephoneBook class in TeleAddress package is the main class in the application and binds all the classes together. The TelephoneBook class contains the following:
  1. All the Interface-Components such as Buttons, Drop-down lists, Text-Boxes,Labels,Tables etc. are defined in this class.
  2. 5 Panel functions for adding the components into each panel.
  3. A Constructor having function calls to the Action-listeners.
  4. A function named initComponents( ) for the lay-out of components.
  5. All Action-listeners for all the buttons such as Create, Delete, Update, Refresh, Cancel etc.
The SearchTable class allows the user to view the results in a table in a different Frame window. The Database class consists of all the database-connections, where we use the JDBC-ODBC Bridge Driver for connecting to MS Access. The static methods in this class are used for every database operation being done in the application. The dao package makes use of PreparedStatement and Statement for database query executions. The Database connectivity with MS Access can be done as shown clearly in these posts as part 1, part 2 and part 3. The dao package has the data access objects listed below:
  1. Database class for Database connection.
  2. AddDAO class for insertion of new contact into the database table.
  3. DeleteDAO class for deletion of contact records from the database table.
  4. OtherDAO class to get all the records from the database table
  5. SearchDAO class to facilitate searching upon inputs given in the Search and Delete panels.
  6. UpdateDAO class for updating contact details.
The documentation for the classes of this application have been automatically generated by NetBeans IDE through Javadocs which can be downloaded here.

Installation and Configuring the application:

Pre-requisites:
  1. The system should have Java (JDK and JRE) installed on your system, which can be downloaded from this page.
  2. The system should have Microsoft Office Access DBMS.
Configuring and Installation:
  1. Invoke Control Panel -- Administrative Tools -- Data Sources (ODBC). Go to UserDSN tab and click on Add button.
  2. Select Microsoft Access Driver (*.mdb, *.accdb) and click on Finish button.
  3. Give the Data Source Name as TeleAddressBook and select the Database Select button and give the path of the database file which can be downloaded from here. After selecting the .accdb file, click OK. Any doubts regarding the configuration of DSN (Data Source Name) can be clarified with the tutorial specified previously at this post with screen-shots.
  4. Download the JAR file which can be downloaded here.
  5. After configuring the DSN and downloading the JAR file, double-click on the JAR file to invoke the application. If its not invoked with the double-click then open Command-Prompt and type the one specified as: java -jar "/* Complete Path of the JAR file */"

    Sunday, February 13, 2011

    Java Swing applications -- Telephone and Address Book -- I

    Before starting the design and implementation of Telephone cum Address Book, I recommend you to go through the following post which gives an insight of how a layout should be in Java Swing.
    How to choose a layout in Java Swing

    It is quite common to have a telephone book to note the contact numbers, addresses and other notes of relatives, colleagues, acquaintances and so on. The search operation still has be improved under such circumstances. We need to find the contact quickly in the telephone-contacts book. In addition to that, updation, deletion of contacts is quite cumbersome in such circumstances. I have come up with a typical CRUD (Create, Read, Update, Delete) kind of an application built on Java -- a Java Swing application to be precise, which provides the following:
    1. Create / Add a new contact to the Telephone book
    2. Update the details of a contact
    3. Delete the contact details
    4. Search functionality based on many parameters such as Name, City, Contact Numbers, Address etc.
    5. List the entire list of contacts
    6. Refresh capability for the changes to be applied to the database.

    Advantages over the normal telephone book:

    1. Quick search and display of details
    2. Easy updation and deletion of contacts
    3. Robust contact addition

    User Interface Analysis and Design:

    The entire application is built on tabs, rather than using Menus and sub-menus in the application as most of them do. But to avoid complexity in the application, the results are maintained in tabs (JPanel). The results of the search and listing functionality are displayed in a Scroll Pane (JScrollPane). The items in individual panels are programmed in separate functions to provide easy maintainability. There are 5 panels in the application which are listed below:
    1. Create Panel
    2. Delete Panel
    3. Update Panel
    4. Search Panel
    5. List Panel
    The search results and the listing operations are displayed in a tabular format (JTable), with 7 different contact details. The operations on the table can be made by clicking on that particular cell and deleting the corresponding record. The table is added to the Scroll Pane (JScrollPane) with vertical and horizontal scroll bars as needed.

    The form-elements in the panels such as Create, Update and Search Panels is arranged in Grid Layout. Go through the guidelines to use Grid Layout. All the guidelines listed to use Grid Layout will have to be followed for better look and feel. The text-boxes (JTextArea) and buttons (JButton) are mainly used in all these forms, with the drop-down lists (JComboBox) also being added to the Search and Update Panels for better functionality and ease of use. The screen-shots can be downloaded here.

    Database Analysis and Design:

    This is a simple application with only a few contact-details which allows us to design the entire database using a single table. The main motto of this series of posts is to work with Java Swing and database is the easiest way to access data. So, we make an assumption that a contact has only single Office, Residence and Mobile contact numbers. The Normalization is not needed as per this assumption and a single table prevents the joining of tables. The database table telephone consists of fields listed below:
    1. Contact-Name
    2. Street, Area and City details of the person's residence
    3. Office, Residence and Mobile phone numbers 

    Saturday, July 24, 2010

    Java Tips: How to use Grid Layout in Java Swing.

    They are many types of layouts in Java. But arranging in a tabular order needs us to use Grid Layout. Grid layout in Java Swing is something in a frame that arranges the components in the form of rows and columns. The size of the components, especially buttons, text boxes change as we resize the actual JFrame window. So, when the window is maximised, the components will be distributed accordingly resulting in unusual look and feel. For example, just imagine you have added 4 buttons to a frame by Grid Layout. You would see everything on the frame filled with buttons. This is very awkward to view and has to be avoided.

    Thus, it is necessary to fix the components' sizes according to the application or the program. That is, grid layout is good looking as far as the components are of sufficient size not large and not small. In addition to the way the components size is, the resizing of the frame would make it look very awkward which results in the end-user dissatisfaction and an ugly user interface.

    Fortunately, there is a solution to this problem. There are two things which you have to remember while using the Grid Layout in Swing.
    1. Set the borders of the frame so, that the components are of good size. This can be done by this piece of code:
      setBorder(BorderFactory.createEmptyBorder(top_margin, left_margin, bottom_margin, right_margin));
    2. After setting the borders, you have to decide the size that the JFrame should be. You can set size by using setSize(width,height).
    3. The most important thing in this discussion is to avoid it from being resized. You can avoid resizing of frame window by the following function: setResizable(false);
    Now , you can enjoy all the privileges that Grid Layout provides without being much concerned about the look and feel.
    Related Posts Plugin for WordPress, Blogger...