[go: up one dir, main page]

0% found this document useful (0 votes)
3 views8 pages

java_doc

Java AWT (Abstract Window Toolkit) is an API for developing GUI applications in Java, featuring a hierarchy of components such as Container, Window, Panel, and Frame. It includes various UI elements like buttons, labels, checkboxes, and text fields, as well as event handling mechanisms through the Delegation Event Model. Additionally, Java Swing provides lightweight components for improved performance, and the document covers byte streams and object serialization for input and output operations.

Uploaded by

vsanji6123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

java_doc

Java AWT (Abstract Window Toolkit) is an API for developing GUI applications in Java, featuring a hierarchy of components such as Container, Window, Panel, and Frame. It includes various UI elements like buttons, labels, checkboxes, and text fields, as well as event handling mechanisms through the Delegation Event Model. Additionally, Java Swing provides lightweight components for improved performance, and the document covers byte streams and object serialization for input and output operations.

Uploaded by

vsanji6123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Java AWT

Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in
java.
Java AWT Hierarchy:

Container
The Container is a component in AWT that can contain another components like buttons, textfields,
labels etc
Window
The window is the container that have no borders and menu bars.
Panel
The Panel is the container that doesn't contain title bar and menu bars.
Frame
The Frame is the container that contain title bar and can have menu bars.
Java AWT Example:
1. import java.awt.*;
2. class First extends Frame{
3. First(){
4. Button b=new Button("click me");
5. b.setBounds(30,100,80,30);// setting button position
6. add(b);//adding button into frame
7. setSize(300,300);//frame size 300 width and 300 height
8. setLayout(null);//no layout manager
9. setVisible(true);//now frame will be visible, by default not visible
10. }
11. public static void main(String args[]){
12. First f=new First();
13. }}
AWT UI Elements:
1.Label
A Label object is a component for placing text in a container.
2.Button
This class creates a labeled button.
3.Check Box
A check box is a graphical component that can be in either an on (true) or off (false) state.
4.Check Box Group
The CheckboxGroup class is used to group the set of checkbox.
5.List
The List component presents the user with a scrolling list of text items.
6.Text Field
A TextField object is a text component that allows for the editing of a single line of text.
7.Text Area
A TextArea object is a text component that allows for the editing of a multiple lines of text.
8.Choice
A Choice control is used to show pop up menu of choices. Selected choice is shown on the top
of the menu.
9.Canvas
A Canvas control represents a rectangular area where application can draw something or can
receive inputs created by user.
10.Image
An Image control is superclass for all image classes representing graphical images.
11.Scroll Bar
A Scrollbar control represents a scroll bar component in order to enable user to select from range
of values.
12.Dialog
A Dialog control represents a top-level window with a title and a border used to take some form
of input from the user.
What is an Event?
Change in the state of an object is known as event i.e. event describes the change in state of
source.For example, clicking on a button, moving the mouse.
Types of Event:
Foreground Events - Those events which require the direct interaction of user.
Background Events - Those events that require the interaction of end user are known as
background
events.
*Button: This component can be use to perform some operation when the user clicks on a
button.
Creation of button: Button b = new Button(String label);
*Label: This component is used to display a message on the frame. This component will be
generally used along with other components.
Creation of Label: Label l = new Label(String);

Swing: Swing is use to develop a better efficient GUI.


The swing components are called as light weight components which will improve the
performance of the application because the amount of resources required is very minimum.

Java JButton
The JButton class is used to create a labeled button that has platform independent
implementation. The application result in some action when the button is pushed. It inherits
AbstractButton class.
JButton class declaration
Declaration :
1. public class JButton extends AbstractButton implements Accessible
Java JLabel
The object of JLabel class is a component for placing text in a container. It is used to display a
single line of read only text.
JLabel class declaration
1. public class JLabel extends JComponent implements SwingConstants, Accessible
CheckBox: This component allows the user to select multiple item from a group of items.
Creation of JCheckBox:
JCbeckBox jcb = new JCheckBox();
JCbeckBox jcb = new JCheckBox(Label);
JCbeckBox jcb = new JCheckBox(Label,boolean);
JTextField: This component allows the user to type some text in a single line.
Creation of JTextField:
JTextField jtf = new JtextField(size);
JTextArea: This component allows the user to type the text in multiple lines.
Creation of JTextArea:
JTextArea jta = new JTextArea(rows,cols);

JTable: This component is used to display the data in the form of rows and colums.
*Creation of JTable: JTable jt = new JTable(rowData,ColumnNames);
The row data represents a two dimensional array and the columnNames represents a single
dimensional array.
*Methods of JTable:
1. getRowCount(): This method returns the count of the number of rows available in the table.
2. getCoulmnCount(): This method returns the count of the number of columns available in
the table.
3. getSelectedRow: This method returns the index of the row that is selected. It returns -1 when
number row is selected.
4. getSelectedRows: This method returns the indexes of the rows that are selected.
5. getSelectedRowCount(): This method returns the count number of rows that are selected.
6.getJTableHeader(): This method returns the heading of the table.

