AssignmentFile 571 27092023162548
AssignmentFile 571 27092023162548
Container Button
Frame List
Dialog …
Frame
• Frame is a container class that provides a window.
Whenever we are developing any window based
application, first of all we are required to create a
window.
• A window can be created by creating an object of
Frame class as follows :
Syntax: 1. Frame obj_name=new Frame();
2. Frame obj_name=new Frame(“title”);
Methods of Frame Class
1. setTitle() : This method is used to specify the title of a frame.
Syntax : Frame_objname.setTitle(“title”);
2. getTitle() : This method is used to retrieve the title of a frame.
Syntax : String objname=Frame_objname.getTitle();
3. setSize() : This method is used to specify the size(width, height) of a frame.
Syntax : Frame_objname.setSize(width,height);
4. show() : This method is used to show a window.
Syntax : Frame_objname.show();
5. hide() : This method is used to hide a frame.
Syntax : Frame_objname.hide();
6. setVisible() : This method is used to show/hide a frame.
Syntax : Frame_objname.setVisible(true/false);
7. dispose() : This method is used to release all the resources used by this frame , its sub
components and all of its own childen.
Syntax : Frame_objname.dispose();
8. setBackground() : This method is used to specify the bavkground color of a frame.
Syntax:
1. Frame_objname.setBackground(Color.COLORNAME);
2. Frame_objname.setBackground(Color objname);
9. setLocationRelativeTo(Component c) : This method is used to specify the
location of the window relative to the specified component. If the
component is not currently showing, or c is null, the window is centered on
the screen.
Syntax :
Frame_objname. setLocationRelativeTo(Component c) ;
Example1 : Develop a windows application having the title
MyWindow.
Components
• Components are the controls that are placed on a
container to design user interface.
• Java.awt package provides a separate class for every
component by inheriting Component class such as
Label, TextField, Button, List, Checkbox etc.
Label
• This class provides the component label that is used to
display text.
Syntax :
1. Label obj_name=new Label();
2. Label obj_name=new Label(“text”);
Methods of Label Class
1. setText() : This method is used to specify the text of a label.
Syntax : Label_objname.setText(“text”);
2. getText() : This method is used to retrieve the text of a label.
Syntax : String objname=Label_objname.getText();
3. setAlignment() : This method is used to specify the alignment of the text with respect
to label
Syntax : Label_objname.setAlignment(alignment);
4. getAlignment() : This method is used to retrieve the alignment of a label.
Syntax : int varname=Label_objname.getAlignment();
Adding Components to a Container
• Components can be added to a container in following
two ways :
1. According to Layout
2. According to User’s Specifications
1. Adding Components according to Layout
• According to layout, components can be added to a
container in following four ways, because there are four
types of layouts as follows:
A. BorderLayout
B. FlowLayout
C. CardLayout
D. GridLayout
A. Adding Components according to BorderLayout
bjname);
Step 3 : Now, components can be added to a container by
calling add() of container class as follows :
ContainerObjname.add(CombonentName, position);
Example :
f.add(l1,BorderLayout.EAST);
f.add(l2,BorderLayout.North);
B. Adding Components according to FlowLayout
bjname);
Step 3 : Now, components can be added to a container by
calling add() of container class as follows :
ContainerObjname.add(CombonentName);
Example :
f.add(l1);
f.add(l2);
C. Adding Components according to CardLayout
bjname);
Step 3 : Now, components can be added to a container by
calling add() of container class as follows :
ContainerObjname.add(CombonentName);
Example :
f.add(l1);
f.add(l2);
D. Adding Components according to GridLayout
bjname);
Step 3 : Now, components can be added to a container by
calling add() of container class as follows :
ContainerObjname.add(CombonentName);
Example :
f.add(l1);
f.add(l2);
2. Adding Components according to User’s Specification
• Whenever a user clicks on a check box, item event is generated that will be
handled same as previous.
Example 1 : Develop a windows application having following
interface :
Radio Buttons
• They are also known as Option Buttons.
• Radio buttons provide a list of choices & from one
group of radio buttons, a user can select only one
choice at a time.
• Therefore, to work with radio buttons, we are required
to perform following two steps :
Step 1 : Creating a Group : To create a group,
CheckboxGroup class is used as follows :
Syntax: CheckboxGroup obj_name=new CheckboxGroup();
Step 2 : Creating Radio Buttons : After creating a group, radio buttons can be
created by creating(placing) checkboxes in the above created group as follows :
Syntax:
2. Checkbox obj_name=new Checkbox(“text”,GrpName);
3. Checkbox obj_name=new Checkbox(“text”,true/false,GrpName);
Example 1 : Develop a windows application having following
interface :
Scrollbar Class
• This class provides the component scroll
bar(Horizontal/Vertical).
• A scroll bar is used to get numeric data from the user
with in the given specific range.
Syntax:
1. Scrollbar obj_name=new Scrollbar();
2. Scrollbar obj_name=new Scrollbar(orientation);
3. Scrollbar obj_name=new Scrollbar(orientation, currentvalue, thumbsize,
minvalue, maxvalue);
Methods of Scrollbar Class
1. setOrientation() : This method is used to specify the orientation of a scrollbar.
Syntax :
Scrollbar_objname.setOrientation(Scrollbar.HORIZONTAL/Scrollbar.VERTICAL);
2. getOrientation) : This method is used to retrieve the orientation of a scrollbar.
Syntax : int varname=Scrollbar_objname.getOrientation();
3. setMinimum() : This method is used to specify the minimum value of the
scrollbar.
Syntax : Scrollbar_Objname.setMinimum(value);
4. getMinimum() : This method returns the minimum value of the scrollbar.
Syntax : int varname=Scrollbar_Objname.getMinimum();
5. setMaximum() : This method is used to specify the maximum value of the
scrollbar.
Syntax : Scrollbar_Objname.setMaximum(value);
6. getMaximum() : This method returns the maximum value of the scrollbar.
Syntax : int varname=Scrollbar_Objname.getMaximum();
7. setUnitIncrement() : This method is used to specify by what value, the value
of a scrollbar is to be increased/decreased whenever a user clicks on scroll
buttons.
Syntax : Scrollbar_Objname.setUnitIncrement(value);
8. getValue() : This method returns the current value of the scrollbar
Syntax : int Varname=Scrollbar_objname.getValue();
Events of Scrollbar Class
Note : After creating a MenuBar, Menus & MenuItems, the MenuBar is to be set
on a Frame(container) as follows :
Frame_Objname.setMenuBar(MenuBar_Objname);
Example 1 : Design a windows application having following interface :
General Events
1. Key Events
2. Mouse Events
3. Window Events
1. Key Events
• Key events are generated whenever we perform any
operation with the key board such as pressing a key,
releasing a key etc.
• Key Events are used to trap a key.
• Corresponding class to key events is KeyEvent class &
corresponding listener is KeyListener.
Methods of KeyListener
Example 2:
Input => Amit Sharma
Output : A. Sharma
Example 3:
Input => Amit
Output : A.
Example 1 : Design a windows application having following interface :
JDBC
• JDBC stands for Java Database Connectivity which is a
standard Java API for database-independent
connectivity between the Java programming language
and a wide range of databases.
• It is a part of JavaSE (Java Standard Edition).
• JDBC API uses JDBC drivers to connect with the
database.
JDBC Drivers
• JDBC Driver is required to establish connection between application and
database. It also helps to process SQL requests and generating result.
• The following are the different types of driver available in JDBC which are used
by the application based on the scenario and type of application.
• Type-1 Driver or JDBC-ODBC bridge
• Type-2 Driver or Native API Partly Java Driver
• Type-3 Driver or Network Protocol Driver
• Type-4 Driver or Thin Driver
1. JDBC-ODBC bridge driver
• The JDBC-ODBC bridge driver uses ODBC driver to connect to the database.
The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC
function calls. This is now discouraged because of thin driver.
• Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle
recommends that you use JDBC drivers provided by the vendor of your
database instead of the JDBC-ODBC Bridge.
• Advantages:
• easy to use.
• can be easily connected to any database.
• Disadvantages:
• Performance degraded because JDBC method call is converted into the ODBC function
calls.
• The ODBC driver needs to be installed on the client machine.
2. Native-API driver (partially java driver)
• The Native API driver uses the client-side libraries of the database. The driver
converts JDBC method calls into native calls of the database API. It is not
written entirely in java.
• Advantage:
• performance upgraded than JDBC-ODBC bridge driver.
• Disadvantage:
• The Native driver needs to be installed on the each client machine.
• The Vendor client library needs to be installed on client machine.
3. Network Protocol driver (fully java driver)
• The thin driver converts JDBC calls directly into the vendor-specific database
protocol. That is why it is known as thin driver. It is fully written in Java
language.
• Advantage:
• Better performance than all other drivers.
• No software is required at client side or server side.
• Disadvantage:
• Drivers depend on the Database.
Steps to Connect to database in Java