java_doc
java_doc
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);
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.
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.
This class contains methods to read bytes from the other input
5 FilterInputStream
streams, which are used as the primary source of data.
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
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.}