Java JScrollBar
The object of JScrollbar class is used to add horizontal and vertical scrollbar. It is an
implementation of a scrollbar. It inherits JComponent class.

JScrollBar class declaration

public class JScrollBar extends JComponent implements Adjustable, Accessible


Layouts: Layout will specify the format or the order which the components has to be placed
on the container.
Layout manager is a class /component that it responsible for arrange the components on the
container according to the specified layout.
Types of Layout:
1. FlowLayout
2. BorderLayout
3. CardLayout
4. GridLayout
5. GridBagLayout
*FlowLayout: This Layout will display the components in sequence from left to right, from
top to bottom.
*BorderLayout: This Layout will display the components along the border of the container.
This Layout contains five locations where the component can be displayed.
*CardLayout: A cardLayout represent a stack of cards displayed on a container. At time only
one card can be visible and each can contain only one component.
*GridLayout: Layout will display the components in the format of rows and columns. The
container will be divided into table of rows and columns.
*GridBagLayout: This layout is the most efficient layout that can be used for displaying
components.
What is Event Handling?
Event Handling is the mechanism that controls the event and decides what should happen if an
event occurs.Java Uses the Delegation Event Model to handle the events. This model
defines the standard mechanism to generate and handle the events.
The Delegation Event Model has the following key participants namely:
Source - The source is an object on which event occurs. Source is responsible for providing
information
of the occurred event to it's handler. Java provide as with classes for source object.
Listener - It is also known as event handler.Listener is responsible for generating response to an
event.
From java implementation point of view the listener is also an object. Listener waits until it receives
anevent. Once the event is received , the listener process the event an then returns.The benefit of
this approach is that the user interface logic is completely separated from the logic that generates
the event. This is an efficient way of handling the event
because the event notifications are sent only to those listener that want to receive them
AWT Event Classes:
1.AWTEvent
It is the root event class for all AWT events. This class and its subclasses supercede the original
java.awt.Event class.
2.ActionEvent
The ActionEvent is generated when button is clicked or the item of a list is double clicked.
3.InputEvent
The InputEvent class is root event class for all component-level input events.
4.KeyEvent
On entering the character the Key event is generated.
5.MouseEvent
This event indicates a mouse action occurred in a component.
6.TextEvent
The object of this class represents the text events.
7.WindowEvent
The object of this class represents the change in state of a window.
8.AdjustmentEvent
The object of this class represents the adjustment event emitted by Adjustable objects.
9.ComponentEvent
The object of this class represents the change in state of a window.
10.ContainerEvent
The object of this class represents the change in state of a window.
11.MouseMotionEvent
The object of this class represents the change in state of a window.
12.PaintEvent
The object of this class represents the change in state of a window.

*WindowAdapter: This calss is an implementation class of WindowListener it contains All


the 7 mehods of WindowListener but available as null implementations.
WindowAdaper can we use to over ride the methods that we require and the remaining methods
need not the implemented.
Adapter classes:
WindowAdapter
KeyAdapter
MouseAdapter
MouseMotion
FocusAdapter
ComponentAdapter
ContainerAdapter
Inner class: If a class declared inside another class then it is called Inner class.
*Anonymous inner class:
An inner class which does not contains any name is called as
Anonymous inner class.
UNIT-3
ByteStream:
ByteStream classes are used to read bytes from the input stream and write bytes to
the output stream.

SN Class Description
1 BufferedInputStream This class provides methods to read bytes from the buffer.

ByteArrayInputStrea
2 This class provides methods to read bytes from the byte array.
m

3 DataInputStream This class provides methods to read Java primitive data types.

4 FileInputStream This class provides methods to read bytes from a file.

This class contains methods to read bytes from the other input
5 FilterInputStream
streams, which are used as the primary source of data.

6 ObjectInputStream This class provides methods to read objects.

OutputStream Class:

Description
Class
1 BufferedOutputStream This class provides methods to write the bytes to the buffer.

ByteArrayOutputStrea
2 This class provides methods to write bytes to the byte array.
m

This class provides methods to write the java primitive data


3 DataOutputStream
types.

4 FileOutputStream This class provides methods to write bytes to a file.

5 FilterOutputStream This class provides methods to write to other output streams.

6 ObjectOutputStream This class provides methods to write objects.


Object Serialization:
Serialization in Java is a mechanism of writing the state of an object into a byte-
stream.
The reverse operation of serialization is called deserialization where byte-stream is
converted into an object.

For serializing the object, we call the writeObject() method


of ObjectOutputStream class, and for deserialization we call
the readObject() method of ObjectInputStream class.

import java.io.*;
1.class Persist{
2. public static void main(String args[]){
3. try{
4. //Creating the object
5. Student s1 =new Student(211,"ravi");
6. //Creating stream and writing the object
7. FileOutputStream fout=new FileOutputStream("f.txt");
8. ObjectOutputStream out=new ObjectOutputStream(fout);
9. out.writeObject(s1);
10. out.flush();
11. //closing the stream
12. out.close();
13. System.out.println("success");
14. }catch(Exception e){System.out.println(e);}
15. }
16.}

You might also like