ADVANCE JAVA TYBSC(CS) SEM 5 COMPILED BY : ST 302 PARANJPE UDYOG BHAVAN, NEAR KHANDELWAL SWEETS, NEAR THANE STATION , THANE (WEST) PHONE NO: 8097071144 / 8097071155
1. ADVANCE JAVA TYBSC(CS) SEM 5 COMPILED BY : ST 302 PARANJPE
UDYOG BHAVAN, NEAR KHANDELWAL SWEETS, NEAR THANE STATION , THANE
(WEST) PHONE NO: 8097071144 / 8097071155
2. INDEX Course: USCS502 TOPICS Advanced Java Programming I
PGNO Unit I Swing Components I: Introduction to JFC and Swing,
Features of the Java Foundation Classes, Swing API Components,
JComponent Class, Windows, Dialog Boxes, and Panels, Labels,
Buttons, Check Boxes, Menus, Pane, JScrollPane, Desktop pane,
Scrollbars, Lists and Combo Boxes, Text-Entry Components. 1 Unit II
Swing Components II: Toolbars, Implementing Action interface,
Colors and File Choosers, Tables and Trees, Printing with 2D API
and Java Print Service API. Schedules Tasks using JVM, Thread-safe
variables, Communication between threads. Event Handling: The
Delegation Event Model, Event classes (ActionEvent, FocusEvent,
InputEvent, ItemEvent, KeyEvent, MouseEvent, MouseWheelEvent,
TextEvent, WindowEvent) and various listener interfaces
(ActionListener, FocusListener, ItemListener, KeyListener,
MouseListener, MouseMotionListener, MouseWheelListener,
TextListener, WindowFocusListener, WindowListener) 28 Unit III
JDBC: JDBC Introduction, JDBC Architecture, Types of JDBC Drivers,
The Connectivity Model, The java.sql package, Navigating the
ResultSet objects contents, Manipulating records of a ResultSet
object through User Interface , The JDBC Exception classes,
Database Connectivity, Data Manipulation (using Prepared
Statements, Joins, Transactions, Stored Procedures), Data
navigation. 62 Unit IV Networking with JAVA: Overview of
Networking, Working with URL, Connecting to a Server, Implementing
Servers, Serving multiple Clients, Sending E-Mail, Socket
Programming, Internet Addresses, URL Connections. Accessing Network
interface parameters, Posting Form Data, Cookies, Overview of
Understanding the Sockets Direct Protocol. Introduction to
distributed object system, Distributed Object Technologies, RMI for
distributed computing, RMI Architecture, RMI Registry Service,
Parameter Passing in Remote Methods, Creating RMI application,
Steps involved in running the RMI application, Using RMI with
Applets. 80
3. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 1 web:www.weit.in UNIT 1
Introduction to JFC JFC is short for Java Foundation Classes, which
encompass a group of features for building graphical user
interfaces (GUIs) and adding rich graphics functionality and
interactivity to Java applications. It is defined as containing the
features shown in the table below. Feature Description Swing GUI
Components Includes everything from buttons to split panes to
tables. Many components are capable of sorting, printing, and drag
and drop, to name a few of the supported features. Pluggable
Look-and- Feel Support The look and feel of Swing applications is
pluggable, allowing a choice of look and feel. For example, the
same program can use either the Java or the Windows look and feel.
Additionally, the Java platform supports the GTK+ look and feel,
which makes hundreds of existing look and feels available to Swing
programs. Many more look-and-feel packages are available from
various sources. Accessibility API Enables assistive technologies,
such as screen readers and Braille displays, to get information
from the user interface. Java 2D API Enables developers to easily
incorporate high-quality 2D graphics, text, and images in
applications and applets. Java 2D includes extensive APIs for
generating and sending high-quality output to printing devices.
Internationalization Allows developers to build applications that
can interact with users worldwide in their own languages and
cultural conventions. With the input method framework developers
can build applications that accept text in languages that use
thousands of different characters, such as Japanese, Chinese, or
Korean. Swing The Swing classes are the next-generation GUI
classes. Swing components are purely written in Java. Lightweight
Pluggable look and feel. Swing is a huge set of components which
includes labels, frames, tables, trees, and styled text documents.
Almost all Swing components are derived from a single parent called
JComponent which extends the AWT Container class. Swing is a layer
on top of AWT rather than a substitution for it. The Java
Foundation Classes consist of five major parts: AWT, Swing, and
Accessibility, Java 2D, and Drag and Drop.
4. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 2 web:www.weit.in Features of
swing : 1. The Swing Components which includes everything from
buttons to split panes to tables. 2. Swing components provide
flexibility for nesting components. We can have a graphic in a
list, combo box in toolbox, panels in list box etc. 3. Swing
components offer good look and feel regardless of OS. They look
different than OS components. But using the plugins support the
same program can have either the Java look and feel or the Windows
look and feel. 4. Swing enables supportive technologies such as
screen readers and Braille displays to get information from the
user interface. 5. Swing components follow Model-View-Controller
Architecture/Design Pattern. Model stores the contents and allows
them to modify. View displays the contents in different forms.
Controller handles user input and interacts with view or model. 6.
Java 2 Platform provides the ability to drag and drop between a
Java application and a native application. 7. The Java 2 API
enables developers to easily incorporate high-quality 2D graphics,
text, and images in applications and in applets. Disadvantages of
Swing 1. Swing components are slower than AWT. 2. Swing components
are not thread-safe. We may not get correct output if we modify the
model concurrently with the screen updater thread. AWT VS SWING
COMPONENTS OR HEAVYWEIGHT VS LIGHTWEIGHT COMPONENTS A heavyweight
component is one that is associated with its OS's own native screen
resource. A lightweight component has no native resource of its own
that's why it is lighter. Lightweight component is painted onto a
heavyweight component. All AWT components are heavyweight and all
Swing components are lightweight (except for the top-level
components such as JWindow, JFrame, JDialog, and JApplet). A
lightweight component can have transparent pixels; a heavyweight is
always blurred. A lightweight component can appear to be
non-rectangular because of its ability to set transparent areas; a
heavyweight can only be rectangular. Mouse events on a lightweight
component fall through to its parent; mouse events on a heavyweight
component do not fall through to its parent. When a lightweight
component overlaps a heavyweight component, the heavyweight
component is always on top, regardless of the relative order of the
two components. "It is advisable that do not mix the heavyweight
and lightweight components in a program."
5. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 3 web:www.weit.in SWING
CONATAINER PANES Swing offers some top-level containers such as -
JApplet, JDialog, and JFrame. There are some problems for mixing
lightweight and heavyweight components together in Swing, we can't
just add anything but first, we must get something called a
"content pane," and then we can add Swing components to that. The
different types of panes that are part of container are as follows
- The Root Pane We don't directly create a JRootPane object. As an
alternative, we get a JRootPane when we instantiate JInternalFrame
or one of the top-level Swing containers, such as JApplet, JDialog,
and JFrame. It's a lightweight container used behind the scenes by
these top-level containers. As the preceding figure shows, a root
pane has four parts : 1. The layered pane : It Serves to position
its contents, which consist of the content pane and the optional
menu bar. It can also hold other components in a specified order.
JLayeredPane adds depth to a JFC/Swing container, allowing
components to overlap each other when needed.It allows for the
definition of a several layers within itself for the child
components. JLayeredPane manages its list of children like
Container, but allows for the definition of a several layers within
itself. 2. The content pane : The container of the root pane's
visible components, excluding the menu bar. 3. The optional menu
bar : It is the home for the root pane's container's menus. If the
container has a menu bar, we generally use the container's
setJMenuBar method to put the menu bar in the appropriate
place.
6. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 4 web:www.weit.in 4. The
glass pane : It is hidden, by default. If we make the glass pane
visible, then it's like a sheet of glass over all the other parts
of the root pane. It's completely transparent.The glass pane is
useful when we want to be able to catch events or paint over an
area that already contains one or more components. We can display
an image over multiple components using the glass pane. SWING
COMPONENTS AND THE CONTAINMENT HIERARCHY Swing application creates
four commonly used Swing components : 1. A frame, or main window
(JFrame). 2. An applet, called JApplet. 3. A panel, sometimes
called a pane (JPanel). 4. Several other types of containers such
as JScrollPane, JTabbedPane, JInternalFrame etc. Every top-level
container indirectly contains an intermediate container known as a
content pane. The content pane contains, directly or indirectly,
all of the visible components in the window's GUI. 1. Component : A
component is an object having a graphical representation that can
be displayed on the screen and that can interact with the user.
Examples of components are the buttons, checkboxes, and scrollbars
of a typical graphical user interface. It contains basic methods
such as setting the size, background/foreground colors that are
associated with every GUI element. 2. Container : A container is a
component that has the ability to hold other components. It also
takes of alignment and sizing of components that are present in it.
This is done using Layout manager classes. 3. Window : A Window
object is a top-level window with no borders and no menubar. The
default layout for a window is BorderLayout. 4. Frame : A Frame is
a top-level window with a title and a border. 5. Panel :
7. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 5 web:www.weit.in Panel is
the simplest container class. A panel provides space in which an
application can attach any other component, including other panels.
The default layout manager for a panel is the FlowLayout layout
manager. 6. Applet : An applet is a small program that is intended
not to be run on its own, instead to be embedded inside another
application. The Applet class must be the superclass of any applet
that is to be embedded in a Web page or viewed by the Java Applet
Viewer. 7. JFrame : JFrame is an extended version of java.awt.Frame
that adds support for the JFC/Swing component architecture. Like
all other JFC/Swing top-level containers, a JFrame contains a
JRootPane as its only child. The content pane provided by the root
pane should, as a rule, contain all the non-menu components
displayed by the JFrame. 8. JApplet : JApplet is an extended
version of java.applet.Applet that adds support for the JFC/Swing
component architecture. 9. Canvas : A Canvas component represents a
blank rectangular area of the screen onto which the application can
draw or from which the application can trap input events from the
user. It cannot be used to add/remove components. The different
types of swing components such as - JList, JButton etc. are derived
from JComponent. JButton Class Simple uses of JButton are very
similar to Button. You create a JButton with a String as a label,
and then drop it in a window. Events are normally handled just as
with a Button: you attach an ActionListener via the
addActionListener method. JButton Constructor JButton() Creates a
button with no set text or icon. JButton(Action a) Creates a button
where properties are taken from the Action supplied. JButton(Icon
icon) Creates a button with an icon. JButton(String text) Creates a
button with text. JButton(String text, Icon icon) Creates a button
with initial text and an icon.
8. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 6 web:www.weit.in Container
class Each GUI component can be contained only once. If a component
is already in a container and you try to add it to another
container, the component will be removed from the first container
and then added to the second. Each top-level container has a
content pane that, generally speaking, contains (directly or
indirectly) the visible components in that top-level container's
GUI. Container class methods add(Component) Adds the specified
component to this container. add(String, Component) Adds the
specified component to this container. countComponents() Returns
the number of components in this panel. deliverEvent(Event)
Delivers an event. getComponent(int) Gets the nth component in this
container. getComponents() Gets all the components in this
container. getLayout() Gets the layout manager for this container.
layout() Does a layout on this Container. locate(int, int) Locates
the component that contains the x,y position. minimumSize() Returns
the minimum size of this container. preferredSize() Returns the
preferred size of this container. remove(Component) Removes the
specified component from this container. removeAll() Removes all
the components from this container. setLayout(LayoutManager) Sets
the layout manager for this container. Write a program to display a
button on the screen and on the click of the button change the
background colour of applet to red using Swing. import
javax.swing.*; import java.awt.*; import java.awt.event.*; public
class firstswing extends JApplet implements ActionListener {
Container cp; public void init() { cp=getContentPane();
cp.setLayout(new FlowLayout()); JButton b1=new JButton("Click Me");
cp.add(b1);
9. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 7 web:www.weit.in
b1.addActionListener(this); } public void
actionPerformed(ActionEvent ae) { cp.setBackground(Color.red); } }
/**/ Execution 1. javac firstswing.java 2. appletviewer
firstswing.java NOTE To obtain a container we use getContentPane()
method. By default the Layout followed by swing is BorderLayout, we
can set the layout to FlowLayout.
10. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 8 web:www.weit.in Write a
program to display the following code: import javax.swing.*; import
java.awt.*; import java.awt.event.*; public class second extends
JApplet implements ActionListener { Container cp; JLabel l1,l2;
JTextField t1,t2; JButton b1; @Override public void init() {
cp=getContentPane(); cp.setLayout(new FlowLayout()); l1=new
JLabel("Enter Pet-Name"); l2=new JLabel("Display"); t1=new
JTextField(20); t2=new JTextField(20); b1=new JButton("Click");
cp.add(l1); cp.add(t1); cp.add(l2); cp.add(t2); cp.add(b1);
b1.addActionListener(this); } public void
actionPerformed(ActionEvent ae) { String str=t1.getText();
t2.setText(str); }
11. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 9 web:www.weit.in } /**/ NOTE
By default every control stores a value in string format. When the
value is retrieve it is bydefault in string, and when you want to
set a value, it should be converted into string Write a program to
display the following. code: import javax.swing.*; import
java.awt.*; import java.awt.event.*; public class thirdCube extends
JApplet implements ActionListener { Container cp; JLabel l1,l2;
JTextField t1,t2; JButton b1,b2; @Override public void init() {
cp=getContentPane(); cp.setLayout(new FlowLayout()); l1=new
JLabel("Enter Number"); l2=new JLabel("Result"); t1=new
JTextField(10); t2=new JTextField(10); b1=new JButton("SQUARE");
b2=new JButton("CUBE"); cp.add(l1);
12. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 10 web:www.weit.in
cp.add(t1); cp.add(l2); cp.add(t2); cp.add(b1); cp.add(b2);
b1.addActionListener(this); b2.addActionListener(this); } public
void actionPerformed(ActionEvent ae) { int
x=Integer.parseInt(t1.getText()); if(ae.getSource()==b1) {
t2.setText(x*x+" "); } else { t2.setText(x*x*x+" "); } } } /**/
JCheckBox and JRadioButton Using CheckBox, we can select multiple
options simultaneously, where as RadioButtons allows single
selection from multiple options. A checkbox is GUI field where use
can make multiple choices for input. Each time the user clicks it,
its state swtiches between checked and unchecked. Radio buttons are
similar to checkboxes, but they are usually arranged in groups.
When we click on one radiobutton in the group the others
automatically turn off. Checkboxes and radio buttons are
represented by JCheckBox and JRadio Button class object. Radio
buttons can be chained together using an instance of another class
called ButtonGroup. A JCheckBox sends ItemEvents when it's clicked.
Since a checkbox is a button, it also fires ActionEvents when it
becomes checked. We can change our choices until we submit the
form. Write a program to display the following.
14. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 12 web:www.weit.in public
void itemStateChanged(ItemEvent ie) { String st1= "";
t1.setText(""); if(c1.isSelected()) { st1 = st1 +" "+
(c1.getText()); t1.setText(st1); } if(c2.isSelected()) { st1 = st1
+" "+ (c2.getText()); t1.setText(st1); } } } /**/ NOTE To provide
single selection within multiple options, we have to add those
appropriate control to a ButtonGroup. WAP to display the following.
code:
16. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 14 web:www.weit.in
t2.setText(x*x+""); }
if(c1.getSelectedItem().toString().equals("CUBE")) {
t2.setText(x*x*x+""); } } if(ae.getSource()==b2) { t1.setText("");
t2.setText(""); } } public void itemStateChanged(ItemEvent ie) {
int x=Integer.parseInt(t1.getText());
if(ie.getItem().equals("CUBE")) { t2.setText(x*x*x+""); }
if(ie.getItem().equals("SQUARE")) { t2.setText(x*x+""); } } } /**/
JPanel Like getContentPane, which is the main container, we have
some secondary container, which can hold other containers and
control over itself. JPanel is normally implemented, whenever we
want to use more than one layout on the main container. A single
application can use, more than one panel simultaneously.
17. ADDRESS:302 PARANJPE UDYOG BHAVAN,OPP SHIVSAGAR
RESTAURANT,THANE [W].PH 8097071144/55 15 web:www.weit.in WAP to
display the following (add panel in container) code: import
javax.swing.*; import java.awt.*; public class ninth extends
JApplet { Container cp; public void init() { cp=getContentPane();
cp.setLayout(new FlowLayout()); JPanel jp=new JPanel();
jp.setLayout(new GridLayout(4,4)); for(int i=1;i