Advance Java Unit 1 Prof Akhil M Jaiswal
Advance Java Unit 1 Prof Akhil M Jaiswal
Jaiswal
Advance Java
Introduction & Overview
Unit 1 Notes
Objectives
• Develop Programs using GUI Framework
• Handle events of AWT and Swings Component
• Develop Programs to handle events in Java
Programming
• Develop Java Programs using Networking
Concept
• Develop programs using Database
• Develop programs using Servlets
Prof. Akhil M. Jaiswal
AWT Example
AWT Hierarchy
Component
• A component is the fundamental Graphical User
Interface object in Java that we see on the display in
a Java application is a Component.
• This includes things like buttons, checkboxes,
scrollbars, lists, menus, and text fields.
• A Component usually must be placed in a Container.
• It contains variables that represent the location,
shape, general appearance, and status of the object
as well as methods for basic painting and event
handling.
• Component is a base class from which all of Java’s
GUI components are derived.
Prof. Akhil M. Jaiswal
AWT Components
Radio Button
Scroll Bars
Choice
Prof. Akhil M. Jaiswal
Container
• The Container class is an extended type of
Component that maintains a list of child components
and helps to group them.
• The Container is a component in AWT that can
contain another components like buttons, textfields,
labels etc. The classes that extends Container class are
known as container such as Frame, Dialog and Panel.
• The Container causes its children to be displayed and
arranges them on the screen according to a particular
layout.
• As Container is also a Component, it can be placed
alongside other Component objects in other
Containers in a hierarchical fashion.
Prof. Akhil M. Jaiswal
Container Example
Window
The Window class creates a top-level window. A
top-level window is not contained within any
other object; it sits directly on the desktop.
The window is the container that have no
borders and menu bars. You must use frame,
dialog or another window for creating a
window.
Frame
The Frame is the top-level container that contain title
bar and can have menu bars & can have other
components like button, textfield etc.
Frame encapsulates what is commonly thought of as a
“window.” It is a subclass of Window and has a title
bar, menu bar, borders, and resizing corners.
Panel
The Panel is the container that doesn't contain
title bar and menu bars. It can have other
components like button, textfield etc.
The Panel class is a concrete subclass of
Container & provides space in which any other
component can be placed, including other
panels.
Dialog
• A Dialog is a top-level small window with a
title and a border that is typically used to take
some form of input from the user.
• The size of the dialog includes any area
designated for the border.
• Dialog extends Window class & Unlike Frame,
it doesn't have maximize and minimize
buttons.
Dialog Ex:
Prof. Akhil M. Jaiswal
Software Required:-
Notepad
Jdk 1.7 / 1.8 – www.oracle.com
Command prompt
setBounds(int xaxis, int yaxis, int width, int sets the position/bounds of the AWT components
height)
public void setSize(int width, int height) sets the size (width and height) of the component.
public void setLayout(LayoutManager m) defines the layout manager for the component.
1. AWT Components:-
AWT stands for Abstract Window Toolkit.
It is a platform dependent (API) Application Programming Interface for creating Graphical
User Interface (GUI) for java programs.
The AWT contains Classes & Methods for creation of window-based, Graphical User Interface.
AWT contains Components & Container useful for GUI creation.
AWT is quite large, heavy-weighted and sophisticated windowing user-interface widget toolkit
AWT components are platform-dependent.
AWT classes & methods are contained in the java.awt package
The AWT supports the following types of controls & are subclasses of Component :-
1. Labels
2. Push buttons
3. Check boxes
4. Choice lists
Prof. Akhil Jaiswal
5. Lists
6. Scroll bars
7. Text editing
Prof. Akhil Jaiswal
1. Labels
The easiest control to use is a label. A label is an object of type Label, and it contains a
string, which it displays. Labels are passive controls that do not support any interaction with
the user. Label defines the following constructors:
/*
<applet code="LabelDemo" width=300 height=200>
</applet>
*/ //Applet code
//Program to create a frame “My Frame” & add a Button “click here” into it.- FirstFrame2.java
import java.awt.*;
class FirstFrame2 extends Frame
{
Prof. Akhil Jaiswal
public static void main(String args[])
{
Frame f=new Frame("My Frame");
10
00
30
3
0 80
import java.awt.*;
class Form1 extends Frame // extends Frame Class
{
Form1( ) // constructor
{
Label l1=new Label("Enter UserName"); // Label l1 object created
l1.setBounds(40,45,100,30); //setting position &
add(l1);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[])
{ Form1 f1=new Form1(); } }
Exercise:-
1) Write a Program to create 3 Labels displaying of your NAME, AGE, CITY.
2) WAP to 3 Text Field just one below other.
3) WAP to Create Two Buttons labelled “Positive”, “Negative”
4) WAP to Create Following form:-
Software Required:-
Notepad
Jdk 1.7 / 1.8 – www.oracle.com
Command prompt
Unit 1 Syllabus:-
CONTENT
Checkbox
Group Checkbox
Scroll Bars
Text Area
Ex:-
Checkbox supports these constructors:
Checkbox( ) //creates empty checkbox
Checkbox(String str) //creates checkbox with str as label
Checkbox(String str, boolean on) //creates checkbox with str as label and value as true/false
1. import java.awt.*;
2. public class CheckboxExample1
3. {
4. CheckboxExample1()
5. {
6. Frame f= new Frame("First Checkbox"); //create frame object with label
7. Checkbox checkbox1 = new Checkbox("Java",true); //create CB object with label
8. checkbox1.setBounds(100,100, 50,50); /
9. Checkbox checkbox2 = new Checkbox("Advance Java", true);
10. checkbox2.setBounds(100,150, 100,50);
11.
12. f.add(checkbox1); //add checkbox
13. f.add(checkbox2);
14.
15. f.setSize(400,400); //set frame size Prof. Akhil M. Jaiswal
16. f.setLayout(null); //set frame layout
17. f.setVisible(true); //set frame visbility
18. }
19. public static void main(String args[])
20. {
21. new CheckboxExample1();
22. } }
1. import java.awt.*;
2. public class CheckboxGroupExample
3. {
4. CheckboxGroupExample()
5. {
6. Frame f= new Frame("CheckboxGroup Example");
7. CheckboxGroup cbg = new CheckboxGroup();
8. Checkbox checkBox1 = new Checkbox("Java", cbg, false);
9. checkBox1.setBounds(100,100, 50,50);
10. Checkbox checkBox2 = new Checkbox("Advance Java", cbg, true);
11. checkBox2.setBounds(100,150, 50,50);
12. f.add(checkBox1);
Prof. Akhil M. Jaiswal
13. f.add(checkBox2);
14. f.setSize(400,400);
15. f.setLayout(null);
16. f.setVisible(true);
17. }
18. public static void main(String args[])
19. {
20. new CheckboxGroupExample();
21. }
22. }
1. import java.awt.*;
2. class ScrollbarExample
Prof. Akhil M. Jaiswal
3. {
4. ScrollbarExample(){
5. Frame f= new Frame("Scrollbar Example");
6. Scrollbar s=new Scrollbar(); //creates a vertical scroll bar
7. s.setBounds(100,100, 50,100);
8. f.add(s);
9. f.setSize(400,400);
10. f.setLayout(null);
11. f.setVisible(true);
12. }
13. public static void main(String args[])
14. { new ScrollbarExample();
15. } }
TextArea( )
TextArea(int numLines, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int sBars)
Here,
numLines specifies the height, in lines, of the text area
numChars specifies its width, in characters.
Initial text can be specified by str.
In the fifth form, you can specify the scroll bars that you want the control to have. sBars
must be one of these values:
SCROLLBARS_BOTH
SCROLLBARS_NONE
SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
{
TextAreaExample(){
Frame f= new Frame("TextAreaExample");
TextArea area=new TextArea("Java is Better than C++");
area.setBounds(10,30, 300,300);
f.add(area);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{ new TextAreaExample();
} }
Prof. Akhil M. Jaiswal 9028637523
Exercise
1. Create 3 checkbox with Positive, Negative & Neutral label
2. Create a 3 checkbox group with Positive, Negative &
Neutral
3. Create 2 vertical & two horizontal scrollbars
4. Create a TextArea that contains details about your name,
age, city with both scroll bars.
Unit 1 Syllabus:-
CONTENT
1.4- Use of Layout managers
Layout Managers;-
FlowLayout
BorderLayout
1.
FlowLayout - The FlowLayout is the default layout. It layouts the
components in a directional flow.
2.
BorderLayout
The borderlayout arranges the components to fit in the five regions: east, west,
north, south and center.
3.
GridLayout
The GridLayout manages the components in form of a rectangular grid.
4.
CardLayout
The CardLayout object treats each component in the container as a card. Only
one card is visible at a time.
FlowLayout
Prof. Akhil M. Jaiswal 9028637523
FlowLayout is the default layout manager.
FlowLayout implements a simple layout style, which is similar to
how words flow in a text editor (Left to Right, Top to Bottom).
The direction of the layout is governed by the container’s component
orientation property, which, by default, is left to right, top to bottom.
Therefore, by default, components are laid out line-by-line beginning
at the upper-left corner.
In all cases, when a line is filled, layout advances to the next line.
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
FlowLayout.LEADING
FlowLayout.TRAILING
Homework:-
Program to Demonstrate FlowLayout( ) by adding 10 food Items
import java.awt.*;
class BorderDemo extends Frame
{
BorderDemo()
{
setSize(300,300);
setLayout(new BorderLayout()); //arranges components as per defined
setVisible(true);
add(new Button("TOP- North."),BorderLayout.NORTH); //create & add button
add(new Button("Footer Bottom- South"),BorderLayout.SOUTH);
add(new Button("Right- EAST "), BorderLayout.EAST);
add(new Button("Left- WEST "), BorderLayout.WEST);
add(new Button("Center"), BorderLayout.CENTER);
}
public static void main(String args[])
{
BorderDemo b1=new BorderDemo();
}
}
Homework:-
Program to Demonstrate BorberLayout( ) by adding a button in top
Label in left, text field in center, checkbox in right, & Scroll bar in
at bottom
GridBagLayout
GridBagLayout, components are also arranged in rectangular grid but can have different
sizes and can occupy multiple rows or columns. A good way to do this is to use a grid bag
layout, which is specified by the GridBagLayout class. The components may not be of same
size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells.The key to
the grid bag is that each component can be a different size, and each row in the grid can have
a different number of columns. This is why the layout is called a grid bag.
The first form creates a default card layout. The second form allows you
to specify the horizontal and vertical space left between components in
horz and vert, respectively.
WAP using AWT to create a menubar in a frame where menubar contains menu items
such as File,Edit,View and the sub menu under File menu should contain New and
Open
Dialog Boxes:-
Dialog Box control represents a top-level window with a title and a border used to
take some form of input from the user.
Dialog boxes don’t have menu bars, but in other respects, they function like frame
windows.
Dialog boxes may be modal or modeless.
When a modal dialog box is active, all input is directed to it until it is closed. This
means that you cannot access other parts of your program until you have closed the
dialog box.
When a modeless dialog box is active, input focus can be directed to another window
in your program. Thus, other parts of your program remain active and accessible.
Dialog boxes are of type Dialog. Two commonly used constructors are shown here:
Here, parentWindow is the owner of the dialog box. If mode is true, the dialog box is modal.
Otherwise, it is modeless. The title of the dialog box can be passed in title. Generally, you
will subclass Dialog, adding the functionality required by your application.
import java.awt.*;
public class DialogExample extends Frame
{
DialogExample()
{
Dialog d = new Dialog(this,"Dialog Example", true); //this refer to current Frame
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogExample();
}
}
Output:-
Java provides a built-in dialog box that lets the user specify a file. To create a file dialog box,
instantiate an object of type FileDialog. This causes a file dialog box to be displayed.
Usually, this is the standard file dialog box provided by the operating system.
FileDialog(Frame parent)
FileDialog(Frame parent, String boxName)
FileDialog(Frame parent, String boxName, int how)
Here, parent is the owner of the dialog box. The boxName parameter specifies the name
displayed in the box’s title bar. If boxName is omitted, the title of the dialog box is empty. If
how is FileDialog.LOAD, then the box is selecting a file for reading. If how is
FileDialog.SAVE, the box is selecting a file for writing. If how is omitted, the box is
selecting a file for reading by default.
FileDialog provides methods that allow you to determine the name of the file and its path as
selected by the user.
import java.awt.*;
f.setVisible(true);
f.setSize(300,300);
}
public static void main(String args[])
{
new FileDialogExample();
}
}
Output:-
Practice Programs:- WAP using AWT to create:-