Professional Documents
Culture Documents
Web Technologies Unit 2 Part 3
Web Technologies Unit 2 Part 3
OuterClassReference.new MemberInnerClassConstructor();
The java compiler creates two class files in the case of the inner
class.
The class file name of the inner class is "Outer$Inner". If you want
to instantiate the inner class, you must have to create the instance
of the outer class.
In such a case, an instance of inner class is created inside the
instance of the outer class.
Java Inner Class
Normal : Anonymous inner class
class a{ class a{
public void show() public void show() {
{ System.out.println("In Class A"); System.out.println("In Class A");
}} }}
class b extends a{ public class anoneg{
public void show() public static void main(String a[]) {
{ System.out.println("In Class B"); a obj= new a(){
}} public void show()
public class anoneg{ { System.out.println("In Class
public static void main(String a[]) { B"); } }; obj.show();
a obj= new b(); }}
obj.show();
}}
Java Inner Class
Normal : Anonymous inner class
2. The components of Java AWT are heavy weighted. The components of Java Swing are light weighted.
3. Java AWT has comparatively less functionality as Java Swing has more functionality as compared to AWT.
compared to Swing.
4. The execution time of AWT is more than Swing. The execution time of Swing is less than AWT.
5. The components of Java AWT are platform dependent. The components of Java Swing are platform independent.
7. AWT provides comparatively less powerful Swing provides more powerful components.
components.
Java Swing
• Java Foundation Classes (JFC) are a set of GUI
components which simplify the development of
desktop applications.
b.setBounds(130,100,100, 40);//x axis, y axis, width, height public static void main(String[] args) {
f.add(b);//adding button in JFrame new sample();
f.setSize(400,500);//400 width and 500 height }
}
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}
Java Swing Commonly used controls while designing GUI using SWING.
JlabelA JLabel object is a component for placing text in a container.
JbuttonThis class creates a labeled button.
import javax.swing.*; JColorChooserA JColorChooser provides a pane of controls designed to allow
public class sample extends JFrame{//inheriting JFrame a user to manipulate and select a color.
JCheck BoxA JCheckBox is a graphical component that can be in either an on
JFrame f; (true) or off (false) state.
sample(){ JRadioButtonThe JRadioButton class is a graphical component that can be in
either an on (true) or off (false) state. in a group.
JButton b=new JButton("click");//create button JlistA JList component presents the user with a scrolling list of text items.
b.setBounds(130,100,100, 40); JComboBoxA JComboBox component presents the user with a to show up
menu of choices.
add(b);//adding button on frame JTextFieldA JTextField object is a text component that allows for the editing
setSize(400,500); of a single line of text.
JPasswordFieldA JPasswordField object is a text component specialized for
setLayout(null); password entry.
setVisible(true); JTextAreaA JTextArea object is a text component that allows editing of a
multiple lines of text.
} ImageIconA ImageIcon control is an implementation of the Icon interface
that paints Icons from Images
public static void main(String[] args) {
JscrollbarA Scrollbar control represents a scroll bar component in order to
new sample(); enable the user to select from range of values.
JFileChooserA JFileChooser control represents a dialog window from which
}} the user can select a file.
JProgressBarAs the task progresses towards completion, the progress bar
displays the task's percentage of completion.
JsliderA JSlider lets the user graphically select a value by sliding a knob
within a bounded interval.
Java Swing
User interface considers the following three main
aspects − Foreground Events − These events require direct interaction of the
user.
• UI Elements − These are the core visual
They are generated as consequences of a person interacting with the
elements the user eventually sees and interacts graphical components in the Graphical User Interface.
with.
• Layouts − They define how UI elements should Background Events − These events require the interaction of the end
be organized on the screen and provide a final user.
look and feel to the GUI (Graphical User Eg., Operating system interrupts, hardware or software failure, timer
Interface). expiration, and operation completion are some examples of
• Behavior − These are the events which occur background events.
when the user interacts with UI elements.
Event Handling is the mechanism that controls the event and decides
Change in the state of an object is known as what should happen if an event occurs.
Event, i.e., event describes the change in the This mechanism has a code which is known as an event handler, that
state of the source. is executed when an event occurs.
Events are generated as a result of user Java uses the Delegation Event Model to handle the events. This
interaction with the graphical user interface model defines the standard mechanism to generate and handle the
components. events.
Eg., clicking on a button, moving the mouse,
entering a character through keyboard, selecting
an item from the list
Java Swing
The Delegation Event Model has the following key participants.
Steps Involved in Event Handling
Step 1 − The user clicks the button and the event is generated.
Source − The source is an object on which the event occurs.
Step 2 − The object of concerned event class is created automatically
• Source is responsible for providing information of the and information about the source and the event get populated within
occurred event to it's handler. Java provide us with classes for
the source object. the same object.
Step 3 − Event object is forwarded to the method of the registered
Listener − event handler.
listener class.
• The listener is responsible for generating a response to an Step 4 − The method is gets executed and returns.
event.
• By implementation, the listener is also an object. The listener
waits till it receives an event. Once the event is received, the
listener processes the event and then returns.
The benefit of this approach is that the user interface logic is
completely separated from the logic that generates the event.
The user interface element is able to delegate the processing of
an event to a separate piece of code.
In this model, the listener needs to be registered with the
source object so that the listener can receive the event
notification.
This is an efficient way of handling the event because the event
notifications are sent only to those listeners who want to
receive them.
Java Swing controlPanel = new JPanel();
import java.awt.*;
controlPanel.setLayout(new FlowLayout());
import java.awt.event.*; mainFrame.add(headerLabel);
import javax.swing.*; mainFrame.add(controlPanel);
public class sample {
private JFrame mainFrame; mainFrame.add(statusLabel);
private JLabel headerLabel; mainFrame.setVisible(true);
private JLabel statusLabel; }
private JPanel controlPanel;
private void showEventDemo(){
public sample(){ headerLabel.setText("Control in action: Button");
prepareGUI(); JButton okButton = new JButton("OK");
}
public static void main(String[] args){ JButton submitButton = new JButton("Submit");
sample swingControlDemo = new sample(); JButton cancelButton = new JButton("Cancel");
swingControlDemo.showEventDemo(); okButton.setActionCommand("OK");
}
private void prepareGUI(){ submitButton.setActionCommand("Submit");
mainFrame = new JFrame("Java SWING Examples"); cancelButton.setActionCommand("Cancel");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
okButton.addActionListener(new ButtonClickListener());
submitButton.addActionListener(new ButtonClickListener());
headerLabel = new JLabel("",JLabel.CENTER ); cancelButton.addActionListener(new ButtonClickListener());
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100); controlPanel.add(okButton);
controlPanel.add(submitButton);
mainFrame.addWindowListener(new WindowAdapter() { controlPanel.add(cancelButton);
public void windowClosing(WindowEvent windowEvent){
System.exit(0); mainFrame.setVisible(true);
} }
});
Java Swing
private class ButtonClickListener implements
ActionListener{
public void actionPerformed(ActionEvent e)
{
String command =
e.getActionCommand();
if( command.equals( "OK" )) {
statusLabel.setText("Ok Button
clicked.");
} else if( command.equals( "Submit" ) ) {
statusLabel.setText("Submit Button
clicked.");
} else {
statusLabel.setText("Cancel Button
clicked.");
}
}
}
}
Java Swing
Displaying graphics in swing:
public abstract void drawString(String str, int x, int y) is used to draw the specified string.
public void drawRect(int x, int y, int width, int height) draws a rectangle with the specified width and height.
public abstract void fillRect(int x, int y, int width, int height)is used to fill rectangle with the default color and
specified width and height.
public abstract void drawOval(int x, int y, int width, int height)is used to draw oval with the specified width
and height.
public abstract void fillOval(int x, int y, int width, int height) is used to fill oval with the default color and
specified width and height.
public abstract void drawLine(int x1, int y1, int x2, int y2)is used to draw line between the points(x1, y1) and
(x2, y2).
public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)is used draw the
specified image.
public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)is used draw a
circular or elliptical arc.
public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)is used to fill a circular
or elliptical arc.
public abstract void setColor(Color c) is used to set the graphics current color to the specified color.
public abstract void setFont(Font font) is used to set the graphics current font to the specified font.
Java Swing
Displaying graphics in swing:
import java.awt.*;
import javax.swing.JFrame;
}
public static void main(String[] args) {
DisplayGraphics m=new DisplayGraphics();
JFrame f=new JFrame();
f.add(m);
f.setSize(400,400);
//f.setLayout(null);
f.setVisible(true);
}
}
Java Swing
Displaying image in swing:
import java.awt.*;
import javax.swing.JFrame;
Toolkit t=Toolkit.getDefaultToolkit();
Image i=t.getImage("E:\\java.jpg");
g.drawImage(i, 0,0,this);
}
public static void main(String[] args) {
Mycanvas m=new Mycanvas();
JFrame f=new JFrame();
f.add(m);
f.setSize(400,400);
f.setVisible(true);
}
}
Java Swing
BorderLayoutThe borderlayout arranges the components to fit in
Layout Manager
the five regions: east, west, north, south, and center.
• The layout manager automatically positions all the CardLayout The CardLayout object treats each component in the
components within the container.
container as a card. Only one card is visible at a time.
• Even if you do not use the layout manager, the FlowLayoutThe FlowLayout is the default layout. It layout the
components are still positioned by the default layout
components in a directional flow.
manager.
GridLayoutThe GridLayout manages the components in the form
• It is possible to lay out the controls by hand, however, of a rectangular grid.
it becomes very difficult because of the following two
reasons. GridBagLayoutThis is the most flexible layout manager class.
The object of GridBagLayout aligns the component vertically,
It is very tedious to handle a large number of
horizontally, or along their baseline without requiring the
controls within the container.
components of the same size.
Usually, the width and height information of a GroupLayoutThe GroupLayout hierarchically groups the
component is not given when we need to arrange them.
components in order to position them in a Container.
• Java provides various layout managers to position the SpringLayoutA SpringLayout positions the children of its
controls.
associated container according to a set of constraints.
• Properties like size, shape, and arrangement varies
from one layout manager to the other.
• When the size of the applet or the application window
changes, the size, shape, and arrangement of the
components also changes in response, i.e. the layout
managers adapt to the dimensions of the appletviewer
or the application window.