Professional Documents
Culture Documents
Oops Through Java: T. Srinivasulu
Oops Through Java: T. Srinivasulu
TECHNOLOGY
By
T. Srinivasulu
Dept of Information Technology
Aditya College of Engineering & Technology
Surampalem.
Aditya College of Engineering & Technology
Introduction:
• The Java Foundation Classes (JFC) provide two frameworks for building GUI-
based application and interestingly both rely on the same event handling
model:
– Abstract Window Toolkit(AWT)
– Swing
• AWT relies on the underlying operating system on a specific platform to
represent its GUI components ( components in AWT are called Heavyweight),
• Swing implements a new set of lightweight GUI components that are written
in Java and has a pluggable look and feel.
• These lightweight components are not dependent on the underlying window
system.
OOPS THROUGH JAVA T.Srinivasulu 4/9/23
Aditya College of Engineering & Technology
Difference between AWT and Swing :
AWT SWING
AWT components are heavy weight. Swing components are light weight.
AWT components are platform Swing components are platform
dependent so there look and feel independent so there look and feel
changes according to OS. remains constant
AWT provides less components than Swing provides more powerful
swing. components such are tables, scroll panes,
colorchooser, tabbedpane etc.,
AWT does not follow MVC (Model View Swing follows MVC.
Controller) where model represents data,
view represents presentation and
controller acts as an interface between
model and view.
GUI:
RECAP OF
EVENT
HANDING
5th UNIT
Label control:
Label is passive components used to display some text to guide user with showing some information.
This is the simplest component of Java AbstractWindowToolkit.
This component is generally used to show the text or string in your application and label never perform any
type of action.
Syntax for defining the label only and with justification:
Label label_name = new Label ("This is the label text.");
above code simply represents the text for the label.
Label label_name = new Label ("This is the label text. ” , Label.CENTER);
Justification of label can be left, right or centered. Above declaration used the center justification of the label
using the Label.CENTER.
Checkbox control:
Checkboxes are used as on-off or yes-no switches
if you click on an unchecked checkbox, it will get checked and vice versa.
Constructors of Checkbox
– Checkbox()
– Checkbox(String str) // constructor with label of chechbox
– Checkbox(String str, boolean on) // constructor with label and initial state of checkbox
– Checkbox(String str, CheckBoxGroup cbg, boolean on) // creating radio button
Methods of Checkbox
– boolean getState() // returns state of the checkbox true / false
– String getLabel() // returns label of chechbox
– CheckboxGroup getCheckboxGroup() // returns checkbox group obj. of radiobutton
Output:
TextField control:
• The TextField class implements a single-line text-entry area.
• Textfields allow the user to enter strings and to edit the text using the arrow keys, cut and paste keys, and
mouse selections.
• TextField is a subclass of TextComponent.
• TextField defines the following Constructors:
o TextField( ) -used to create default text editor
o TextField(int numChars) -used to create a text filed with some number of characters length.
o TextField(String str) -used to create a text field with string in it.
o TextField(String str, int numChars) -used to create text filed with string in it and also specified number o
characters length.
• Methods:
o String getText( ) -used to get the text from the text field
o void setText(String str) -used to set the text to the text field
o String getSelectedText( ) -used to get the selected text
o void select(int startIndex, int endIndex) -used to select the characters starting from startindex to the
endindex.
o void setEchoChar(char ch) -used to create passwords. Actual characters are not shown.
o char getEchoChar( ) -used to obtain the echo characters.
Output:
Output:
Output:
Frame:
A Frame provides the "main window" for your GUI application. It has a title bar (containing an icon, a
title, the minimize, maximize/restore-down and close buttons), an optional menu bar, and the content
display area.
To create a Frame:
Frame frameName = new Frame();
Constructors of Frame
public Frame() Creates a Frame window with no name.
public Frame(String name) Creates a Frame window with a name.
Methods of Frame
public void add(Component comp) This method adds the component, to the Frame.
public void setLayout(LayoutManager object) This method sets the layout of the components
public void remove(Component comp) This method removes a component
public void setSize(int widthPixel, int heightPixel) This method sets the size of a Frame in terms of pixels.
public void setVisible(boolean status) changes the visibility of the frame
Output:
Dialog:
An AWT Dialog is a "pop-up window" used for interacting with the users. A Dialog has a title-bar (containing an icon, a title and a close button) and a
content display area
An object of the Dialog class cannot exist without an associated object of the Frame class
Unlike Frame, it doesn't have maximize and minimize buttons.
There are two kinds of Dialog windows
Modal Dialog window
When a modal dialog window is active, all the user inputs are directed to it and all the other parts of application are inaccessible until this model
dialog is closed.
Modeless Dialog window
When a modeless dialog window is active, the other parts of application are still accessible as normal and inputs can be directed to them, without
needing to close this modeless dialog window.
Constructors:
1. Dialog(Frame, boolean) - Constructs an initially invisible Dialog.
2. Dialog(Frame parent, String title, boolean modal) - parent - the owner of the dialog, title - the title of the dialog modal - if true, dialog blocks
input to other windows when shown
Methods:
1. getTitle() - Gets the title of the Dialog.
2. isModal() - Returns true if the Dialog is modal.
3. isResizable() - Returns true if the user can resize the frame.
4. paramString() - Returns the parameter String of this Dialog.
5. setResizable(boolean) - Sets the resizable flag.
6. setTitle(String) - Sets the title of the Dialog.
Panel:
Layout Managers:
Each container has a layout manager, which controls the way the components are positioned in the
container.
One of the advantages of using layout managers is that there is no need for absolute coordinates where
each component is to be placed or the dimensions of the component.
Whenever the dimensions of the container change (e.g. user resizes the window), layout manager
recalculates the coordinates of components for them to fit the new container size.
There are four basic layout managers are available in java.awt.package:
1. FlowLayout
2. BorderLayout
3. GridLayout
4. CardLayout
5. GridbagLayout
FlowLayout:
The Flow Layout manager arranges the components left-to-right, top-to-bottom in the order they
were inserted into the container.
Components are added to the container first come first basis.
When the container is not wide enough to display all the components, the remaining components
are placed in the next row, etc.
Each row is centered. And all components are sized to preferred size and maintains a gap of 5
pixels.
This layout is default layout for Panel, Applet.
package examples;
import java.awt.*;
import java.applet.*;
/*
<applet code="FlowLayoutDemo.class" width="300" height="200">
</applet>
*/
public class FlowLayoutDemo extends Applet {
public void init() {
setLayout(new FlowLayout());
CheckboxGroup cp = new CheckboxGroup();
Checkbox cb1 = new Checkbox("Large",cp,true);
Checkbox cb2 = new Checkbox("Medium",cp,false);
Checkbox cb3 = new Checkbox("Small",cp,false);
add(cb1);
add(cb2);
add(cb3);
}
}
Grid Layout:
The Grid Layout manager lays out all the components in a rectangular grid.
All the components have identical sizes, since the manager automatically stretches or compresses
the components as to fill the entire space of the container.
Constructors are:
GridLayout(r, c, hgap, vgap)
r – number of rows in the layout
c – number of columns in the layout
hgap – horizontal gaps between components
vgap – vertical gaps between components
GridLayout(r, c)
r – number of rows in the layout
c – number of columns in the layout
No vertical or horizontal gaps.
BorderLayout:
The Border Layout manager arranges components into five regions: (4 borders + center)
Top border – BorderLayout.NORTH
Left border – BorderLayout.WEST
Right border – BorderLayout.EAST
Bottom border – BorderLayout.SOUTH
This layout is so useful to arrange menubar at top, vertical scrollbar at right border, and horizontal scrollbar at
bottom border.
Components in the North and South are set to their natural heights and horizontally stretched to fill the entire
width of the container.
Components in the East and West are set to their natural widths and stretched vertically to fill the entire width
of the container.
The Center component fills the space left in the center of the container.
This layout is default layout for Frame and dialog.
CardLayout:
In card layout, components are added in layers.(one component is on the top of another
components, like pack of cards)
Using this layout, we can hide components which are necessary to display.
Cardlayout has different methods to switch from one card to another card.
o public void next(Container parent): is used to flip to the next card of the given container.
o public void previous(Container parent): is used to flip to the previous card of the given
container.
o public void first(Container parent): is used to flip to the first card of the given container.
o public void last(Container parent): is used to flip to the last card of the given container.
GridBagLayout:
We can arrange components in horizontal as well in vertical direction or by positioning them within a cell of a grid
Components need not be of same size in a row. Each row can contain dissimilar number of columns.
Customization of a GridBagConstraints object can be done by setting one or more of its instance variables.
gridx & gridy
The initial address of cell of a grid is gridx = 0 and gridy = 0.
gridwidth & gridheight
gridwidth constraint specifies the number of cells in a row and gridheight specifies number of columns in
display area of the components. The default value is 1.
fill
GridBagConstraints.NONE (default value–does not grow when the window is resized)
GridBagConstraints.HORIZONTAL (this value fills all the horizontal display area of a component, but it
does not change height).
GridBagConstraints.VERTICAL (it changes the height of a component, but does not change its width)
GridBagConstraints.BOTH (makes the component fill its display area horizontally and vertically, both).
ScrollBar :
Scrollbars are used to select continuous values through a range of integer values (the range set between maximum
and minimum).
These scrollbars can either be set horizontally or vertically.
The scrollbar’s maximum and minimum values can be set along with line increments and page increments.
– Scrollbar()
– Scrollbar(int direction)
PYQs:
1. What are the different types of controls available in AWT?
2. What is the role of layout manager in AWT or Swing?
3. List out the differences between AWT and Swings.
4. Differentiate between swing components and AWT components.
5. Differentiate between grid layout and gridbag layout managers.
6. Differentiate between Text field and Text area.
7. Why layouts are needed?
8. Discuss in detail Menu bars and menus in Java with examples.
9. Explain about any two Layout Managers with example programs.
10. Explain any two AWT controls in java with suitable examples.
11. Discuss about different layouts in AWT?
12. What is the significance of Layout managers? Discuss briefly various layout managers.
13. Write a note on dialog box usage in user interfaces.
14. Explain about the components and containers of AWT.