Lab 7 Graphical User Interface (GUI)
Lab 7 Graphical User Interface (GUI)
Lab-07
Graphical User Interface (GUI)
Lab 07: Graphical User Interface (GUI)
Table of Contents
1. Introduction 64
4. Concept Map 65
4.1 AWT Packages 65
4.2 SWING Packages 65
4.3 Components & Containers 65
4.4 Event Handling 65
4.5 Creating a simple User Interface using Java AWT 66
4.5.1 Creating a component and adding it to container 66
4.5.2 Writing a simple GUI Counter Program using Java.awt 66
6 Procedure& Tools 69
6.1 Tools 69
6.2 Setting-Up IDE [Expected time = 10mins] 69
6.2.1 Download NetBeans 69
6.2.2 Install NetBeans 69
6.3 Walkthrough Task [Expected time = 50mins] 69
7 Practice Tasks 87
7.1 Practice Task 1 [Expected time = 20mins] 87
7.2 Practice Task 2 [Expected time = 20mins] 87
7.3 Bonus Practice Task [Expected time = 1 day] 88
7.4 Out comes 88
7.5 Testing 88
9 Evaluation criteria 89
10 Further Reading 90
10.1 Books 90
10.2 Slides 90
1. Introduction
Graphical user interface plays a very important role in the success of an application. GUI
provides an interface to the user which is user friendly and easy to operate. The GUI initiates
whole new level of programming which belongs to the category of event driven programming.
Event driven programming is the approach where every component of a program is registered to
the event handler. When the event occurs on a particular component then the handler associated
with it executes a block of code specially designed for that particular event.
A user interface is very important for every system that is being developed for interaction by the
user. In Java we can develop a very rich user interface. Java has predefined graphic classes
available in JDK for developing our own user interface. There are two main API’s for
developing user interface in Java.
AWT API ( Introduced in JDK 1.0 now most of the parts are obsolete and replaced by
swing)
SWING ( More Comprehensive, Enhanced state of AWT)
SWING and AWT have almost the same classes except one difference that the SWING classes
have character “J” as a prefix. For example JButton, JFrame etc.
In this lab we are going to work on making an attractive Graphical User Interface. You will also
learn how to use the Integrated Development Environment of Net Beans to develop the
application and interfaces quickly.
4. Concept Map
AWT stands for Abstract Window Toolkit. AWT is a Java API which contains 12 Java packages
two are most commonly used java.awt and java.awt.event.
Swing is also a Java API for GUI programming. It is bigger thenawtas it contains 18 Java
packages in jdk 1.7.
Primary Java GUI entities are called component like button, label, text field etc. Components are
customizable. Containers hold the components and sub containers in a specific layout. Like
frame and applet are the containers which can hold other components. Figure 1 shows the
components contained in the container.
When you press a button on an interface you are triggering an event. You write event listener to
control what will happen when the button will be pressed.There are two main parts of event
handling, a source like button or textfieldetc, and a listener associated with that source.
Component declaration
Initializing with appropriate constructor
Identifying the container (Frame or panel)
Example
In this example we will insert a main frame and inside that frame we will insert 3 component
A label with text “Counter”, a textfield which should be non editable and a button count.When
someone will press a button count it will add +1 in the count value. Following steps will guide
you how create a simple form represented in Figure 2.
import java.awt.*;
importjava.awt.event.*;
publicAWTCounter () {
setLayout(new FlowLayout());
// "this" Frame sets its layout to FlowLayout, which arranges the components
// from left-to-right, and flow to next row from top-to-bottom.
@Override
public void actionPerformed(ActionEventevt) {
++count; // increase the counter value
// Display the counter value on the TextFieldtfCount
tfCount.setText(count + ""); // convert int to String
}
}
After reading the reference material mentioned in the introduction, now you are ready to perform
homework assigned to you.
Develop the understanding of all the packages used in AWT and Swing.
Draw a hierarchy of component and container classes contained in AWT and Swing
packages.
Revise different layout managers
(https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html )
5.2.1 Task-1
Your task is to create a basic interface of small calculator, Insert a JFRAME Container and insert
3 textfields each with its label “First Number”, “Second Number”, “Results” respectively.Result
text field should be non-editable.Now insert 4 jbuttons “Multiply”, “Divide”, “Add”, “Substract”
each will the source of its event handler and perform a specified operation. And will show the
result in result text field.
5.2.2 Task-2
Find the component that best fits each of the following needs.
5.2.2.2 A component that displays an icon, but that doesn't react to user clicks.
5.2.2.3 A component that looks like a button and that, when pressed, brings up a menu of items for the
user to choose from.
5.2.2.4 Which method do you use to add a menu bar to a top-level container such as a JFrame?
5.2.2.5 Which method do you use to enable and disable components such as JButtons? What class is
it defined in?
5.2.2.7 Do those components use any other models to handle other aspects of the components' state?
If so, list the other models' types.
6 Procedure& Tools
In this section you will study how to create an event driven program using NetBeans IDE.
6.1 Tools
Go to the following link to download latest version of NetBeans IDE with Java SE
support.http://www.oracle.com/technetwork/java/javase/downloads/jdk-7-netbeans-download-
432126.html
Install the downloaded NetBeans in your desired location then follow directions given in the next
section.
3. Click on FileNew Project. New window of project will looks like Figure 4.
4. Choose the Java and then Java application then click Next. Figure 5 shows the window
that you will get after selecting Next Button.
5. Enter the project name, location and click Finish. The new window looks like Figure 6.
6. Click on + sign of Project Tab on left of screen to Add/ View the files.
7. Click on Source package newJava Package to enter the package name to which your
class files belong to. After entering the name click finish when you are done. Refer
toFigure 7 and Figure 8 for preview of the process.
9. New window will open up as shown in Figure 10. Enter the name of the form and click
finish.
10. We named the form “First” so a new window will open as shown in Figure 11.
11. Drag the Label Button from Right side Menu to Form as shown in Figure 12. After that
right click on the Label to edit text.
Figure 12: Drag and Drop Label and edit its text
12. Enter the Valid Field Name for example “USERNAME” as shown in Figure 13.
13. Same as the JLabel, now drag the text field from menu and right click on text field and
choose “CHANGE VARIABLE NAME” as shown in Figure 14
14. Enter the text field Name that is used for getting the input data. Preview is shown in
Figure 15.
15. Then right click on text field and choose “EDIT TEXT” as shown in Figure 16.
16. Remove data from that field.The text field will be empty.
17. Insert another label for password and then on right click choose “EDIT TEXT” to name it
logiocally.
18. Drag the “PASSWORD FIELD” and drop it on the form as shown in Figure 17.
19. Right click on password field and choose “CHANGE VARIABLE NAME” same s you
have already done for username field.
21. Right click on password field and choose “EDIT TEXT” and empty the field. Your form
should look like Figure 19.
23. Drag that field Button on the Form. It would look something like Figure 20.
24. Right click on Button and select “EDIT TEXT” to give some logical name to the button
like the other fields. In our case we will have two buttons. One is ok and the other is
cancel button like the form shown in Figure 21.
26. After creating the file. Again go to NetBeans and make a new form that is looks like the
form shown in Figure 26.
27. Create a file in which we will write a file reading code for the file that you have just
created. Right Click on “GUI PACKAGE” and select “JAVA CLASS” as shown in
Figure 27.
29. Write the file reading code to read from the file.
isExist=true;
break;
}
}
fr.close();
br.close();
return isExist;
33. Write the code in that space provided for the method and make another method for
visibility of Form. Here get method is used for visibility. Figure 31 shows what to write
in actionperformed method.
37. The GUI project is now complete.What is left is just a testing part.
39. After you run the project the window will open up as shown in Figure 34.
40. Enter some data in the input fields. The correct data will be the one record you entered in
the Access database. Figure 35 shows where to enter the data.
41. Press OK. If the username and password match with the database then a window showing
the successful login message will show up as shown in Figure 36.
7 Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You
need to finish the tasks in the required time. When you finish them, put these tasks in the
following folder:
\\dataserver\assignments$\Advanced Computer Programming\Lab13
Develop them through text file first and then IDE. (NetBeans or Eclipse)
Create a Java interface using a jframe and four components on top of that frame, with title
“Yellow”, “Blue”, “Green”, “Gray” when user click on any button, the frame background color
as well as of that button’s should change accordingly.Frame’s title should be COLORS.
Female Ideal Weight = Height2 /30, Male Ideal Weight = Height2 /28
Insert a main Jframe and two radio buttons for Male & Female Selection, and 5 Radio buttons
with labels with the range from 60 to 80 inches split each range with the difference of 4 and pick
the middle value. Also sets the default values of radio buttons.
Insert the Ideal weight text field, when user will change the selected radio button, it will update
the ideal weight read only text field. Your weight calculator should look something like the form
shown in Figure 37.
After completing this lab, student will be able to understand the usage IDE in developing event
driven applications.
7.5 Testing
This section provides you the test cases to test the working of your program. If you get the
desired mentioned outputs for the given set of inputs then your program is right.
Click on the buttons and check if the background colors change accordingly or not.
Input Output
Click on Green Button
Select different options from Gender and Height and check if the ideal weight value is according
to the formula
The lab instructor will give you unseen task depending upon the progress of the class.
9 Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).
10 Further Reading
10.1 Books
Text Book:
Java: How to Program by Paul J. Deitel, Harvey M. Deitel. Eighth Edition
Java Beginners Guide: http://www.oracle.com/events/global/en/java-outreach/resources/java-a-
beginners-guide-1720064.pdf
http://exampledepot.8waytrips.com/ for the package by package examples
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available
at \\dataserver\jinnah$\