Upload
others
View
23
Download
0
Embed Size (px)
Citation preview
“Jobs and bosses will come and go, but your education will always help you to grow.” 1
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
1. What is an applet ?
An applet is a small Internet-based program written in Java, a programming language
for the Web, which can be downloaded by any computer. The applet is also able to run
in HTML. The applet is usually embedded in an HTML page on a Web site and can be
executed from within a browser.
2. What is the purpose of appletviewer ?
AppletViewer is a standalone command-line program from Sun to run Java applets.
Appletviewer is generally used by developers for testing their applets before deploying
them to a website.
3. How does applet differ from stand alone program?
Applets do not use main() method for initiating the execution of the code
.Applets when loaded automatically call certain methods of applets class to start
and execute the apllet code.
Unlike stand alone applications , apllets cannot be run independently .They are
run from inside a webpage using a special feature known as HTML tag.
Applets cannot read from or write to the files in the local computer.
Applets cannot communicate with other servers on the network.
Applets cannot run any program from the local computer.
4. Write a note on applet skeleton?
An Applet skeleton is as follows
Import java awt.*;
Import java.applet.*;
/*
<applet code=”Appleskel” width=300 height=300>
</applet>
/*
Public class Appleskel extends Applet
{
Public void init()
{ //initialization
}
“Jobs and bosses will come and go, but your education will always help you to grow.” 2
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
Public void start()
{ // start or resume execution
}
Public void stop()
{
// suspends execution
}
Public void destroy()
{
//perform shutdown activities
}
Public void paint()
{
// redisplay contents of window
}
}
5. What is an repaint() method in applet ?
Call repaint( ) when you have changed something and want your changes to show up on
the screen
o You do not need to call repaint() when something in Java’s own components
(Buttons, TextFields, etc.)
o You do need to call repaint() after drawing commands (drawRect(...), fillRect(...),
drawString(...), etc.)
repaint( ) is a request--it might not happen
When you call repaint( ), Java schedules a call to update(Graphics g)
6. What is an update method in java applet?
The update() method is defined by the AWT and is called when your applet has requested that a portion of its window be redrawn. The problem is that the default version of update() first fills an applet with the default background colour and then calls paint(). You can override the update() method. The paint() in this case will simply call update(). public void update(Graphic g) { //Redisplay your window here. } public void paint(Graphics g) { update(g); // call to the update()method. }
“Jobs and bosses will come and go, but your education will always help you to grow.” 3
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
7. How do you pass parameters to applets ?
We can get any information from the HTML file as a parameter. For this purpose, Applet
class provides a method named getParameter().
It returns the value of the specified parameter in the form of a String object.
Example:
Example of using parameter in Applet:
import java.applet.Applet;
import java.awt.Graphics;
public class UseParam extends Applet{
public void paint(Graphics g){
String str=getParameter("msg");
g.drawString(str,50, 50);
}
}
myapplet.html
<html>
<body>
<applet code="UseParam.class" width="300" height="300">
<param name="msg" value="Welcome to applet"> // we are passing the value here
</applet>
</body>
</html>
8. Explain all the methods used to run an applet?
Lifecycle of Java Applet
1. Applet is initialized.
2. Applet is started.
“Jobs and bosses will come and go, but your education will always help you to grow.” 4
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
3. Applet is painted.
4. Applet is stopped.
5. Applet is destroyed.
For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet.
1. public void init(): is used to initialized the Applet. It is invoked only once.
2. public void start(): is invoked after the init() method or browser is maximized. It is used
to start the Applet.
3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or
browser is minimized.
4. public void destroy(): is used to destroy the Applet. It is invoked only once.
The Component class provides 1 life cycle method of applet.
1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class
object that can be used for drawing oval, rectangle, arc etc.
9. How to run Applet?
There are two ways to run an applet
1. By html file.
2. By appletViewer tool (for testing purpose).
// 1st way Simple example of Applet by html file:
To execute the applet by html file, create an applet and compile it. After that create an html file and place the applet code in html file. Now click the html file.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
“Jobs and bosses will come and go, but your education will always help you to grow.” 5
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
g.drawString("welcome",150,150); } }
Note: class must be public because its object is created by Java Plugin software that resides
on the browser.
myapplet.html
<html>
<body>
<applet code="First.class" width="300" height="300">
</applet>
</body>
</html>
// 2nd way :Simple example of Applet by appletviewer tool:
To execute the applet by appletviewer tool, create an applet that contains applet tag in comment and compile it. After that run it by: appletviewer First.java. Now Html file is not required but it is for testing purpose only.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g.drawString("welcome to applet",150,150);
}
}
/*
<applet code="First.class" width="300" height="300">
</applet> */
To execute the applet by appletviewer tool, write in command prompt:
c:\>javac First.java c:\>appletviewer First.java
“Jobs and bosses will come and go, but your education will always help you to grow.” 6
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
10. What is an event? Give Example.
Event describes the change of state of any object . Example : Pressing a button, Entering a character in Textbox.
11. What are the components of Event handling ?
Event handling has three main components,
Events : An event is a change of state of an object.
Events Source : Event source is an object that generates an event.
Listeners : A listener is an object that listens to the event. A listener gets notified when
an event occurs.
12. How events are handled in java? Explain few Event handlers classes n listener
interfaces in java ?
Note : for both Event Classes and Listener Interfaces the description are same .
Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events
Events are supported by a number of Java packages, like java.util, java.awt and java.awt.event.
Important Event Classes and Interface
Event Classes Description Listener Interface
ActionEvent generated when button is pressed, menu-item is selected, list-item is double clicked
ActionListener
“Jobs and bosses will come and go, but your education will always help you to grow.” 7
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
MouseEvent generated when mouse is dragged, moved,clicked,pressed or released also when the enters or exit a component
MouseListener
KeyEvent generated when input is received from keyboard KeyListener
ItemEvent generated when check-box or list item is clicked ItemListener
TextEvent generated when value of textarea or textfield is changed
TextListener
MouseWheelEvent generated when mouse wheel is moved MouseWheelListener
WindowEvent generated when window is activated, deactivated, deiconified, iconified, opened or closed
WindowListener
ComponentEvent generated when component is hidden, moved,
resized or set visible
ComponentEventListener
ContainerEvent generated when component is added or removed from container
ContainerListener
AdjustmentEvent generated when scroll bar is manipulated AdjustmentListener
FocusEvent generated when component gains or loses keyboard
focus
FocusListener
13. Explain about Delegation Event Model?
Event Handling is the mechanism that controls the event and decides what should
happen if an event occurs. This mechanism have the code which is known as event
“Jobs and bosses will come and go, but your education will always help you to grow.” 8
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
handler that is executed when 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. Let’s have a brief introduction to this model.
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 its 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 an event. Once the event is received, the listener
process the event a 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 the separate piece of code. In this model, 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 listener that want to receive them.
Steps involved in event handling
The User clicks the button and the event is generated.
Now the object of concerned event class is created automatically and information about
the source and the event get populated with in same object.
Event object is forwarded to the method of registered listener class.
The method is now get executed and returns.
14. Expand AWT ?
AWT stands for Abstract Window ToolKit.
AWT contains large number of classes and methods that allows you to create and
manage windows GUI(Graphical User Interface) application. AWT is the foundation upon
which Swing is made. It is used for GUI programming in Java. But now a days it is merely
used because most GUI java programs are implemented using Swing because of its rich
implementation of GUI controls and light-weighted nature.
15. Explain few AWT UI Elements used?
Labels –
“Jobs and bosses will come and go, but your education will always help you to grow.” 9
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
A label displays a single line of read-only text. However the text can be
changed by the application programmer but cannot be changed by the end
user in any way.
Label()
Constructs an empty label.
Label(String text)
Constructs a new label with the specified string of text, left justified.
Label(String text, int alignment)
Constructs a new label that presents the specified string of text with the
specified alignment.
Buttons –
This class creates a labeled button.
Button() Constructs a button with an empty string for its label.
Button(String text) Constructs a new button with specified label.
Check Box-
A check box is a graphical component that can be in either
an on (true) or off (false) state.
Checkbox() Creates a check box with an empty string for its label.
Checkbox(String label) Creates a check box with the specified label.
Checkbox(String label, boolean state) Creates a check box with the specified label and sets the specified
state
Checkbox Group –
The CheckboxGroup class is used to group the set of checkbox.
“Jobs and bosses will come and go, but your education will always help you to grow.” 10
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
CheckboxGroup() Creates a new instance of CheckboxGroup.
Choice Control List –
A Choice control is used to show pop up menu of choices. Selected
choice is shown on the top of the menu.
Choice() Creates a new choice menu. o void add(String item)
Adds an item to this Choice menu.
Lists – The List component presents the user with a scrolling list of text items. List() Creates a new scrolling list.
List(int rows) Creates a new scrolling list initialized with the specified number of visible lines.
Scroll Bars- A Scrollbar control represents a scroll bar component in order to enable user to select from range of values.
Scrollbar() Constructs a new vertical scroll bar.
Scrollbar(int orientation) Constructs a new scroll bar with the specified orientation.
Text fields-
A TextField object is a text component that allows for the editing of a single line of text.
TextField() Constructs a new text field.
“Jobs and bosses will come and go, but your education will always help you to grow.” 11
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
TextField(int columns) Constructs a new empty text field with the specified number of columns.
TextField(String text) Constructs a new text field initialized with the specified text.
TextArea –
A TextArea object is a text component that allows for the editing of a multiple lines of text.
TextArea() Constructs a new text area with the empty string as text.
TextArea(int rows, int columns) Constructs a new text area with the specified number of rows and columns and the empty string as text.
16. What is the purpose of Layout Manager Interface?
Java provide us with various layout manager to position the controls. The properties
like size,shape and arrangement varies from one layout manager to other layout manager.
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 appletviewer or the application window.
The layout manager is associated with every Container object. Each layout manager is
an object of the class that implements the LayoutManager interface.
17. Explain few Layout managers interface in java?
BorderLayout
The class BorderLayout arranges the components to fit in the five regions: east,
west, north, south and center. Each region is can contain only one component
and each component in each region is identified by the corresponding constant
NORTH, SOUTH, EAST, WEST, and CENTER.
BorderLayout() Constructs a new border layout with no gaps between components.
“Jobs and bosses will come and go, but your education will always help you to grow.” 12
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
BorderLayout(int hgap, int vgap)
Constructs a border layout with the specified gaps between components.
FlowLayout
The FlowLayout is the default layout.It layouts the components in a directional flow.
FlowLayout() Constructs a new FlowLayout with a centered alignment and a default 5-unit horizontal and vertical gap.
FlowLayout(int align) Constructs a new FlowLayout with the specified alignment and a default 5-unit horizontal and vertical gap.
GridLayout- The GridLayout manages the components in form of a rectangular grid
GridLayout() Creates a grid layout with a default of one column per component, in a single row.
GridLayout(int rows, int cols) Creates a grid layout with the specified number of rows and columns.
Menu-
The Menu class represents pull-down menu component which is deployed from a menu bar.
Menu() Constructs a new menu with an empty label.
Menu(String label)
Constructs a new menu with the specified label.
MenuBar class-
“Jobs and bosses will come and go, but your education will always help you to grow.” 13
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
The MenuBar object is associated with the top-level window. The MenuBar class provides menu bar bound to a frame and is platform specific
MenuBar() Creates a new menu bar.
18. Explain Swings in detail?
Swing is a part of Java Foundation classes (JFC).
Swing API is set of extensible GUI Components to ease developer's life to
create JAVA based Front End/ GUI Applications.
It is build upon top of AWT API and acts as replacement of AWT API as it
has almost every control corresponding to AWT controls.
Swing component follows a Model-View-Controller architecture to fulfill
the following criterias.
A single API is to be sufficient to support multiple look and feel.
API is to model driven so that highest level API is not required to have the data.
API is to use the Java Bean model so that Builder Tools and IDE can provide better
services to the developers to use it.
MVC Architecture
Swing API architecture follows loosely based MVC architecture in
the following manner.
A Model represents component's data.
View represents visual representation of the component's data.
Controller takes the input from the user on the view and reflects the changes in
Component's data.
Swing component have Model as a seperate element and View and Controller part are
clubbed in User Interface elements. Using this way, Swing has pluggable look-and-feel
architecture.
Swing features
“Jobs and bosses will come and go, but your education will always help you to grow.” 14
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
Light Weight - Swing component are independent of native Operating System's API as
Swing API controls are rendered mostly using pure JAVA code instead of underlying
operating system calls.
Rich controls - Swing provides a rich set of advanced controls like Tree, TabbedPane,
slider, colorpicker, table controls
Highly Customizable - Swing controls can be customized in very easy way as visual
apperance is independent of internal representation.
Pluggable look-and-feel- SWING based GUI Application look and feel can be changed at
run time based on available values.
19. Explain different Swing classes ?
JPanel : JPanel is Swing's version of AWT class Panel and uses the same default
layout, FlowLayout. JPanel is descended directly from JComponent.
JFrame : JFrame is Swing's version of Frame and is descended directly
from Frame class. The component which is added to the Frame, is refered as its
Content.
JWindow : This is Swing's version of Window and has descended directly
from Window class. Like Window it uses BorderLayout by default.
JLabel : JLabel descended from Jcomponent, and is used to create text labels.
JButton : JButton class provides the functioning of push button. JButton allows
an icon, string or both associated with a button.
JTextField : JTextFields allow editing of a single line of text.
20. Explain few swing components in java?
JButton -
JButton class provides functionality of a button. JButton class has three
constuctors,
JButton(Icon ic)
JButton(String str)
JButton(String str, Icon ic)
It allows a button to be created using icon, a string or both. JButton supports
ActionEvent. When a button is pressed an ActionEvent is generated.
“Jobs and bosses will come and go, but your education will always help you to grow.” 15
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
JTextField -
JTextField is used for taking input of single line of text. It is most widely
used text component. It has three constructors,
JTextField(int cols)
JTextField(String str, int cols)
JTextField(String str)
cols represent the number of columns in text field.
JCheckBox -
JCheckBox class is used to create checkboxes in frame. Following is
constructor for JCheckBox,
JCheckBox(String str)
JRadioButton -
Radio button is a group of related button in which only one can be
selected. JRadioButton class is used to create a radio button in Frames.
Following is the constructor for JRadioButton,
JRadioButton(String str)
JComboBox -
Combo box is a combination of text fields and drop-down list.JComboBox
component is used to create a combo box in Swing. Following is the
constructor for JComboBox,
JComboBox(String arr[])
“Jobs and bosses will come and go, but your education will always help you to grow.” 16
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
21. How to create swing applet with example?
Any applet that contains Swing components must be implemented with a subclass of JApplet. JApplet is a subclass of java.applet.Applet .
Example
In this example, We embed a Swing applet into web browser. This applet contains two buttons, when you click on any one of it, it shows which one is clicked.
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class SwingApplet extends JApplet { JButton jbtnOne; JButton jbtnTwo; JLabel jlab; public void init() { try { SwingUtilities.invokeAndWait(new Runnable () { public void run() { guiInit(); // initialize the GUI } }); } catch(Exception exc) { System.out.println("Can't create because of "+ exc); } } // Called second, after init(). Also called // whenever the applet is restarted. public void start() { // Not used by this applet. } // Called when the applet is stopped. public void stop() { // Not used by this applet. }
“Jobs and bosses will come and go, but your education will always help you to grow.” 17
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
// Called when applet is terminated. This is // the last method executed. public void destroy() { // Not used by this applet. } // Setup and initialize the GUI. private void guiInit() { // Set the applet to use flow layout. setLayout(new FlowLayout()); // Create two buttons and a label. jbtnOne = new JButton("One"); jbtnTwo = new JButton("Two"); jlab = new JLabel("Press a button."); // Add action listeners for the buttons. jbtnOne.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent le) { jlab.setText("Button One pressed."); } }); jbtnTwo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent le) { jlab.setText("Button Two pressed."); } }); // Add the components to the applet's content pane. getContentPane().add(jbtnOne); getContentPane().add(jbtnTwo); getContentPane().add(jlab); } }
For invoking it into a browser, the following Html code is needed :
<html> <title>The Applet Demo</title> <hr> <applet code="SwingApplet.class" width=240 height=100>
“Jobs and bosses will come and go, but your education will always help you to grow.” 18
Pemmaiah.U.T, HOD, Dept. of BCA, Cauvery College, Gonikoppal JAVA Exam Guide
This message only appears if your browser is not java enabled or there is some error. </applet> <hr> </html>
Note : I have tried to cover as much from syllabus . refer Text book and previous year question
papers .