Saturday, December 26, 2009

My First Java Swing based Application Part II with Modifications

Now, we have rough outline of the application as shown before, but due to the requirement of new features, some modifications have been made to the GUI.

Modification 1:
A new JTextField and JLabel have been added to input book publisher data. They have been named as publisherTF and publisherLabel respectively.

Modification 2:
One new JButton has been added to clear the contents of the textfield's. The JButton, has been appropriately named as "Clear".

Modificaiton 3:
The radiobuttons, under Delivery Time have been renamed from '7-14 working days' and '20 working days' to '7-15 working days' and '20-30 working days' respectively.

Modification 4:
The overall dimensions of the JFrame have been modified to fit the needs of application. The width of all the textfields, and placement of the other components also are adjusted as per the requirement.

Designing the Code Logic
To design the logic of the application, let us understand the purpose of the application once again, clearly.

The program is designed to format the given inputs about a book, into a book description in HTML format. This HTML template will be used for display in Ebay India website.

Now, let us outline all the inputs about the book, that we are going to consider in our application
  1. Title
  2. Author(s)
  3. ISBN-10
  4. ISBN-13
  5. Pages
  6. Publisher
  7. Format
  8. Publisher's note or Book Description &
  9. Delivery Time
All the above inputs, will be added in corresponding locations within the HTML template and the completed HTML code will be generated in the Description TextArea on pressing the Generate button.

Placing the Inputs
To place the inputs, appropriately in a given location in HTML document, I have created a new class called DescriptionParts, that consists of a set of strings. These strings are parts of the whole HTML code formatted, minus the inputs. When all the strings are combined in a specified order along with the inputs, we get the complete HTML code which can be used on the website. The process of combining the inputs with the HTML code is done on clicking the Generate button. The combining process is done with the help of StringBuilder object. With the help of StringBuilder's append() method, a final string(completed HTML code) is built.
  • The Strings in Description Parts are broken appropriately, so that the inputs from the GUI are placed correctly
  • When the Generate button is clicked, append calls are made one after the other to add the Strings and inputs.
finalCodeObject.append(DescriptionPartsObject.anyStringVariable);
finalCodeObject.append(JTextFieldObject.getText());
The above syntaxes are used for appending strings from DescriptionParts and from inputs respectively, one after the other.

Absence of Data
If due to any reason. some textfields namely publisher, format & ISBN-10 are not filled, then the application will built the final book description by omitting these fields. To program this logic, I am using length() method of String class, to check if the textfield is empty or not. The syntax for coding this is:
if( textFieldObject.getText().length()!= 0 ){
.........
}


Event Actions
Generate Button
In the above image, you can see part of the code, that is used for appending strings from DescriptionParts and Data Inputs from the text-fields. All this code is defined in the method generateButtonActionPerformed(java.awt.event.ActionEvent evt). This method runs, only when the generate button is clicked.
To reach this method in the source-code directly from the design interface, follow the steps below
  • Right-click on the generate button,
  • Then, move the mouse cursor over events,
  • Next, select Action &
  • Finally, click on Action Performed

ClearButton
The 'Clear' button is used to clear the contents of the textfields. The clearButtonActionPerformed() method, assigns all text-fields to null, using the JTextComponent's setText() method.
Syntax:
   jTextFieldObject.setText(null);

The Clear button also, sets the delivery time to 7-15 working days by default.

Delivery Time RadioButtons
The 2 radio buttons used for Delivery Time should be mutually exclusive. That is, when one button is selected the other should be deselected. This is implemented by coding the selection and deselection logic in the actionPerformed() methods of the respective radio-buttons.

The Constructor
  • Initializes the GUI components
  • Selects 7-15 working days (Delivery Time) radio button


Reference Links:
My First Java Swing base Application

No comments:

Post a Comment